<?php

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

return new class extends Migration
{
    // 설계용 BOM 항목: bom_template_items
    public function up(): void
    {
        Schema::create('bom_template_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트ID');
            $table->unsignedBigInteger('bom_template_id')->comment('BOM템플릿ID');
            $table->string('ref_type', 20)->comment('참조타입: MATERIAL|PRODUCT');
            $table->unsignedBigInteger('ref_id')->comment('참조ID(materials.id 또는 products.id)');
            $table->decimal('qty', 18, 6)->default(1)->comment('수량');
            $table->decimal('waste_rate', 9, 6)->default(0)->comment('로스율');
            $table->unsignedBigInteger('uom_id')->nullable()->comment('단위ID(참조용)');
            $table->string('notes', 255)->nullable();
            $table->integer('sort_order')->default(0);
            $table->timestamps();

            $table->index(['tenant_id', 'bom_template_id'], 'idx_bomtpl_items_tenant_tpl');
            $table->index(['tenant_id', 'ref_type', 'ref_id'], 'idx_bomtpl_items_tenant_ref');
            $table->index(['bom_template_id', 'sort_order'], 'idx_bomtpl_items_sort');
        });
    }

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