<?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('picking_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('picking_id')->constrained()->cascadeOnDelete();
            $table->foreignId('insumo_id')->constrained()->restrictOnDelete();
            $table->foreignId('receta_id')->nullable()->constrained()->nullOnDelete();
            $table->decimal('cantidad_necesaria', 10, 3);
            $table->decimal('cantidad_despachada', 10, 3)->default(0);
            $table->boolean('completado')->default(false);
            $table->foreignId('user_id_completado')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('completado_at')->nullable();
            $table->timestamps();

            // Un insumo no puede aparecer dos veces en el mismo picking
            $table->unique(['picking_id', 'insumo_id']);
            $table->index(['picking_id', 'completado']);
        });
    }

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