<?php

declare(strict_types=1);

namespace App\Tests\Feature\Reader;

use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Tests\Feature\AbstractApiTestCase;
use Zenstruck\Foundry\Test\ResetDatabase;

final class GetChapterContextTest extends AbstractApiTestCase
{
    use ResetDatabase;

    public function testItReturnsChapterContext(): void
    {
        // Arrange
        $manga = MangaFactory::createOne([
            'slug' => 'manga-1',
        ]);

        $chapter1 = ChapterFactory::createOne([
            'manga' => $manga,
            'title' => 'Chapter 1',
            'number' => 1,
            'visible' => true,
            'cbzPath' => '/path/to/chapter1.cbz',
        ]);

        $chapter2 = ChapterFactory::createOne([
            'manga' => $manga,
            'title' => 'Chapter 2',
            'number' => 2,
            'visible' => true,
            'cbzPath' => '/path/to/chapter2.cbz',
        ]);

        $chapter3 = ChapterFactory::createOne([
            'manga' => $manga,
            'title' => 'Chapter 3',
            'number' => 3,
            'visible' => true,
            'cbzPath' => '/path/to/chapter3.cbz',
        ]);

        // Act
        static::createClient()->request('GET', '/api/reader/chapter/' . $chapter1->getId());

        // Assert
        $this->assertResponseIsSuccessful();
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');

        $this->assertJsonContains([
            'id' => (string)$chapter1->getId(),
            'mangaId' => (string)$manga->getId(),
            'title' => 'Chapter 1',
            'number' => 1,
            'totalPages' => 0,
            'navigation' => [
                'hydra:member' => [
                    null, (string)$chapter2->getId(),
                ],
            ]
        ]);
    }

    public function testItReturns404ForNonExistentChapter(): void
    {
        // Act
        static::createClient()->request('GET', '/api/reader/chapter/0');

        // Assert
        $this->assertResponseStatusCodeSame(404);
        $this->assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');

        $this->assertJsonContains([
            'hydra:title' => 'An error occurred',
            'hydra:description' => 'Le chapitre 0 n\'existe pas',
        ]);
    }
}
