<?php

use App\Models\EventItem;

/*
|--------------------------------------------------------------------------
| Event API Tests
|--------------------------------------------------------------------------
|
| Feature tests for the /api/v1/events endpoint
|
*/

it('returns a successful response for events index', function () {
    $response = $this->getJson('/api/v1/events');

    $response->assertStatus(200);
});

it('returns events with correct structure', function () {
    $response = $this->getJson('/api/v1/events');

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data',
            'pagination' => [
                'currentPage',
                'lastPage',
                'perPage',
                'total',
                'count',
                'hasMorePages',
                'nextPageUrl',
                'prevPageUrl',
            ],
        ]);
});

it('can paginate events', function () {
    $response = $this->getJson('/api/v1/events?perPage=5&page=1');

    $response->assertStatus(200)
        ->assertJsonPath('pagination.perPage', 5)
        ->assertJsonPath('pagination.currentPage', 1);
});

it('can filter upcoming events', function () {
    $response = $this->getJson('/api/v1/events?upcoming=1');

    $response->assertStatus(200);
});

it('can filter past events', function () {
    $response = $this->getJson('/api/v1/events?filterTime=past');

    $response->assertStatus(200);
});

it('can filter future events', function () {
    $response = $this->getJson('/api/v1/events?filterTime=upcoming');

    $response->assertStatus(200);
});

it('can order events alphabetically', function () {
    $response = $this->getJson('/api/v1/events?orderBy=alphabetical');

    $response->assertStatus(200);
});

/*
|--------------------------------------------------------------------------
| Event Show Endpoint Tests
|--------------------------------------------------------------------------
*/

it('returns an event by slug', function () {
    $event = EventItem::full()
        ->where('status', 'published')
        ->first();

    if (!$event) {
        $this->markTestSkipped('No published events in the database');
    }

    $response = $this->getJson("/api/v1/events/{$event->lang_slug}");

    $response->assertStatus(200);
});

it('returns 404 for non-existent event', function () {
    $response = $this->getJson('/api/v1/events/non-existent-slug-12345');

    $response->assertStatus(404);
});
