<?php

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

/**
 * Asset registry (Release 2, §5.5): every trackable item (chairs, tables, AV,
 * décor) carries identity (code/QR optional), current location, condition and a
 * full movement history. Flowers and similar consumables stay in ERP inventory
 * and move through StockServiceContract instead — assets move through
 * AssetServiceContract, which writes the movement audit below.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_assets')) {
            Schema::create('bms_assets', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('code', 60);                       // asset tag / QR payload
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('category', 40)->default('other'); // seating/tables/av/lighting/decor/linen/other
                $t->unsignedInteger('location_id')->nullable();   // current ERP business_location (home/warehouse)
                $t->unsignedBigInteger('hall_id')->nullable();    // current hall placement (if any)
                $t->string('condition', 30)->default('good');     // good/fair/worn/damaged/retired
                $t->string('status', 30)->default('available');   // available/in_use/in_transit/maintenance/retired
                $t->unsignedInteger('quantity')->default(1);      // batch count (e.g. a lot of 200 chairs)
                $t->decimal('unit_value', 12, 2)->nullable();
                $t->string('serial_no', 120)->nullable();
                $t->date('purchased_at')->nullable();
                $t->string('photo_path', 255)->nullable();
                $t->text('notes')->nullable();
                $t->timestamps();
                $t->unique(['business_id', 'code']);
                $t->index(['business_id', 'status']);
                $t->index(['business_id', 'category']);
                $t->index(['hall_id']);
            });
        }

        if (! Schema::hasTable('bms_asset_movements')) {
            Schema::create('bms_asset_movements', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('asset_id');
                $t->unsignedInteger('from_location_id')->nullable();
                $t->unsignedBigInteger('from_hall_id')->nullable();
                $t->unsignedInteger('to_location_id')->nullable();
                $t->unsignedBigInteger('to_hall_id')->nullable();
                $t->unsignedBigInteger('transfer_id')->nullable();  // links bms_transfer_requests
                $t->decimal('quantity', 10, 2)->nullable();
                $t->string('condition', 30)->nullable();            // condition recorded at this move
                $t->string('reason', 40)->nullable();               // register/transfer/manual/maintenance
                $t->unsignedInteger('moved_by')->nullable();
                $t->string('notes', 255)->nullable();
                $t->timestamps();
                $t->index(['business_id', 'asset_id']);
                $t->index(['transfer_id']);
            });
        }
    }

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