<?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('item_bom_items', function (Blueprint $table) {
            $table->id()->comment('BOM 항목 ID');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('section_id')->comment('섹션 ID');
            $table->string('item_code', 100)->nullable()->comment('품목 코드');
            $table->string('item_name')->comment('품목명');
            $table->decimal('quantity', 15, 4)->default(0)->comment('수량');
            $table->string('unit', 50)->nullable()->comment('단위');
            $table->decimal('unit_price', 15, 2)->nullable()->comment('단가');
            $table->decimal('total_price', 15, 2)->nullable()->comment('총액');
            $table->text('spec')->nullable()->comment('사양');
            $table->text('note')->nullable()->comment('비고');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자 ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자 ID');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes()->comment('소프트 삭제');

            // 인덱스
            $table->index(['tenant_id', 'section_id'], 'idx_item_bom_items_tenant_section');

            // 외래키
            $table->foreign('tenant_id', 'fk_item_bom_items_tenant')
                ->references('id')->on('tenants')
                ->onDelete('cascade');
            $table->foreign('section_id', 'fk_item_bom_items_section')
                ->references('id')->on('item_sections')
                ->onDelete('cascade');
        });
    }

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