<?php

namespace apexl\Io\modules\user\controllers;

use apexl\Io\includes\Controller;
use apexl\Io\modules\user\Actions\UserCreateAction;
use apexl\Io\modules\user\Request\RegisterRequest;
use Exception;
use Psr\Http\Message\ResponseInterface;

final readonly class RegisterController extends Controller
{
    /**
     * @throws Exception
     */
    public function __invoke(
        RegisterRequest $request,
        ResponseInterface $response,
        UserCreateAction $userCreateAction,
    ): ResponseInterface {
        $userCreateAction->create(
            $request->firstName(),
            $request->lastName(),
            $request->email(),
            $request->password(),
        );

        $this->output->addRedirect(
            'user.register.redirect',
            $request->redirect(),
        );

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