<?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_scrap_breakdowns', function (Blueprint $table) {
            $table->id();
            $table->foreignId('manufacturing_run_id')->constrained('manufacturing_runs')->cascadeOnDelete();
            $table->foreignId('ingredient_account_id')->constrained('accounts')->restrictOnDelete();
            $table->decimal('expected_qty', 15, 4); // Expected quantity based on BOM
            $table->decimal('actual_qty', 15, 4); // Actual quantity used
            $table->decimal('scrap_qty', 15, 4); // Scrap quantity (expected - actual)
            $table->decimal('scrap_percentage', 5, 2); // Scrap percentage
            $table->string('unit', 20); // Unit of measure
            $table->text('notes')->nullable(); // Additional notes
            $table->timestamps();

            // Indexes
            $table->index(['manufacturing_run_id', 'ingredient_account_id'], 'msb_run_ingredient_idx');
        });
    }

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