<?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('insumo_stock', function (Blueprint $table) {
            $table->id();
            $table->foreignId('insumo_id')->constrained()->cascadeOnDelete();
            $table->foreignId('sucursal_id')->constrained('sucursales')->cascadeOnDelete();
            $table->decimal('cantidad_actual', 10, 3)->default(0);
            $table->decimal('cantidad_minima', 10, 3)->default(0);
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

            // Un insumo solo puede tener un registro de stock por sucursal
            $table->unique(['insumo_id', 'sucursal_id']);
            $table->index(['sucursal_id', 'cantidad_actual', 'cantidad_minima'], 'idx_stock_alerta');
        });
    }

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