<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Schema::dropIfExists('booking_rooms');
        // Schema::dropIfExists('bookings');
        Schema::create('bookings', function (Blueprint $table) {
            $table->id();
            $table->string('reference')->unique()->index();

            $table->foreignId('guest_id')->constrained('guests')->onDelete('cascade');

            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();


            $table->string('source')->default('guest');

            $table->date('check_in');
            $table->date('check_out');

            $table->decimal('total_price', 12, 2); // in base currency
            $table->string('base_currency', 3);

            $table->decimal('display_total_price', 12, 2)->nullable(); // in base currency
            $table->string('display_currency', 3)->nullable();
            $table->decimal('exchange_rate', 12, 6)->default(1.0); // snapshot at booking time
            $table->foreignId('exchange_rate_id')->nullable()->constrained('exchange_rates')->nullOnDelete();

            $table->json('price_data')->nullable();

            $table->enum('payment_option', ['pay_now', 'pay_at_hotel'])->default('pay_now');
            $table->enum('status', ['pending', 'booked', 'cancelled', 'checked-in', 'checked-out'])->default('pending');

            $table->timestamp('booked_at')->nullable();
            $table->timestamp('cancelled_at')->nullable();
            $table->timestamp('checked_in_at')->nullable();
            $table->timestamp('checked_out_at')->nullable();

            $table->json('addons')->nullable();       // snapshot of booking addons
            $table->decimal('addon_total', 12, 2)->default(0);

            $table->text('note')->nullable();

            $table->timestamps();
        });

        Schema::create('booking_rooms', function (Blueprint $table) {
            $table->id();

            $table->foreignId('booking_id')->constrained()->cascadeOnDelete();
            $table->foreignId('room_id')->nullable()->constrained()->nullOnDelete(); // optional reference

            $table->string('room_title');
            $table->string('room_slug');
            $table->string('room_number')->nullable();

            $table->unsignedInteger('adults')->default(1);
            $table->unsignedInteger('children')->default(0);

            $table->json('meal_package')->nullable(); // snapshot of meal package
            $table->json('addons')->nullable();       // snapshot of room 

            $table->json('room_daily_prices');

            $table->decimal('meal_total', 12, 2)->default(0);     // total of meal package
            $table->decimal('addon_total', 12, 2)->default(0);    // total of room addons
            $table->decimal('grand_total', 12, 2)->default(0);    // room_total + meal_total + addon_total
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('booking_rooms');
        Schema::dropIfExists('bookings');
    }
};
