<?php

use App\Models\GenericBackoffice\Entry;

/*
|--------------------------------------------------------------------------
| Entry API Tests
|--------------------------------------------------------------------------
|
| Feature tests for the /api/v1/entries endpoint
|
*/

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

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

it('returns entries as a collection', function () {
    $response = $this->getJson('/api/v1/entries');

    $response->assertStatus(200);

    $data = $response->json();
    expect($data)->toBeArray();
});

/*
|--------------------------------------------------------------------------
| Entry Show Endpoint Tests
|--------------------------------------------------------------------------
*/

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

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

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

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

it('returns 401 for password protected entry without password', function () {
    $entry = Entry::full()
        ->where('status', 'published')
        ->whereNotNull('password_protected')
        ->where('password_protected', '!=', '')
        ->first();

    if (!$entry) {
        $this->markTestSkipped('No password protected entries in the database');
    }

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

    $response->assertStatus(401)
        ->assertJsonPath('requires_password', true);
});

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

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