<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Item requests & transfers (scenarios 2.5–2.7): warehouse→hall and hall→hall
 * movements of consumables (ERP stock) and assets, each requiring explicit
 * in-app acceptance — who accepted, when, and the quantity/condition confirmed.
 * No paper anywhere in the chain.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_transfer_requests')) {
            Schema::create('bms_transfer_requests', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->enum('type', ['warehouse_to_hall', 'hall_to_hall'])->default('warehouse_to_hall');
                $t->unsignedInteger('from_location_id')->nullable(); // ERP business_location (source warehouse)
                $t->unsignedBigInteger('from_hall_id')->nullable();   // for hall→hall
                $t->unsignedBigInteger('to_hall_id');                 // destination hall
                $t->unsignedInteger('to_location_id')->nullable();    // denormalized destination location
                $t->unsignedBigInteger('booking_id')->nullable();
                $t->enum('status', ['requested', 'in_transit', 'accepted', 'rejected', 'cancelled'])->default('requested');
                $t->unsignedInteger('requested_by')->nullable();
                $t->unsignedInteger('accepted_by')->nullable();
                $t->dateTime('accepted_at')->nullable();
                $t->string('notes', 255)->nullable();
                $t->json('stock_refs')->nullable();   // posted ERP movement references
                $t->timestamps();
                $t->index(['business_id', 'status']);
                $t->index(['to_hall_id']);
                $t->index(['business_id', 'booking_id']);
            });
        }

        if (! Schema::hasTable('bms_transfer_items')) {
            Schema::create('bms_transfer_items', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('transfer_id');
                $t->enum('kind', ['consumable', 'asset'])->default('consumable');
                $t->unsignedInteger('product_id')->nullable();    // ERP product (consumables)
                $t->unsignedInteger('variation_id')->nullable();
                $t->string('name', 191);
                $t->decimal('quantity', 10, 2)->default(1);
                $t->decimal('accepted_quantity', 10, 2)->nullable();
                $t->string('condition', 60)->nullable();          // good / damaged / short
                $t->timestamps();
                $t->index(['business_id', 'transfer_id']);
            });
        }
    }

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