<?php

namespace Tests\Feature;

use App\Models\Guest;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class GuestMultiTypeTrackingTest extends TestCase
{
    private static bool $schemaReady = false;

    protected function setUp(): void
    {
        parent::setUp();
        $this->ensureGuestSchema();
    }

    private function ensureGuestSchema(): void
    {
        if (self::$schemaReady) {
            return;
        }

        if (!Schema::hasTable('guests')) {
            Schema::create('guests', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('hotel_id')->nullable();
                $table->string('first_name')->nullable();
                $table->string('last_name')->nullable();
                $table->string('email')->nullable();
                $table->string('phone')->nullable();
                $table->string('status')->nullable();
                $table->boolean('is_walkin_guest')->default(false);
                $table->boolean('is_reservation_guest')->default(false);
                $table->boolean('is_service_guest')->default(false);
                $table->boolean('is_regular_guest')->default(false);
                $table->integer('total_visits')->default(0);
                $table->date('last_visit_date')->nullable();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('reservations')) {
            Schema::create('reservations', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('hotel_id')->nullable();
                $table->unsignedBigInteger('guest_id')->nullable();
                $table->string('status')->nullable();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('service_requests')) {
            Schema::create('service_requests', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('hotel_id')->nullable();
                $table->unsignedBigInteger('guest_id')->nullable();
                $table->string('status')->nullable();
                $table->decimal('total_price', 10, 2)->default(0);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('guest_activities')) {
            Schema::create('guest_activities', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('guest_id');
                $table->string('activity_type')->nullable();
                $table->text('description')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamp('activity_date')->nullable();
                $table->unsignedBigInteger('recorded_by')->nullable();
                $table->timestamps();
            });
        }

        self::$schemaReady = true;
    }

    public function test_guest_can_be_both_reservation_and_service_type(): void
    {
        $guest = Guest::create([
            'hotel_id' => 1,
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => 'john.multi@example.com',
            'status' => 'active',
        ]);

        $guest->markAsReservationGuest();
        $guest->markAsServiceGuest();
        $guest->refresh();

        $this->assertTrue($guest->is_reservation_guest);
        $this->assertTrue($guest->is_service_guest);
        $this->assertTrue($guest->is_regular_guest);
        $this->assertTrue($guest->has_multiple_types);
    }
}

