<?php

use App\Models\Document;

/*
|--------------------------------------------------------------------------
| Document API Tests
|--------------------------------------------------------------------------
|
| Feature tests for the /api/v1/documents endpoint
|
*/

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

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

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

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

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

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

it('can filter documents by document type', function () {
    $response = $this->getJson('/api/v1/documents?documentType=1');

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

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

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

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

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

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

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

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

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

/*
|--------------------------------------------------------------------------
| Document Show Endpoint Tests
|--------------------------------------------------------------------------
*/

it('returns a document by slug', function () {
    // Get a published document from the database
    $document = \App\Models\Document::full()
        ->where('status', 'published')
        ->first();

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

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

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

it('returns document with correct show structure', function () {
    $document = \App\Models\Document::full()
        ->where('status', 'published')
        ->first();

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

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

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

it('returns related documents in show response', function () {
    $document = \App\Models\Document::full()
        ->where('status', 'published')
        ->first();

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

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

    $response->assertStatus(200);

    $data = $response->json();
    expect($data)->toHaveKey('related');
    expect($data['related'])->toBeArray();
});
