<?php

namespace App\Domain\Manga\Application\CommandHandler;

use App\Domain\Manga\Application\Command\ToggleMangaMonitoring;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;

readonly class ToggleMangaMonitoringHandler
{
    public function __construct(
        private MangaRepositoryInterface $mangaRepository
    ) {
    }

    public function handle(ToggleMangaMonitoring $command): void
    {
        $manga = $this->mangaRepository->findById($command->mangaId->getValue());

        if (!$manga) {
            throw new MangaNotFoundException($command->mangaId->getValue());
        }

        if ($command->enabled) {
            $manga->enableMonitoring();
        } else {
            $manga->disableMonitoring();
        }

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