<?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('work_order_step_progress', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->foreignId('work_order_id')->constrained('work_orders')->cascadeOnDelete()->comment('작업지시 ID');
            $table->foreignId('process_step_id')->constrained('process_steps')->cascadeOnDelete()->comment('공정 단계 ID');
            $table->unsignedBigInteger('work_order_item_id')->nullable()->comment('작업지시 품목 ID (특정 품목 연결 시)');
            $table->string('status', 20)->default('waiting')->comment('상태: waiting/in_progress/completed');
            $table->timestamp('completed_at')->nullable()->comment('완료 일시');
            $table->unsignedBigInteger('completed_by')->nullable()->comment('완료 처리자 ID');
            $table->timestamps();

            // 인덱스
            $table->unique(['work_order_id', 'process_step_id', 'work_order_item_id'], 'uq_wo_step_progress');
            $table->index(['tenant_id', 'work_order_id'], 'idx_wo_step_tenant');
            $table->index(['work_order_id', 'status'], 'idx_wo_step_status');
        });
    }

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