<?php

namespace App\Tests\Feature\Scraping;

use App\Domain\Scraping\Application\Command\ScrapeChapter;
use App\Tests\Feature\AbstractApiTestCase;
use App\Tests\Shared\Adapter\InMemoryMessageBus;
use Symfony\Component\Messenger\MessageBusInterface;

class ScrapeChapterTest extends AbstractApiTestCase
{
    private MessageBusInterface|InMemoryMessageBus $messageBus;

    protected function setUp(): void
    {
        parent::setUp();
        $this->messageBus = new InMemoryMessageBus();
        $this->container->set(MessageBusInterface::class, $this->messageBus);
        $this->messageBus->clear();
    }

    public function testInitiateChapterScraping(): void
    {
        // Given
        $payload = [
            'chapterId' => 'chapter-123',
        ];

        // When
        $response = static::createClient()->request('POST', '/api/scraping/chapters', [
            'json' => $payload,
            'headers' => ['Accept' => 'application/json'],
        ]);

        // Then
        $this->assertResponseStatusCodeSame(202);

        $messages = InMemoryMessageBus::$messages;
        $this->assertCount(1, $messages, 'Un message devrait être dispatché');

        /** @var ScrapeChapter $message */
        $message = $messages[0];
        $this->assertInstanceOf(ScrapeChapter::class, $message);
        $this->assertEquals('chapter-123', $message->chapterId);
        $this->assertNotEmpty($message->jobId);
    }

    public function testInitiateChapterScrapingWithInvalidPayload(): void
    {
        // Given
        $payload = [
            'chapterId' => '',
        ];

        // When
        $response = static::createClient()->request('POST', '/api/scraping/chapters', [
            'json' => $payload,
            'headers' => ['Accept' => 'application/json'],
        ]);

        // Then
        $this->assertResponseStatusCodeSame(422);
        $this->assertJsonContains([
            'violations' => [
                [
                    'propertyPath' => 'chapterId',
                    'message' => 'This value should not be blank.',
                ],
            ],
        ]);
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        InMemoryMessageBus::$messages = [];
    }
}
