<?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
    {
        // 1. TABEL MASTER RESEP / BOM (BILL OF MATERIALS)
        Schema::create('fabrication_formulas', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('branch_id');
            $table->unsignedBigInteger('result_product_id')->comment('Barang jadi yang dihasilkan');
            $table->decimal('result_quantity', 15, 2)->comment('Jumlah barang jadi per 1 resep');
            $table->string('name')->comment('Nama Resep/Formula');
            
            // Komponen Biaya Standar / Estimasi
            $table->decimal('labor_cost_estimate', 15, 2)->default(0)->comment('Estimasi Biaya Tenaga Kerja (BTKL)');
            $table->decimal('overhead_cost_estimate', 15, 2)->default(0)->comment('Estimasi Biaya Overhead/Listrik (BOP)');
            
            $table->boolean('is_active')->default(true);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();

            $table->foreign('branch_id')->references('id')->on('branches')->onDelete('restrict');
            $table->foreign('result_product_id')->references('id')->on('products')->onDelete('restrict');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
        });

        // 2. TABEL KOMPONEN BAHAN BAKU RESEP
        Schema::create('fabrication_formula_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('fabrication_formula_id');
            $table->unsignedBigInteger('material_product_id')->comment('Bahan baku yang dipakai');
            $table->decimal('quantity_needed', 15, 2)->comment('Jumlah bahan baku yang dibutuhkan');
            $table->timestamps();

            $table->foreign('fabrication_formula_id', 'fk_formula_items_formula_id')
                  ->references('id')->on('fabrication_formulas')->onDelete('cascade');
            $table->foreign('material_product_id', 'fk_formula_items_material_id')
                  ->references('id')->on('products')->onDelete('restrict');
        });

        // 3. TABEL SURAT PERINTAH KERJA (SPK) / TRANSAKSI PRODUKSI
        Schema::create('fabrication_orders', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('branch_id');
            $table->unsignedBigInteger('fabrication_formula_id');
            $table->unsignedBigInteger('user_id')->comment('Mandor / Pembuat SPK');
            
            $table->string('spk_number')->unique();
            $table->enum('status', ['draft', 'processing', 'completed', 'void'])->default('draft');
            
            // Pengali (Berapa kali resep ini dibuat dalam 1 SPK)
            $table->decimal('multiplier', 15, 2)->default(1)->comment('Faktor pengali resep');
            $table->decimal('target_quantity', 15, 2)->comment('Total barang jadi yang dihasilkan');
            
            // Rekaman HPP saat SPK Diselesaikan (Snapshot)
            $table->decimal('total_material_cost', 15, 2)->default(0)->comment('Total nilai HPP Bahan Baku yang terpotong');
            $table->decimal('total_labor_cost', 15, 2)->default(0)->comment('Total BTKL yang dibebankan');
            $table->decimal('total_overhead_cost', 15, 2)->default(0)->comment('Total BOP yang dibebankan');
            $table->decimal('grand_total_cost', 15, 2)->default(0)->comment('Total HPP Barang Jadi');
            
            $table->text('notes')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->foreign('branch_id')->references('id')->on('branches')->onDelete('restrict');
            $table->foreign('fabrication_formula_id')->references('id')->on('fabrication_formulas')->onDelete('restrict');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');
        });

        // 4. TABEL DETAIL BAHAN BAKU YANG BENAR-BENAR TERPAKAI PADA SPK
        // (Berfungsi sebagai histori absolut jika di masa depan resep berubah)
        Schema::create('fabrication_order_materials', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('fabrication_order_id');
            $table->unsignedBigInteger('material_product_id');
            
            $table->decimal('quantity_used', 15, 2);
            $table->decimal('unit_price', 15, 2)->comment('HPP bahan baku per unit saat produksi terjadi');
            $table->decimal('total_price', 15, 2)->comment('quantity_used * unit_price');
            
            $table->timestamps();

            $table->foreign('fabrication_order_id', 'fk_order_materials_order_id')
                  ->references('id')->on('fabrication_orders')->onDelete('cascade');
            $table->foreign('material_product_id', 'fk_order_materials_product_id')
                  ->references('id')->on('products')->onDelete('restrict');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('fabrication_order_materials');
        Schema::dropIfExists('fabrication_orders');
        Schema::dropIfExists('fabrication_formula_items');
        Schema::dropIfExists('fabrication_formulas');
    }
};