<?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_consumption', function (Blueprint $table) {
            $table->id();
            $table->string('consumption_number')->unique();
            $table->date('consumption_date');
            $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->decimal('quantity_consumed', 10, 2);
            $table->enum('consumption_type', ['consumed', 'written_off', 'damaged', 'expired']);
            $table->text('reason')->nullable();
            $table->foreignId('recorded_by')
                  ->constrained('users')->onDelete('cascade');
            $table->foreignId('approved_by')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->timestamps();

            $table->index(['project_id', 'indirect_good_id'], 'ig_consumption_proj_good_idx');
            $table->index('consumption_date', 'ig_consumption_date_idx');
            $table->index('invoice_number', 'ig_consumption_invoice_idx');
        });
    }

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