<?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('room_availabilities', function (Blueprint $table) {
            $table->id();

            // The room this availability applies to
            $table->foreignId('room_id')->constrained()->cascadeOnDelete();

            // Optional reservation reference, NULL for admin blocks
            $table->foreignId('booking_id')->nullable()->constrained()->cascadeOnDelete();

            // Booking / block date range
            $table->date('start_date');
            $table->date('end_date');

            // Status: booked by guest, blocked by admin, pending reservation
            $table->enum('status', ['pending', 'booked', 'blocked', 'checked_out'])->default('pending');

            // Source of this availability: website, admin, third-party channel
            $table->string('source')->default('website');

            // Optional admin message or reason for block
            $table->string('message')->nullable();

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

            // Optional admin user who created the block
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamps();

            // Indexes for fast availability queries
            $table->index(['room_id', 'start_date', 'end_date']);
            $table->index(['status']);
        });
    }

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