<?php

use App\Models\PressRelease;

/*
|--------------------------------------------------------------------------
| Press Release API Tests
|--------------------------------------------------------------------------
|
| Feature tests for the /api/v1/press-releases endpoint
|
*/

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

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

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

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

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

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

it('can filter press releases by country', function () {
    $response = $this->getJson('/api/v1/press-releases?country=1');

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

it('can order press releases by latest (default)', function () {
    $response = $this->getJson('/api/v1/press-releases?orderBy=latest');

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

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

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

it('can order press releases by country', function () {
    $response = $this->getJson('/api/v1/press-releases?orderBy=country');

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

/*
|--------------------------------------------------------------------------
| Press Release Show Endpoint Tests
|--------------------------------------------------------------------------
*/

it('returns a press release by slug', function () {
    // Get a press release that is not expired
    $pressRelease = PressRelease::full()
        ->where('status', 'published')
        ->where(function ($query) {
            $query->whereNull('expiration_date')
                ->orWhere('expiration_date', '>', now());
        })
        ->first();

    if (!$pressRelease) {
        $this->markTestSkipped('No published non-expired press releases in the database');
    }

    $response = $this->getJson("/api/v1/press-releases/{$pressRelease->lang_slug}");

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

it('returns press release with correct show structure', function () {
    // Get a press release that is not expired
    $pressRelease = PressRelease::full()
        ->where('status', 'published')
        ->where(function ($query) {
            $query->whereNull('expiration_date')
                ->orWhere('expiration_date', '>', now());
        })
        ->first();

    if (!$pressRelease) {
        $this->markTestSkipped('No published non-expired press releases in the database');
    }

    $response = $this->getJson("/api/v1/press-releases/{$pressRelease->lang_slug}");

    $response->assertStatus(200)
        ->assertJsonStructure([
            'pressRelease',
            'related',
        ]);
});

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

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