<?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('transferencias_bodega', function (Blueprint $table) {
            $table->id();
            $table->foreignId('producto_id')->constrained('productos')->onDelete('cascade');
            $table->foreignId('bodega_origen_id')->constrained('bodegas')->onDelete('cascade');
            $table->foreignId('bodega_destino_id')->constrained('bodegas')->onDelete('cascade');
            $table->integer('cantidad');
            $table->text('motivo')->nullable();
            $table->foreignId('usuario_id')->constrained('users')->onDelete('cascade');
            $table->timestamp('fecha_transferencia');
            $table->timestamps();
            
            // Índices para optimizar consultas
            $table->index(['producto_id', 'fecha_transferencia']);
            $table->index(['bodega_origen_id', 'fecha_transferencia']);
            $table->index(['bodega_destino_id', 'fecha_transferencia']);
        });
    }

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