<?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_current_stock', function (Blueprint $table) {
            $table->id();
            $table->foreignId('item_id')->constrained('items')->onDelete('restrict');
            $table->unsignedBigInteger('warehouse_id');
            $table->enum('bucket', ['available', 'rejected', 'quarantine']);
            $table->decimal('current_qty', 12, 2)->default(0);
            $table->timestamps();

            // Unique constraint for upsert operations
            $table->unique(['item_id', 'warehouse_id', 'bucket']);

            // Indexes
            $table->index('warehouse_id');
            $table->index('item_id');
            $table->index('bucket');
        });
    }

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