<?php
session_start();
if (!empty($_SESSION['tgbot_admin_logged_in'])) {
    header('Location: index.php');
    exit;
}

$config = include __DIR__ . '/../config.php';
$error  = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pass      = $_POST['password'] ?? '';
    $adminPass = $config['admin_password'] ?? '';

    if ($adminPass === '') {
        $error = 'Admin password not configured. Add ADMIN_PASSWORD to your .env file.';
    } elseif (hash_equals($adminPass, $pass)) {
        session_regenerate_id(true);
        $_SESSION['tgbot_admin_logged_in'] = true;
        header('Location: index.php');
        exit;
    } else {
        $error = 'Incorrect password. Please try again.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login — Fit Bot</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
    <style>
        body { background: linear-gradient(135deg, #1a1d2e 0%, #2d3253 100%); min-height: 100vh; }
    </style>
</head>
<body class="d-flex align-items-center justify-content-center">
<div class="w-100 px-3" style="max-width:400px">
    <div class="card border-0 shadow-lg" style="border-radius:16px">
        <div class="card-body p-4 p-md-5">
            <div class="text-center mb-4">
                <div style="font-size:2.5rem">🤖</div>
                <h4 class="fw-bold mb-1 mt-2">Fit Bot Admin</h4>
                <p class="text-muted small mb-0">Authorised access only</p>
            </div>
            <?php if ($error): ?>
            <div class="alert alert-danger py-2 small">⚠️ <?= htmlspecialchars($error) ?></div>
            <?php endif; ?>
            <form method="POST" autocomplete="off">
                <div class="mb-3">
                    <label class="form-label fw-semibold small">Admin Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Enter password" autofocus required>
                </div>
                <button class="btn btn-primary w-100 fw-semibold" type="submit">Sign In →</button>
            </form>
        </div>
    </div>
    <p class="text-center text-white-50 small mt-3">TG Fit Bot &mdash; Admin Access Only</p>
</div>
</body>
</html>
