<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/core/bootstrap.php';

HyroAdminAuth::startSession();

if (HyroAdminAuth::check()) {
    hyro_redirect(
        '/experience/admin/dashboard.php'
    );
}

$error = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!HyroCsrf::verify($_POST['_token'] ?? null)) {
        $error =
            'Your session expired. Please refresh and try again.';
    } else {
        $email = trim(
            (string) ($_POST['email'] ?? '')
        );

        $password = (string) ($_POST['password'] ?? '');

        try {
            $loggedIn = HyroAdminAuth::attempt(
                HyroDatabase::connection(),
                $email,
                $password
            );

            if ($loggedIn) {
                hyro_redirect(
                    '/experience/admin/dashboard.php'
                );
            }

            $error =
                'Invalid login details or account temporarily locked.';
        } catch (Throwable $exception) {
            hyro_log(
                'admin-auth',
                'Admin login failed with exception.',
                [
                    'error' => $exception->getMessage(),
                ]
            );

            $error =
                'Unable to complete login. Please try again.';
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <meta
        name="robots"
        content="noindex, nofollow, noarchive"
    >

    <title>HYRO ONE Admin Login</title>

    <link
        rel="icon"
        href="/experience/assets/icons/favicon.svg"
        type="image/svg+xml"
    >

    <link
        rel="stylesheet"
        href="/experience/assets/css/site.css?v=<?= time() ?>"
    >

    <link
        rel="stylesheet"
        href="/experience/assets/css/admin.css?v=<?= time() ?>"
    >

</head>

<body>

<main class="hyro-admin-login-page">

    <section class="hyro-admin-login-card">

        <div class="hyro-admin-login-brand">

            <strong>HYRO ONE</strong>

            <span>ADMIN PORTAL</span>

        </div>

        <h1>Welcome back</h1>

        <p>
            Sign in to manage leads and HYRO ONE operations.
        </p>

        <?php if ($error !== null): ?>

            <div class="hyro-admin-login-error">
                <?= hyro_escape($error) ?>
            </div>

        <?php endif; ?>

        <form method="post" action="">

            <?= HyroCsrf::input() ?>

            <div class="hyro-admin-field">

                <label for="email">
                    Email Address
                </label>

                <input
                    type="email"
                    id="email"
                    name="email"
                    autocomplete="username"
                    required
                    autofocus
                >

            </div>

            <div class="hyro-admin-field">

                <label for="password">
                    Password
                </label>

                <input
                    type="password"
                    id="password"
                    name="password"
                    autocomplete="current-password"
                    required
                >

            </div>

            <div class="hyro-admin-login-actions">

                <button
                    type="submit"
                    class="hyro-button hyro-button-primary"
                >
                    Sign In →
                </button>

            </div>

        </form>

        <div class="hyro-admin-login-footer">

            <a href="/experience/">
                Return to HYRO ONE Website
            </a>

        </div>

    </section>

</main>

</body>
</html>
