<?php

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Domain\Manga\Application\Command\FetchMangaChapters;
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
use App\Domain\Manga\Infrastructure\ApiPlatform\Resource\FetchMangaChaptersResource;
use Symfony\Component\Messenger\MessageBusInterface;

readonly class FetchMangaChaptersProcessor implements ProcessorInterface
{
    public function __construct(
        private MessageBusInterface $messageBus
    ) {
    }

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
    {
        if (!$data instanceof FetchMangaChaptersResource) {
            throw new \InvalidArgumentException('Invalid resource type');
        }

        $this->messageBus->dispatch(
            new FetchMangaChapters(new MangaId($data->mangaId))
        );
    }
}
