<?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('m_fstr_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('fstr_id');
            $table->unsignedBigInteger('raw_material_id');
            $table->decimal('quantity', 10, 4);
            $table->string('unit', 20);
            $table->decimal('unit_cost', 12, 2)->default(0);
            $table->decimal('total_cost', 15, 2)->default(0);
            $table->text('notes')->nullable();
            $table->boolean('delete_flag')->default(false);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            // Indexes
            $table->index(['fstr_id', 'delete_flag']);
            $table->index(['raw_material_id', 'delete_flag']);

            // Foreign key constraints (jika tabel sudah ada)
            // $table->foreign('fstr_id')->references('id')->on('m_fstrs')->onDelete('cascade');
            // $table->foreign('raw_material_id')->references('id')->on('m_raw_materials');
            // $table->foreign('created_by')->references('id')->on('users');
            // $table->foreign('updated_by')->references('id')->on('users');
        });
    }

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