<?php

declare(strict_types=1);

namespace apexl\Io\modules\userDisplay\controllers;

use apexl\Io\includes\RouteManager;
use apexl\Io\modules\display\components\BasicLink;
use apexl\Io\modules\display\Controllers\DisplayController;
use apexl\Io\modules\userDisplay\FormBuilders\RegisterFormBuilder;
use apexl\Io\services\VariableManager;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final readonly class RegisterDisplayController extends DisplayController
{
    /**
     * @throws JsonException
     */
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        RegisterFormBuilder $formBuilder,
        RouteManager $routeManager,
        VariableManager $variableManager
    ): ResponseInterface {
        $this->render->setActive("noUI");
        $this->render->setPageTitle("Register");

        $this->output->addResponse($request, $this->render->build([
            $formBuilder->build(),

            $this->component(BasicLink::class)
                ->addClass('forgottenPasswordLink return mt-5 text-decoration-underline')
                ->addRoute($routeManager->getRoutePattern('user.display.login'))
                ->addText($variableManager->get('user_login_return_login_text') ?? 'Return to login'),
        ]));

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


