<?php

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Domain\Manga\Application\Command\CreateManga;
use App\Domain\Manga\Application\CommandHandler\CreateMangaHandler;
use App\Domain\Manga\Infrastructure\ApiPlatform\Resource\CreateMangaDirectlyResource;

readonly class CreateMangaDirectlyProcessor implements ProcessorInterface
{
    public function __construct(
        private CreateMangaHandler $handler
    ) {
    }

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

        $command = new CreateManga(
            title: $data->title,
            slug: $data->slug,
            description: $data->description,
            author: $data->author,
            publicationYear: $data->publicationYear,
            genres: $data->genres,
            status: $data->status,
            externalId: $data->externalId,
            imageUrl: $data->imageUrl,
            rating: $data->rating
        );

        $this->handler->handle($command);
    }
}
