<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('process_steps', function (Blueprint $table) {
            $table->id();
            $table->foreignId('process_id')
                ->comment('공정 ID')
                ->constrained('processes')
                ->cascadeOnDelete();
            $table->string('step_code', 20)->comment('단계코드 (STP-001)');
            $table->string('step_name', 100)->comment('단계명');
            $table->boolean('is_required')->default(false)->comment('필수여부');
            $table->boolean('needs_approval')->default(false)->comment('승인필요여부');
            $table->boolean('needs_inspection')->default(false)->comment('검사필요여부');
            $table->boolean('is_active')->default(true)->comment('사용여부');
            $table->unsignedInteger('sort_order')->default(0)->comment('정렬순서');
            $table->string('connection_type', 20)->nullable()->comment('연결유형 (팝업/없음)');
            $table->string('connection_target', 255)->nullable()->comment('연결대상');
            $table->string('completion_type', 30)->nullable()->comment('완료유형 (선택완료시완료/클릭시완료)');
            $table->timestamps();

            $table->unique(['process_id', 'step_code']);
            $table->index(['process_id', 'is_active', 'sort_order']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('process_steps');
    }
};
