<?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('warehouse_stock_ledger', function (Blueprint $table) {
            $table->id();
            $table->foreignId('item_id')->constrained('items')->onDelete('restrict');
            $table->unsignedBigInteger('warehouse_id');
            $table->enum('bucket', ['available', 'rejected', 'quarantine']);
            $table->enum('movement_type', ['inward', 'outward', 'adjustment']);
            $table->decimal('qty', 12, 2);
            $table->string('source_type'); // e.g., 'GRN', 'QC', 'PO', 'reversal'
            $table->unsignedBigInteger('source_id');
            $table->timestamps();

            // Indexes
            $table->index(['item_id', 'warehouse_id']);
            $table->index(['source_type', 'source_id']);
            $table->index('warehouse_id');
            $table->index('bucket');
            $table->index('movement_type');
        });
    }

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