<?php

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

return new class extends Migration
{
    /**
     * 선생산 완료 시 재고 입고를 위해 stock_lots에 work_order_id FK 추가
     * - 구매입고: receiving_id 참조
     * - 생산입고: work_order_id 참조
     */
    public function up(): void
    {
        Schema::table('stock_lots', function (Blueprint $table) {
            $table->unsignedBigInteger('work_order_id')
                ->nullable()
                ->after('receiving_id')
                ->comment('생산입고 시 작업지시 참조');

            $table->foreign('work_order_id')
                ->references('id')
                ->on('work_orders')
                ->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('stock_lots', function (Blueprint $table) {
            $table->dropForeign(['work_order_id']);
            $table->dropColumn('work_order_id');
        });
    }
};