<?php

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Domain\Setting\Application\Command\DeleteContentSourceCommand;
use App\Domain\Setting\Application\CommandHandler\DeleteContentSourceCommandHandler;
use App\Domain\Setting\Domain\Exception\ContentSourceNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

readonly class DeleteContentSourceStateProcessor implements ProcessorInterface
{
    public function __construct(
        private DeleteContentSourceCommandHandler $handler
    ) {
    }

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

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

            return Response::HTTP_NO_CONTENT;
        } catch (ContentSourceNotFoundException $e) {
            throw new NotFoundHttpException($e->getMessage());
        }
    }
}
