<?php

declare(strict_types=1);

namespace App\Tests\Feature\Setting;

use App\Entity\ContentSource;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\ResetDatabase;

final class GetContentSourceTest extends AbstractApiTestCase
{
    use ResetDatabase;

    private int $sourceId;

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

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

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

        $this->sourceId = $source->getId();
    }

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

        $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
        $this->assertJsonContains([
            'detail' => 'ContentSource with id 999999 not found'
        ]);
    }

    public function testItReturnsSourceSuccessfully(): void
    {
        $response = static::createClient()->request('GET', "/api/content-sources/{$this->sourceId}");

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

        $this->assertArrayHasKey('id', $data);
        $this->assertEquals($this->sourceId, $data['id']);
        $this->assertArrayHasKey('baseUrl', $data);
        $this->assertEquals('https://mangadex.org', $data['baseUrl']);
        $this->assertArrayHasKey('chapterUrlFormat', $data);
        $this->assertEquals('https://mangadex.org/chapter/{id}', $data['chapterUrlFormat']);
        $this->assertArrayHasKey('scrapingType', $data);
        $this->assertEquals('html', $data['scrapingType']);
        $this->assertArrayHasKey('imageSelector', $data);
        $this->assertEquals('.chapter-image img', $data['imageSelector']);
        $this->assertArrayHasKey('nextPageSelector', $data);
        $this->assertEquals('.next-page', $data['nextPageSelector']);
        $this->assertArrayHasKey('chapterSelector', $data);
        $this->assertEquals('.chapter-list a', $data['chapterSelector']);
        $this->assertArrayHasKey('cleanBaseUrl', $data);
        $this->assertEquals('mangadex.org', $data['cleanBaseUrl']);
    }

    public function testItReturnsSourceWithJavascriptScrapingType(): void
    {
        // Créer une source avec le type javascript
        $source = new ContentSource();
        $source->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($source);
        $this->entityManager->flush();

        $response = static::createClient()->request('GET', "/api/content-sources/{$source->getId()}");

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

        $this->assertEquals('javascript', $data['scrapingType']);
        $this->assertEquals('https://mangakakalot.com', $data['baseUrl']);
        $this->assertEquals('mangakakalot.com', $data['cleanBaseUrl']);
    }

    public function testItReturnsSourceWithNullOptionalFields(): void
    {
        // Créer une source sans les champs optionnels
        $source = new ContentSource();
        $source->setBaseUrl('https://simple-source.com')
            ->setChapterUrlFormat('https://simple-source.com/chapter/{id}')
            ->setScrapingType('html');

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

        $response = static::createClient()->request('GET', "/api/content-sources/{$source->getId()}");

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

        // Les champs optionnels peuvent ne pas être présents dans la réponse
        if (array_key_exists('imageSelector', $data)) {
            $this->assertNull($data['imageSelector']);
        }
        if (array_key_exists('nextPageSelector', $data)) {
            $this->assertNull($data['nextPageSelector']);
        }
        if (array_key_exists('chapterSelector', $data)) {
            $this->assertNull($data['chapterSelector']);
        }
    }

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

        $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
    }
}
