<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('sop_steps', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('sop_template_id')->comment('模板ID');
            $table->integer('step_number')->comment('步骤序号');
            $table->string('title')->comment('步骤标题');
            $table->text('content')->nullable()->comment('步骤内容');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->boolean('is_required')->default(true)->comment('是否必需');
            $table->timestamps();

            // 添加外键约束
            $table->foreign('sop_template_id')
                  ->references('id')
                  ->on('sop_templates')
                  ->onDelete('cascade');

            // 添加索引
            $table->index(['sop_template_id', 'sort_order'], 'idx_template_sort');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sop_steps');
    }
};
