<?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('shipment_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->comment('테넌트 ID');
            $table->foreignId('shipment_id')->comment('출하 ID');
            $table->integer('seq')->default(1)->comment('순번');
            $table->string('item_code', 50)->nullable()->comment('품목코드');
            $table->string('item_name', 100)->comment('품목명');
            $table->string('floor_unit', 50)->nullable()->comment('층/M호');
            $table->string('specification', 100)->nullable()->comment('규격');
            $table->decimal('quantity', 10, 2)->default(0)->comment('수량');
            $table->string('unit', 20)->nullable()->comment('단위');
            $table->string('lot_no', 50)->nullable()->comment('LOT번호');
            $table->foreignId('stock_lot_id')->nullable()->comment('재고 LOT ID');
            $table->text('remarks')->nullable()->comment('비고');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index(['shipment_id', 'seq']);
            $table->index(['tenant_id', 'item_code']);

            // 외래키
            $table->foreign('shipment_id')->references('id')->on('shipments')->onDelete('cascade');
        });
    }

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