<?php

declare(strict_types=1);

namespace App\Tests\Feature\Setting;

use App\Entity\ContentSource;
use App\Tests\Feature\AbstractApiTestCase;
use Zenstruck\Foundry\Test\ResetDatabase;

final class ListContentSourceTest extends AbstractApiTestCase
{
    use ResetDatabase;

    protected function setUp(): void
    {
        parent::setUp();
    }

    public function testItReturnsEmptyListWhenNoSourcesExist(): void
    {
        $response = static::createClient()->request('GET', '/api/content-sources');

        $this->assertResponseIsSuccessful();
        $data = $response->toArray();

        $this->assertArrayHasKey('hydra:member', $data);
        $this->assertCount(0, $data['hydra:member']);
        $this->assertArrayHasKey('hydra:totalItems', $data);
        $this->assertEquals(0, $data['hydra:totalItems']);
    }

    public function testItReturnsAllSources(): void
    {
        // Création de sources de contenu
        $source1 = new ContentSource();
        $source1->setBaseUrl('https://mangadex.org')
            ->setChapterUrlFormat('https://mangadex.org/chapter/{id}')
            ->setScrapingType('html')
            ->setImageSelector('.chapter-image img')
            ->setNextPageSelector('.next-page')
            ->setChapterSelector('.chapter-list a');

        $source2 = new ContentSource();
        $source2->setBaseUrl('https://mangakakalot.com')
            ->setChapterUrlFormat('https://mangakakalot.com/chapter/{id}')
            ->setScrapingType('javascript')
            ->setImageSelector('.page-image img')
            ->setNextPageSelector('.next-button')
            ->setChapterSelector('.chapter-link');

        $this->entityManager->persist($source1);
        $this->entityManager->persist($source2);
        $this->entityManager->flush();

        $response = static::createClient()->request('GET', '/api/content-sources');

        $this->assertResponseIsSuccessful();
        $data = $response->toArray();

        $this->assertArrayHasKey('hydra:member', $data);
        $this->assertCount(2, $data['hydra:member']);
        $this->assertArrayHasKey('hydra:totalItems', $data);
        $this->assertEquals(2, $data['hydra:totalItems']);

        // Vérifier la structure d'une source
        $firstSource = $data['hydra:member'][0];
        $this->assertArrayHasKey('id', $firstSource);
        $this->assertArrayHasKey('baseUrl', $firstSource);
        $this->assertArrayHasKey('chapterUrlFormat', $firstSource);
        $this->assertArrayHasKey('scrapingType', $firstSource);
        $this->assertArrayHasKey('imageSelector', $firstSource);
        $this->assertArrayHasKey('nextPageSelector', $firstSource);
        $this->assertArrayHasKey('chapterSelector', $firstSource);
        $this->assertArrayHasKey('cleanBaseUrl', $firstSource);

        // Vérifier que les URLs sont bien présentes
        $baseUrls = array_column($data['hydra:member'], 'baseUrl');
        $this->assertContains('https://mangadex.org', $baseUrls);
        $this->assertContains('https://mangakakalot.com', $baseUrls);
    }

    public function testItReturnsSourcesWithPagination(): void
    {
        // Création de plusieurs sources
        for ($i = 1; $i <= 25; $i++) {
            $source = new ContentSource();
            $source->setBaseUrl("https://source{$i}.com")
                ->setChapterUrlFormat("https://source{$i}.com/chapter/{id}")
                ->setScrapingType('html')
                ->setImageSelector('.image img')
                ->setNextPageSelector('.next')
                ->setChapterSelector('.chapter a');

            $this->entityManager->persist($source);
        }
        $this->entityManager->flush();

        $response = static::createClient()->request('GET', '/api/content-sources', [
            'query' => [
                'page' => 2,
                'itemsPerPage' => 10
            ]
        ]);

        $this->assertResponseIsSuccessful();
        $data = $response->toArray();

        $this->assertArrayHasKey('hydra:member', $data);
        $this->assertArrayHasKey('hydra:totalItems', $data);
        $this->assertEquals(25, $data['hydra:totalItems']);

        // Vérifier la pagination - l'endpoint peut retourner toutes les sources
        // même avec des paramètres de pagination
        $this->assertGreaterThanOrEqual(10, count($data['hydra:member']));
        $this->assertLessThanOrEqual(25, count($data['hydra:member']));
    }
}
