<?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_stock_movements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('indirect_good_id')
                  ->constrained('indirect_goods')->onDelete('cascade');
            $table->enum('movement_type', ['in', 'out', 'adjustment', 'consumption']);
            $table->string('reference_type');
            $table->unsignedBigInteger('reference_id');
            $table->decimal('quantity_before', 10, 2)->default(0);
            $table->decimal('quantity_moved', 10, 2);
            $table->decimal('quantity_after', 10, 2)->default(0);
            $table->string('invoice_number', 100)->nullable();
            $table->foreignId('division_id')->nullable()
                  ->constrained('project_divisions')->onDelete('set null');
            $table->foreignId('user_id')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['indirect_good_id', 'movement_type'], 'ig_stock_mv_good_type_idx');
            $table->index('invoice_number', 'ig_stock_mv_invoice_idx');
            $table->index('created_at', 'ig_stock_mv_created_idx');
        });
    }

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