<?php

namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Processor;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Domain\Manga\Application\Command\CreateMangaFromMangadex;
use App\Domain\Manga\Application\CommandHandler\CreateMangaFromMangadexHandler;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

readonly class CreateMangaProcessor implements ProcessorInterface
{
    public function __construct(
        private CreateMangaFromMangadexHandler $handler
    ) {
    }

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
    {
        try {
            $this->handler->handle(new CreateMangaFromMangadex($data->externalId));
        } catch (MangaNotFoundException $e) {
            throw new NotFoundHttpException($e->getMessage());
        }
    }
}
