<?php

namespace Tests\Feature;

use App\Enums\EventStatus;
use App\Models\Category;
use App\Models\Event;
use App\Models\Organizer;
use App\Models\User;
use App\Models\Venue;
use Database\Seeders\DemoCatalogSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LandingEventsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DemoCatalogSeeder::class);
    }

    public function test_eventos_catalog_returns_ok(): void
    {
        $this->get(route('landing.events'))->assertOk();
    }

    public function test_published_listed_event_is_visible_on_eventos(): void
    {
        $user = User::factory()->organizador()->create();
        $organizer = Organizer::query()->create(['user_id' => $user->id, 'stripe_account_id' => null]);
        $venue = Venue::query()->firstOrFail();
        $category = Category::query()->firstOrFail();

        $start = now()->addWeeks(3)->startOfHour();
        Event::query()->create([
            'organizer_id' => $organizer->id,
            'venue_id' => $venue->id,
            'category_id' => $category->id,
            'title' => 'Evento público listado',
            'slug' => 'evento-publico-listado-'.uniqid(),
            'description' => null,
            'short_description' => 'Resumen',
            'start_date' => $start,
            'end_date' => $start->copy()->addHours(2),
            'timezone' => 'America/Puerto_Rico',
            'status' => EventStatus::Published,
            'hide_from_listings' => false,
            'is_featured' => false,
        ]);

        $this->get(route('landing.events'))
            ->assertOk()
            ->assertSee('Evento público listado', false);
    }

    public function test_draft_event_show_returns_not_found(): void
    {
        $user = User::factory()->organizador()->create();
        $organizer = Organizer::query()->create(['user_id' => $user->id, 'stripe_account_id' => null]);
        $venue = Venue::query()->firstOrFail();
        $category = Category::query()->firstOrFail();

        $start = now()->addWeeks(3)->startOfHour();
        $event = Event::query()->create([
            'organizer_id' => $organizer->id,
            'venue_id' => $venue->id,
            'category_id' => $category->id,
            'title' => 'Borrador',
            'slug' => 'evento-borrador-'.uniqid(),
            'description' => null,
            'short_description' => null,
            'start_date' => $start,
            'end_date' => $start->copy()->addHours(2),
            'timezone' => 'America/Puerto_Rico',
            'status' => EventStatus::Draft,
            'hide_from_listings' => false,
            'is_featured' => false,
        ]);

        $this->get(route('landing.event.show', ['event' => $event->slug]))->assertNotFound();
    }

    public function test_published_event_show_returns_ok(): void
    {
        $user = User::factory()->organizador()->create();
        $organizer = Organizer::query()->create(['user_id' => $user->id, 'stripe_account_id' => null]);
        $venue = Venue::query()->firstOrFail();
        $category = Category::query()->firstOrFail();

        $start = now()->addWeeks(3)->startOfHour();
        $event = Event::query()->create([
            'organizer_id' => $organizer->id,
            'venue_id' => $venue->id,
            'category_id' => $category->id,
            'title' => 'Evento detalle',
            'slug' => 'evento-detalle-'.uniqid(),
            'description' => '<p>Descripción</p>',
            'short_description' => 'Resumen corto',
            'start_date' => $start,
            'end_date' => $start->copy()->addHours(2),
            'timezone' => 'America/Puerto_Rico',
            'status' => EventStatus::Published,
            'hide_from_listings' => false,
            'is_featured' => false,
        ]);

        $this->get(route('landing.event.show', ['event' => $event->slug]))
            ->assertOk()
            ->assertSee('Evento detalle', false);
    }
}
