<?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('inventory_stock_transfers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('status', 32);
            $table->foreignId('source_warehouse_id')->constrained('inventory_warehouses')->restrictOnDelete();
            $table->foreignId('destination_warehouse_id')->constrained('inventory_warehouses')->restrictOnDelete();
            $table->foreignId('item_id')->constrained('inventory_items')->restrictOnDelete();
            $table->decimal('quantity', 24, 8);
            $table->uuid('correlation_id')->nullable();
            $table->foreignId('transfer_out_movement_id')->nullable()->constrained('inventory_stock_movements')->nullOnDelete();
            $table->foreignId('transfer_in_movement_id')->nullable()->constrained('inventory_stock_movements')->nullOnDelete();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'status'], 'inv_stock_xfer_tenant_status');
        });
    }

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