<?php

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Domain\Manga\Application\Command\DeleteChapter;
use App\Domain\Manga\Application\CommandHandler\DeleteChapterHandler;
use App\Domain\Manga\Domain\Exception\ChapterNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

readonly class DeleteChapterProcessor implements ProcessorInterface
{
    public function __construct(
        private DeleteChapterHandler $handler
    ) {
    }

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

        try {
            $command = new DeleteChapter($uriVariables['id']);
            $this->handler->handle($command);
        } catch (ChapterNotFoundException $e) {
            throw new NotFoundHttpException('Chapter not found');
        }
    }
}
