<?php declare(strict_types=1);

namespace App\Engine\Admin\Auth;

class LoginController extends \App\Engine\Admin\Inertia\BaseInertiaAdminController
{

    public function getId(): string
    {
        return "AdminLogin";
    }

    public function index(
        \Symfony\Component\Security\Http\Authentication\AuthenticationUtils $authenticationUtils,
        \Symfony\Contracts\Translation\TranslatorInterface                  $translator,
        \Symfony\Component\Routing\RouterInterface                          $router,
    ): \Symfony\Component\HttpFoundation\Response {
        $error = $authenticationUtils->getLastAuthenticationError();

        $form = null;
        if ($error) {
            $form = new \App\Engine\Inertia\ViewModels\Form\FormViewModel(
                id: "login-form",
                messages: [
                    new \App\Engine\Inertia\MessageViewModel(
                        message: $translator->trans("admin.login.form.error-invalid-username-or-password"),
                        type: \App\Engine\Inertia\MessageViewModel::ERROR_MESSAGE_TYPE,
                    )
                ],
            );
        }

        $props = new \App\Engine\Inertia\Admin\Props\LoginProps();
        $props->form = $form;

        $webauthnForm = new \App\Engine\Inertia\Admin\ViewModels\WebauthnFormViewModel();
        $webauthnForm->optionsUrl = $router->generate(BundleDefinedRouteIdentifiers::WEBAUTHN_LOGIN_OPTIONS);
        $webauthnForm->resultUrl = $router->generate(BundleDefinedRouteIdentifiers::WEBAUTHN_LOGIN);
        $webauthnForm->onSuccessRedirectUrl = $router->generate("AdminDashboard");
        $props->webauthnForm = $webauthnForm;

        return $this->inertia->render("Admin/AdminLogin", $this->getInertiaProps(props: $props));
    }

}
