<?php

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

return new class extends Migration
{
    /**
     * 작업지시 품목 테이블 (Work Order Items)
     */
    public function up(): void
    {
        Schema::create('work_order_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('work_order_id')->comment('작업지시ID');
            $table->unsignedBigInteger('item_id')->nullable()->comment('품목ID');
            $table->string('item_name', 200)->comment('품목명');
            $table->string('specification', 500)->nullable()->comment('규격');
            $table->decimal('quantity', 12, 2)->default(1)->comment('수량');
            $table->string('unit', 20)->nullable()->comment('단위');
            $table->integer('sort_order')->default(0)->comment('정렬순서');
            $table->timestamps();

            // Foreign Keys
            $table->foreign('work_order_id')
                ->references('id')
                ->on('work_orders')
                ->onDelete('cascade');

            // Indexes
            $table->index(['work_order_id', 'sort_order'], 'idx_work_order_items_order_sort');
        });
    }

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