<?php

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

/**
 * Packages, package items (composed from ERP products so costing flows),
 * add-ons (fireworks, sparks, extra cameras…) and per-hall add-on
 * availability. Packages and add-ons map to ERP products/services so
 * selections feed the BEO and the eventual invoice.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_packages')) {
            Schema::create('bms_packages', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('venue_id')->nullable(); // null = business-wide
                $t->unsignedBigInteger('hall_id')->nullable();  // null = all halls of the venue
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->text('description')->nullable();
                $t->enum('fnb_level', ['high_tea', 'drinks_only', 'set_menu', 'open_buffet', 'custom'])->nullable();
                $t->decimal('price', 15, 2)->default(0);
                $t->boolean('price_per_person')->default(false);
                $t->unsignedInteger('min_pax')->nullable();
                $t->unsignedInteger('max_pax')->nullable();
                $t->boolean('is_active')->default(true);
                $t->integer('sort_order')->default(0);
                $t->timestamps();
                $t->index(['business_id', 'hall_id']);
                $t->index(['business_id', 'venue_id']);
            });
        }

        if (! Schema::hasTable('bms_package_items')) {
            Schema::create('bms_package_items', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('package_id');
                $t->unsignedInteger('product_id')->nullable();   // ERP product
                $t->unsignedInteger('variation_id')->nullable(); // ERP variation
                $t->string('name', 191);
                $t->decimal('quantity', 10, 2)->default(1);
                $t->decimal('unit_cost', 15, 2)->nullable();
                $t->timestamps();
                $t->index(['business_id', 'package_id']);
            });
        }

        if (! Schema::hasTable('bms_addons')) {
            Schema::create('bms_addons', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('product_id')->nullable();   // maps to ERP product/service
                $t->unsignedInteger('variation_id')->nullable();
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('category', 60)->nullable();          // entertainment, media, decor…
                $t->text('description')->nullable();
                $t->decimal('price', 15, 2)->default(0);
                $t->string('unit', 30)->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_hall_addon')) {
            Schema::create('bms_hall_addon', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('hall_id');
                $t->unsignedBigInteger('addon_id');
                $t->decimal('price_override', 15, 2)->nullable();
                $t->boolean('is_available')->default(true);
                $t->timestamps();
                $t->unique(['hall_id', 'addon_id']);
                $t->index(['business_id', 'hall_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('bms_hall_addon');
        Schema::dropIfExists('bms_addons');
        Schema::dropIfExists('bms_package_items');
        Schema::dropIfExists('bms_packages');
    }
};
