<?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('process_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('process_id')->comment('공정 ID');
            $table->unsignedBigInteger('item_id')->comment('품목 ID');
            $table->unsignedInteger('priority')->default(0)->comment('우선순위');
            $table->boolean('is_active')->default(true)->comment('활성여부');
            $table->timestamps();

            // 외래키
            $table->foreign('process_id')->references('id')->on('processes')->onDelete('cascade');
            $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');

            // 유니크 제약 (동일 공정-품목 중복 방지)
            $table->unique(['process_id', 'item_id']);

            // 인덱스
            $table->index('process_id');
            $table->index('item_id');
            $table->index(['process_id', 'is_active']);
        });
    }

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