<?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('inventory_movements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('item_id')->constrained()->cascadeOnDelete();
            $table->enum('type', ['IN', 'OUT']);
            $table->decimal('quantity', 10, 3);
            $table->decimal('unit_cost', 10, 4)->default(0);
            $table->decimal('total_cost', 12, 4)->default(0);
            $table->decimal('balance_after', 15, 3)->default(0); // IMPORTANT
            $table->enum('reference_type', [
                'receiving',
                'feed_consumption',
                'move_order',
                'misc_receipt',
                'misc_issue',
                'transfer_out',
                'transfer_in'
            ]);
            $table->unsignedBigInteger('reference_id');
            $table->text('remarks')->nullable();
            $table->foreignId('user_id');
            $table->timestamps();
            $table->softDeletes();
            $table->index('item_id');
            $table->index(['reference_type', 'reference_id']);
        });
    }

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