<?php

namespace App\Domain\Manga\Application\CommandHandler;

use App\Domain\Manga\Application\Command\DeleteManga;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use App\Domain\Shared\Domain\Contract\CommandHandlerInterface;
use App\Domain\Shared\Domain\Contract\CommandInterface;

readonly class DeleteMangaHandler implements CommandHandlerInterface
{
    public function __construct(
        private MangaRepositoryInterface $mangaRepository
    ) {
    }

    public function handle(CommandInterface $command): void
    {
        assert($command instanceof DeleteManga);

        $manga = $this->mangaRepository->findById($command->mangaId);

        if (!$manga) {
            throw new MangaNotFoundException(sprintf('Manga with id %s not found', $command->mangaId));
        }

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