<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('indirect_goods_project_inventory', function (Blueprint $table) {
            $table->id();
            $table->foreignId('project_id')
                  ->constrained('projects')->onDelete('cascade');
            $table->foreignId('indirect_good_id')
                  ->constrained('indirect_goods')->onDelete('cascade');
            $table->string('invoice_number', 100)->nullable();
            $table->string('batch_number', 50)->nullable();
            $table->decimal('quantity_available', 10, 2)->default(0);
            $table->decimal('quantity_consumed', 10, 2)->default(0);
            $table->decimal('unit_price', 10, 2)->nullable();
            $table->decimal('total_value', 15, 2)->default(0);
            $table->timestamps();

            $table->index(['project_id', 'indirect_good_id'], 'ig_proj_inv_project_good_idx');
            $table->index('invoice_number', 'ig_proj_inv_invoice_idx');
            $table->index('quantity_available', 'ig_proj_inv_qty_idx');
        });
    }

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