<?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_material_inputs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->foreignId('work_order_id')->constrained('work_orders')->cascadeOnDelete()->comment('작업지시 ID');
            $table->foreignId('work_order_item_id')->constrained('work_order_items')->cascadeOnDelete()->comment('개소(작업지시품목) ID');
            $table->unsignedBigInteger('stock_lot_id')->comment('투입 로트 ID');
            $table->unsignedBigInteger('item_id')->comment('자재 품목 ID');
            $table->decimal('qty', 12, 3)->comment('투입 수량');
            $table->unsignedBigInteger('input_by')->nullable()->comment('투입자 ID');
            $table->timestamp('input_at')->useCurrent()->comment('투입 시각');
            $table->timestamps();

            // 인덱스
            $table->index('tenant_id', 'idx_womi_tenant');
            $table->index(['work_order_id', 'work_order_item_id'], 'idx_womi_wo_item');
            $table->index('stock_lot_id', 'idx_womi_lot');
        });
    }

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