<?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('receivings', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('receiving_number', 30)->comment('입고번호');
            $table->string('order_no', 30)->nullable()->comment('발주번호');
            $table->date('order_date')->nullable()->comment('발주일자');
            $table->unsignedBigInteger('item_id')->nullable()->comment('품목 ID');
            $table->string('item_code', 50)->comment('품목코드');
            $table->string('item_name', 200)->comment('품목명');
            $table->string('specification', 200)->nullable()->comment('규격');
            $table->string('supplier', 100)->comment('공급업체');
            $table->decimal('order_qty', 15, 2)->comment('발주수량');
            $table->string('order_unit', 20)->default('EA')->comment('발주단위');
            $table->date('due_date')->nullable()->comment('납기일');
            $table->decimal('receiving_qty', 15, 2)->nullable()->comment('입고수량');
            $table->date('receiving_date')->nullable()->comment('입고일자');
            $table->string('lot_no', 50)->nullable()->comment('입고 LOT번호');
            $table->string('supplier_lot', 50)->nullable()->comment('공급업체 LOT');
            $table->string('receiving_location', 100)->nullable()->comment('입고위치');
            $table->string('receiving_manager', 50)->nullable()->comment('입고담당');
            $table->string('status', 30)->default('order_completed')->comment('상태: order_completed/shipping/inspection_pending/receiving_pending/completed');
            $table->text('remark')->nullable()->comment('비고');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');
            $table->softDeletes();
            $table->timestamps();

            // 인덱스
            $table->unique(['tenant_id', 'receiving_number'], 'uk_tenant_receiving_number');
            $table->index(['tenant_id', 'status'], 'idx_tenant_status');
            $table->index(['tenant_id', 'receiving_date'], 'idx_tenant_receiving_date');
            $table->index('item_id', 'idx_item');
            $table->index('order_no', 'idx_order_no');
        });
    }

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