<?php

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

/**
 * Venue structure: coverage areas, venues (properties), halls (bookable
 * spaces) and hall media galleries.
 *
 * All tables are prefixed bms_ and scoped by business_id. References into
 * ERP core tables (business_locations, users, products) are plain indexed
 * columns — no cross-table FK constraints — matching the existing module
 * convention so a single ERP install can serve many businesses cleanly.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_areas')) {
            Schema::create('bms_areas', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable(); // mapped ERP business_location
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->text('description')->nullable();
                $t->boolean('is_active')->default(true);
                $t->integer('sort_order')->default(0);
                $t->timestamps();
                $t->index(['business_id', 'is_active']);
            });
        }

        if (! Schema::hasTable('bms_venues')) {
            Schema::create('bms_venues', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable(); // ERP business_location this venue belongs to
                $t->unsignedBigInteger('area_id')->nullable();
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('code', 60)->nullable();
                $t->text('address')->nullable();
                $t->string('city', 100)->nullable();
                $t->string('phone', 30)->nullable();
                $t->text('description')->nullable();
                $t->boolean('is_active')->default(true);
                $t->integer('sort_order')->default(0);
                $t->timestamps();
                $t->index(['business_id', 'is_active']);
                $t->index(['business_id', 'location_id']);
            });
        }

        if (! Schema::hasTable('bms_halls')) {
            Schema::create('bms_halls', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('venue_id');
                $t->unsignedInteger('location_id')->nullable();  // denormalized from venue for matching/scoping
                $t->unsignedBigInteger('area_id')->nullable();   // denormalized from venue
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('code', 60)->nullable();
                $t->unsignedInteger('capacity_min')->default(0);
                $t->unsignedInteger('capacity_max')->default(0);
                $t->enum('venue_type', ['open', 'closed', 'both'])->default('closed');
                $t->json('fnb_levels')->nullable();              // supported F&B levels
                $t->decimal('min_budget', 15, 2)->default(0);
                $t->decimal('base_price', 15, 2)->nullable();
                $t->decimal('area_sqm', 10, 2)->nullable();
                $t->unsignedInteger('hall_manager_id')->nullable(); // ERP user
                $t->json('features')->nullable();                // extensible flags (parking, stage, valet, bridal_room…)
                $t->text('description')->nullable();
                $t->boolean('is_active')->default(true);
                $t->integer('sort_order')->default(0);
                $t->timestamps();
                $t->index(['business_id', 'is_active']);
                $t->index(['venue_id']);
                $t->index(['business_id', 'location_id']);
            });
        }

        if (! Schema::hasTable('bms_hall_media')) {
            Schema::create('bms_hall_media', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('hall_id');
                $t->enum('type', ['image', 'video'])->default('image');
                $t->string('disk', 60)->nullable();              // SmartStorage disk name
                $t->string('path', 255)->nullable();
                $t->string('url', 500)->nullable();              // external/CDN URL if not on a disk
                $t->string('thumbnail_path', 255)->nullable();
                $t->string('title', 191)->nullable();
                $t->boolean('is_cover')->default(false);
                $t->integer('sort_order')->default(0);
                $t->json('meta')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'hall_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('bms_hall_media');
        Schema::dropIfExists('bms_halls');
        Schema::dropIfExists('bms_venues');
        Schema::dropIfExists('bms_areas');
    }
};
