<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\controllers;

use apexl\Io\exceptions\RouteNotFoundException;
use apexl\Io\includes\Controller;
use apexl\Io\includes\RouteManager;
use apexl\Io\modules\user\Actions\EndMasqueradeAction;
use apexl\Io\modules\user\exceptions\MasqueradeException;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final readonly class EndMasqueradeController extends Controller
{
    /**
     * @throws JsonException
     * @throws RouteNotFoundException
     * @throws MasqueradeException
     */
    public function __invoke(
        EndMasqueradeAction $masqueradeAction,
        ServerRequestInterface $request,
        ResponseInterface $response,
        RouteManager $routeManager,
    ): ResponseInterface {
        $masqueradeAction->handle();

        $this->output->addRedirect('redirect.masquerade.end', $routeManager->getRoutePattern('home'));

        return $this->json($response);
    }
}