<?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('manufacturing_bom_ingredients', function (Blueprint $table) {
            $table->id();
            $table->foreignId('manufacturing_bom_id')->constrained('manufacturing_boms')->cascadeOnDelete();
            $table->foreignId('manufacturing_bom_step_id')->nullable()->constrained('manufacturing_bom_steps')->cascadeOnDelete();
            $table->foreignId('ingredient_account_id')->constrained('accounts')->restrictOnDelete();
            $table->decimal('quantity_required', 15, 4);
            $table->string('unit');
            $table->enum('wastage_type', ['percentage', 'absolute'])->default('percentage');
            $table->decimal('wastage_value', 15, 4)->default(0);
            $table->timestamps();

            $table->index(['manufacturing_bom_id', 'ingredient_account_id'], 'mb_ing_bom_account_idx');
            $table->index(['manufacturing_bom_step_id'], 'mb_ing_step_idx');
        });
    }

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