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

    private int $sourceId;

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

        $source = new ContentSource();
        $source->setBaseUrl('https://mangadex.org')
            ->setChapterUrlFormat('https://mangadex.org/chapter/{id}')
            ->setScrapingType('html');

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

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

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

        $this->assertResponseStatusCodeSame(Response::HTTP_NO_CONTENT);

        $this->entityManager->clear();
        $deletedSource = $this->entityManager->find(ContentSource::class, $this->sourceId);
        $this->assertNull($deletedSource);
    }

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

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