<?php
/**
 * Slate — admin login.
 */
require_once dirname(__DIR__) . '/config.php';

// If already logged in, send straight to dashboard
if (Auth::check()) {
    header('Location: ' . SLATE_URL . '/admin/');
    exit;
}

$error     = '';
$installed = isset($_GET['installed']);
$next      = $_GET['next'] ?? (SLATE_URL . '/admin/');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!csrf_verify()) {
        $error = __('csrf_failed', 'Security check failed. Please try again.');
    } else {
        $email    = trim($_POST['email'] ?? '');
        $password = (string)($_POST['password'] ?? '');

        if ($email === '' || $password === '') {
            $error = __('login_required', 'Email and password are required.');
        } elseif (!Auth::attemptLogin($email, $password)) {
            if (Auth::lastLoginWasThrottled()) {
                $error = __('login_throttled', 'Too many login attempts. Please wait a few minutes and try again.');
                AuditLog::record('login.throttled', $email);
            } else {
                $error = __('login_invalid', 'Invalid email or password.');
                AuditLog::record('login.failed', $email);
            }
        } else {
            AuditLog::record('login.success', Auth::user()['email']);
            $target = slate_safe_redirect_target($next, SLATE_URL . '/admin/');
            header('Location: ' . $target);
            exit;
        }
    }
}

$siteName = Database::setting('site_name') ?: 'Slate';
?>
<!DOCTYPE html>
<html lang="<?= e(I18n::currentLocale()) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
    <meta name="theme-color" content="#FAFAFA">
    <title><?= __('login', 'Log in') ?> — <?= e($siteName) ?></title>
    <?php slate_ui_emit_css(); ?>
    <?php require SLATE_ROOT . '/includes/a11y_head.php'; ?>
    <style>
    body {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
        min-height: 100dvh;
        padding: var(--space-4);
    }
    .login-wrap {
        width: 100%;
        max-width: 380px;
    }
    .login-brand {
        text-align: center;
        margin-bottom: var(--space-6);
    }
    .login-brand-mark {
        width: 56px; height: 56px;
        margin: 0 auto var(--space-3);
        background: var(--accent);
        border-radius: 14px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        color: var(--on-accent);
        font-size: 24px;
        font-weight: 700;
        box-shadow: var(--shadow-md);
        letter-spacing: -0.02em;
    }
    .login-title {
        font-size: 22px;
        font-weight: 600;
        letter-spacing: -0.02em;
        margin: 0;
    }
    .login-sub {
        color: var(--muted);
        margin: 4px 0 0;
        font-size: 14px;
    }
    .login-card {
        background: var(--card);
        border-radius: var(--radius-lg);
        padding: var(--space-6);
        box-shadow: var(--shadow);
    }
    @media (max-width: 480px) {
        .login-card {
            padding: var(--space-5) var(--space-4);
        }
    }
    </style>
</head>
<body>
<div class="login-wrap">
    <div class="login-brand">
        <div class="login-brand-mark" aria-hidden="true">
            <?= e(mb_strtoupper(mb_substr($siteName, 0, 1))) ?>
        </div>
        <h1 class="login-title"><?= e($siteName) ?></h1>
        <p class="login-sub"><?= __('admin_login', 'Admin login') ?></p>
    </div>

    <?php if ($installed): ?>
        <div class="alert alert-success" role="status">
            <?= __('install_success', 'Slate is installed. Log in with the account you just created.') ?>
        </div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-error" role="alert"><?= e($error) ?></div>
    <?php endif; ?>

    <form method="post" class="login-card" novalidate>
        <?= csrf_field() ?>

        <div class="field">
            <label class="field-label" for="email"><?= __('email', 'Email') ?></label>
            <input type="email" id="email" name="email" required autofocus
                   value="<?= e($_POST['email'] ?? '') ?>"
                   autocomplete="username" inputmode="email">
        </div>

        <div class="field">
            <label class="field-label" for="password"><?= __('password', 'Password') ?></label>
            <input type="password" id="password" name="password" required
                   autocomplete="current-password">
        </div>

        <button type="submit" class="btn btn-primary btn-lg btn-block mt-2">
            <?= __('login', 'Log in') ?>
        </button>
    </form>
</div>
</body>
</html>
