<?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::create('bookings', function (Blueprint $table): void {
            $table->id();
            $table->string('booking_number', 50)->unique();
            $table->foreignId('guest_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('created_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('room_id')->constrained()->cascadeOnDelete();
            $table->string('guest_name');
            $table->string('phone', 50)->nullable();
            $table->string('email')->nullable();
            $table->string('source', 30)->default('site');
            $table->string('booking_variant', 30)->default('regular');
            $table->string('status', 30)->default('draft');
            $table->date('arrival_date')->nullable();
            $table->date('departure_date')->nullable();
            $table->unsignedSmallInteger('nights')->default(1);
            $table->unsignedTinyInteger('adults')->default(2);
            $table->unsignedTinyInteger('children_under_2')->default(0);
            $table->boolean('breakfast')->default(false);
            $table->decimal('nightly_amount', 10, 2)->nullable();
            $table->decimal('full_amount', 10, 2)->default(0);
            $table->decimal('prepayment_amount', 10, 2)->default(0);
            $table->decimal('remaining_amount', 10, 2)->default(0);
            $table->dateTime('payment_deadline')->nullable();
            $table->text('comment_guest')->nullable();
            $table->text('comment_admin')->nullable();
            $table->string('booking_link', 2000)->nullable();
            $table->string('check_in_time_from', 10)->nullable();
            $table->string('check_in_time_to', 10)->nullable();
            $table->dateTime('confirmed_at')->nullable();
            $table->timestamps();

            $table->index(['status', 'arrival_date']);
            $table->index(['room_id', 'arrival_date']);
            $table->index(['phone', 'email']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('bookings');
    }
};
