<?php

namespace App\Tests\Domain\Manga\Application\CommandHandler;

use App\Domain\Manga\Application\Command\ToggleMangaMonitoring;
use App\Domain\Manga\Application\CommandHandler\ToggleMangaMonitoringHandler;
use App\Domain\Manga\Domain\Model\Manga;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
use App\Domain\Manga\Domain\Model\ValueObject\MonitoringStatus;
use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
use PHPUnit\Framework\TestCase;

class ToggleMangaMonitoringHandlerTest extends TestCase
{
    private InMemoryMangaRepository $mangaRepository;
    private ToggleMangaMonitoringHandler $handler;

    protected function setUp(): void
    {
        $this->mangaRepository = new InMemoryMangaRepository();
        $this->handler = new ToggleMangaMonitoringHandler($this->mangaRepository);
    }

    public function testEnableMonitoringForManga(): void
    {
        // Arrange
        $mangaId = 'manga-123';
        $manga = new Manga(
            new MangaId($mangaId),
            new MangaTitle('Test Manga'),
            new MangaSlug('test-manga'),
            'Description',
            'Author',
            2024,
            [],
            'ongoing',
            new ExternalId('external-123')
        );

        $this->mangaRepository->save($manga);

        // Act
        $command = new ToggleMangaMonitoring(new MangaId($mangaId), true);
        $this->handler->handle($command);

        // Assert
        $updatedManga = $this->mangaRepository->findById($mangaId);
        $this->assertNotNull($updatedManga);
        $this->assertTrue($updatedManga->getMonitoringStatus()->isEnabled());
        $this->assertNotNull($updatedManga->getLastMonitoringCheck());
    }

    public function testDisableMonitoringForManga(): void
    {
        // Arrange
        $mangaId = 'manga-123';
        $manga = new Manga(
            new MangaId($mangaId),
            new MangaTitle('Test Manga'),
            new MangaSlug('test-manga'),
            'Description',
            'Author',
            2024,
            [],
            'ongoing',
            new ExternalId('external-123')
        );

        // Activer d'abord le monitoring
        $manga->enableMonitoring();
        $this->mangaRepository->save($manga);

        // Act
        $command = new ToggleMangaMonitoring(new MangaId($mangaId), false);
        $this->handler->handle($command);

        // Assert
        $updatedManga = $this->mangaRepository->findById($mangaId);
        $this->assertNotNull($updatedManga);
        $this->assertFalse($updatedManga->getMonitoringStatus()->isEnabled());
    }

    public function testToggleMonitoringWithNonExistingManga(): void
    {
        // Arrange
        $nonExistingMangaId = 'non-existing-manga';

        // Act & Assert
        $this->expectException(\App\Domain\Manga\Domain\Exception\MangaNotFoundException::class);
        $this->expectExceptionMessage($nonExistingMangaId);

        $command = new ToggleMangaMonitoring(new MangaId($nonExistingMangaId), true);
        $this->handler->handle($command);
    }

    public function testEnableMonitoringWhenAlreadyEnabled(): void
    {
        // Arrange
        $mangaId = 'manga-123';
        $manga = new Manga(
            new MangaId($mangaId),
            new MangaTitle('Test Manga'),
            new MangaSlug('test-manga'),
            'Description',
            'Author',
            2024,
            [],
            'ongoing',
            new ExternalId('external-123')
        );

        $manga->enableMonitoring();
        $firstActivationTime = $manga->getLastMonitoringCheck();
        $this->mangaRepository->save($manga);

        // Wait a bit to ensure time difference
        sleep(1);

        // Act
        $command = new ToggleMangaMonitoring(new MangaId($mangaId), true);
        $this->handler->handle($command);

        // Assert
        $updatedManga = $this->mangaRepository->findById($mangaId);
        $this->assertNotNull($updatedManga);
        $this->assertTrue($updatedManga->getMonitoringStatus()->isEnabled());

        // Le timestamp devrait être mis à jour même si déjà activé
        $this->assertGreaterThan(
            $firstActivationTime->getTimestamp(),
            $updatedManga->getLastMonitoringCheck()->getTimestamp()
        );
    }

    public function testDisableMonitoringWhenAlreadyDisabled(): void
    {
        // Arrange
        $mangaId = 'manga-123';
        $manga = new Manga(
            new MangaId($mangaId),
            new MangaTitle('Test Manga'),
            new MangaSlug('test-manga'),
            'Description',
            'Author',
            2024,
            [],
            'ongoing',
            new ExternalId('external-123')
        );

        $this->mangaRepository->save($manga);

        // Act
        $command = new ToggleMangaMonitoring(new MangaId($mangaId), false);
        $this->handler->handle($command);

        // Assert
        $updatedManga = $this->mangaRepository->findById($mangaId);
        $this->assertNotNull($updatedManga);
        $this->assertFalse($updatedManga->getMonitoringStatus()->isEnabled());
        $this->assertNull($updatedManga->getLastMonitoringCheck());
    }
}
