<?php

declare(strict_types=1);

session_start();

require_once __DIR__ . '/../Core/Database.php';

if (isset($_SESSION['admin_id'])) {
    header('Location: dashboard.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username === '' || $password === '') {

        $error = 'Please enter username and password.';

    } else {

        try {

            $pdo = Database::connection();

            $stmt = $pdo->prepare("
                SELECT id, username, password, full_name
                FROM admin_users
                WHERE username = ?
                  AND status = 'ACTIVE'
                LIMIT 1
            ");

            $stmt->execute([$username]);

            $admin = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($admin && password_verify($password, $admin['password'])) {

                session_regenerate_id(true);

                $_SESSION['admin_id'] = (int)$admin['id'];
                $_SESSION['admin_username'] = $admin['username'];
                $_SESSION['admin_name'] = $admin['full_name'];

                header('Location: dashboard.php');
                exit;

            } else {

                $error = 'Invalid username or password.';

            }

        } catch (Throwable $e) {

            $error = 'Unable to connect to the database.';
        }
    }
}

?>

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

<head>

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

<title>Admin Login | SmartApply</title>

<style>

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: Arial, Helvetica, sans-serif;
    background: #f3f6fb;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.login-card {
    width: 100%;
    max-width: 420px;
    background: white;
    padding: 35px;
    border-radius: 18px;
    box-shadow: 0 15px 45px rgba(0,0,0,.10);
}

.logo {
    text-align: center;
    margin-bottom: 30px;
}

.logo h1 {
    margin: 0;
    color: #2563eb;
    font-size: 30px;
}

.logo p {
    margin-top: 8px;
    color: #777;
}

label {
    display: block;
    margin-bottom: 7px;
    color: #333;
    font-weight: 600;
}

input {
    width: 100%;
    padding: 13px 14px;
    border: 1px solid #d8dee8;
    border-radius: 9px;
    outline: none;
    font-size: 15px;
    margin-bottom: 18px;
}

input:focus {
    border-color: #2563eb;
}

button {
    width: 100%;
    border: none;
    padding: 14px;
    background: #2563eb;
    color: white;
    border-radius: 9px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
}

button:hover {
    background: #1d4ed8;
}

.error {
    background: #fee2e2;
    color: #b91c1c;
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 18px;
    font-size: 14px;
}

.footer {
    text-align: center;
    margin-top: 25px;
    color: #999;
    font-size: 13px;
}

</style>

</head>

<body>

<div class="login-card">

    <div class="logo">

        <h1>SmartApply</h1>

        <p>Administrator Login</p>

    </div>

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

        <div class="error">
            <?= htmlspecialchars($error) ?>
        </div>

    <?php endif; ?>

    <form method="POST">

        <label for="username">
            Username
        </label>

        <input
            type="text"
            id="username"
            name="username"
            autocomplete="username"
            required
        >

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

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

        <button type="submit">
            Login
        </button>

    </form>

    <div class="footer">
        SmartApply Administration
    </div>

</div>

</body>

</html>