<?php

declare(strict_types=1);

namespace App\SuperAdmin\Infrastructure\Api\Processor;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\SuperAdmin\Application\Command\SwitchTenant\SwitchTenantCommand;
use App\SuperAdmin\Application\Command\SwitchTenant\SwitchTenantHandler;
use App\SuperAdmin\Domain\Exception\EstablishmentIntrouvableException;
use App\SuperAdmin\Infrastructure\Api\Resource\SwitchTenantInput;
use Override;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * @implements ProcessorInterface<SwitchTenantInput, void>
 */
final readonly class SwitchTenantProcessor implements ProcessorInterface
{
    public function __construct(
        private SwitchTenantHandler $handler,
    ) {
    }

    /**
     * @param SwitchTenantInput $data
     */
    #[Override]
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
    {
        try {
            ($this->handler)(new SwitchTenantCommand(
                tenantId: $data->tenantId,
            ));
        } catch (EstablishmentIntrouvableException $e) {
            throw new NotFoundHttpException($e->getMessage(), $e);
        }
    }
}
