<?php

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Domain\Manga\Application\Command\DeleteManga;
use App\Domain\Manga\Application\CommandHandler\DeleteMangaHandler;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

readonly class DeleteMangaProcessor implements ProcessorInterface
{
    public function __construct(
        private DeleteMangaHandler $handler
    ) {
    }

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): int
    {
        if (!isset($uriVariables['id'])) {
            throw new \InvalidArgumentException('Manga ID is required');
        }

        try {
            $command = new DeleteManga($uriVariables['id']);
            $this->handler->handle($command);

            return Response::HTTP_NO_CONTENT;
        } catch (MangaNotFoundException $e) {
            throw new NotFoundHttpException('Manga not found');
        }
    }
}
