<?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 GetChapterPagesTest extends AbstractApiTestCase
{
    use ResetDatabase;

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

        $chapter = ChapterFactory::createOne([
            'manga' => $manga,
            'title' => 'Chapter 1',
            'number' => 1,
            'cbzPath' => __DIR__ . '/../../Fixtures/chapter.cbz',
        ]);

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

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

        $response = static::createClient()->request('GET', '/api/reader/chapter/' . $chapter->getId() . '/pages')->toArray();
        

        $this->assertArrayHasKey('pages', $response);
        $this->assertCount(1, $response['pages']);
        $this->assertEquals(1, $response['pages'][0]['pageNumber']);
        $this->assertArrayHasKey('dimensions', $response['pages'][0]);
        $this->assertEquals(1, $response['totalItems']);
        $this->assertEquals(1, $response['currentPage']);
        $this->assertEquals(20, $response['itemsPerPage']);
    }

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

        $chapter = ChapterFactory::createOne([
            'manga' => $manga,
            'title' => 'Chapter with multiple pages',
            'number' => 1,
            'cbzPath' => __DIR__ . '/../../Fixtures/chapter-multiple.cbz',
        ]);

        // Act
        $response = static::createClient()->request('GET', '/api/reader/chapter/' . $chapter->getId() . '/pages', [
            'query' => [
                'page' => 2,
                'itemsPerPage' => 1
            ]
        ])->toArray();

        // Assert
        $this->assertResponseIsSuccessful();
        $this->assertCount(1, $response['pages']);
        $this->assertEquals(2, $response['pages'][0]['pageNumber']);
        $this->assertEquals(2, $response['totalItems']);
        $this->assertEquals(2, $response['currentPage']);
        $this->assertEquals(1, $response['itemsPerPage']);
    }

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

        // 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',
        ]);
    }

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

        $chapter = ChapterFactory::createOne([
            'manga' => $manga,
            'title' => 'Chapter without CBZ',
            'number' => 1,
            'cbzPath' => null,
        ]);

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

        // Assert
        $this->assertResponseIsSuccessful();
        $this->assertJsonContains([
            'pages' => [],
            'totalItems' => 0,
            'currentPage' => 1,
            'itemsPerPage' => 20
        ]);
    }
} 