<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {

        Schema::create('addons', function (Blueprint $table) {
            $table->id();
            $table->integer('sort_order')->default(0);

            $table->string('title');
            $table->text('description')->nullable();
            $table->string('tag')->nullable();
            $table->boolean('is_icon')->default(true);
            $table->string('icon_value')->nullable();

            $table->enum('guest_type', ['adults', 'children', 'infant', 'all']);
            $table->enum('calculation_type', [
                'per_booking',
                'per_room',
                'per_room_per_night',
                'per_night',
                'per_person',
                'per_person_per_night',
                'per_quantity',
            ])->default('per_booking');

            $table->enum('scope', ['booking', 'all_rooms', 'room_category', 'room'])->default('all_rooms');
            $table->boolean('is_published')->default(true);
            $table->boolean('default_availability')->default(true);
            $table->enum('apply_on', ['check_in', 'check_out', 'entire_stay', 'any_day', 'no_check'])->default('entire_stay');

            $table->boolean('exclude_check_in_day')->default(false);
            $table->boolean('include_check_out_day')->default(false);

            // Varinat related
            $table->boolean('is_variant')->default(false);
            $table->boolean('is_multiple_select')->default(false);

            $table->boolean('is_allow_select_quantity')->default(false);
            $table->boolean('is_allow_select_guests')->default(false);

            // if the item is varint none of blowe apply for the main item
            $table->decimal('price', 10, 2);

            $table->integer('unit')->default(1);
            $table->integer('min_quantity')->default(1);
            $table->integer('max_quantity')->nullable();

            $table->integer('min_guests')->default(1);
            $table->integer('max_guests')->nullable();

            $table->boolean('track_availability')->default(false);
            $table->enum('stock_type', ['quantity', 'guest', 'mixed'])->default('quantity');
            $table->integer('stock_quantity')->nullable();  // null = unlimited
            $table->integer('stock_guests')->nullable();   // null = unlimited

            $table->timestamps();

            $table->index('scope');
        });

        Schema::create('addon_variants', function (Blueprint $table) {
            $table->id();
            $table->foreignId('addon_id')->constrained()->cascadeOnDelete();
            $table->integer('sort_order')->default(0);

            $table->string('title');           // Car, Van, Mini Bus, Extra Bed
            $table->text('description')->nullable(); // Optional details
            $table->boolean('is_icon')->default(true);
            $table->string('icon_value')->nullable();

            $table->boolean('is_published')->default(true);
            $table->boolean('default_availability')->default(true);

            $table->decimal('price', 10, 2);  // Price for this variant

            $table->integer('unit')->default(1);
            $table->integer('min_quantity')->default(1);
            $table->integer('max_quantity')->nullable();

            $table->integer('max_guests')->nullable(); // Optional, only for capacity-based addons
            $table->integer('min_guests')->nullable(); // Optional, only for capacity-based addons

            $table->boolean('track_availability')->default(false);
            $table->enum('stock_type', ['quantity', 'guest', 'mixed'])->default('quantity');
            $table->integer('stock_quantity')->nullable();  // null = unlimited
            $table->integer('stock_guests')->nullable();   // null = unlimited

            $table->timestamps();
        });

        Schema::create('addon_room', function (Blueprint $table) {
            $table->id();
            $table->foreignId('addon_id')->constrained()->cascadeOnDelete();
            $table->foreignId('room_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
            $table->unique(['addon_id', 'room_id']);
        });

        Schema::create('addon_room_category', function (Blueprint $table) {
            $table->id();
            $table->foreignId('addon_id')->constrained()->cascadeOnDelete();
            $table->foreignId('room_category_id')->constrained()->cascadeOnDelete();
            $table->timestamps();
            $table->unique(['addon_id', 'room_category_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('addon_room_category');
        Schema::dropIfExists('addon_room');

        Schema::dropIfExists('addon_variants');
        Schema::dropIfExists('addons');
    }
};
