<?php
// Strict Session Enforcement
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// If already logged in, push them straight to the dashboard
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    header("Location: dashboard.php");
    exit();
}

require_once __DIR__ . '/../config/config.php';

// ---------------------------------------------------------
// DYNAMIC LOGO CONFIGURATION
// ---------------------------------------------------------
$dynamic_logo_url = ''; // e.g., SITE_URL . '/assets/images/custom-logo.png'

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];

    try {
        $stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?");
        $stmt->execute([$username]);
        $admin = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$admin) {
            $error = "Invalid administrator credentials.";
        } else {
            if (password_verify($password, $admin['password'])) {
                session_regenerate_id(true);
                $_SESSION['admin_logged_in'] = true;
                $_SESSION['admin_user']      = $admin['username'];
                $_SESSION['admin_id']        = $admin['id'];
                header("Location: dashboard.php");
                exit();
            } else {
                $error = "Invalid administrator credentials.";
            }
        }
    } catch (PDOException $e) {
        $error = "Database Error: " . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login - Football Pred Pro</title>
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/css/output.css?v=<?php echo time(); ?>">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            background: #020617;
            background-image: radial-gradient(circle at 10% 20%, rgba(16, 185, 129, 0.05) 0%, transparent 40%),
                              radial-gradient(circle at 90% 80%, rgba(59, 130, 246, 0.05) 0%, transparent 40%);
        }
        ::-webkit-scrollbar { width: 6px; }
        ::-webkit-scrollbar-track { background: transparent; }
        ::-webkit-scrollbar-thumb { background: #1e293b; border-radius: 4px; }
        .glass-card {
            background: rgba(15, 23, 42, 0.7);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(30, 41, 59, 0.6);
        }
        .input-dark {
            background: rgba(2, 6, 23, 0.8);
            border: 1px solid rgba(30, 41, 59, 0.5);
            transition: all 0.2s ease;
        }
        .input-dark:focus {
            border-color: #10b981;
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
        }
        .btn-emerald {
            background: linear-gradient(135deg, #10b981, #059669);
            transition: all 0.3s ease;
        }
        .btn-emerald:hover {
            transform: translateY(-1px);
            box-shadow: 0 0 30px rgba(16, 185, 129, 0.3);
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center p-4">

    <div class="w-full max-w-md glass-card rounded-2xl p-6 sm:p-8 shadow-2xl space-y-6 relative overflow-hidden">
        <!-- Subtle glow -->
        <div class="absolute -top-20 -right-20 w-64 h-64 bg-emerald-500/5 rounded-full blur-[80px] pointer-events-none"></div>
        <div class="absolute -bottom-20 -left-20 w-64 h-64 bg-cyan-500/5 rounded-full blur-[80px] pointer-events-none"></div>

        <div class="text-center space-y-3 relative z-10">
            
            <?php if (!empty($dynamic_logo_url)): ?>
                <img src="<?php echo htmlspecialchars($dynamic_logo_url); ?>" alt="Platform Logo" class="h-16 mx-auto object-contain drop-shadow-lg">
            <?php else: ?>
                <div class="w-14 h-14 bg-gradient-to-br from-emerald-500 to-teal-600 rounded-xl flex items-center justify-center text-2xl mx-auto shadow-lg shadow-emerald-500/20">
                    🛡️
                </div>
            <?php endif; ?>

            <h2 class="text-2xl font-black text-white tracking-tight">Admin Login</h2>
            <p class="text-xs text-slate-400">Secure gateway to the control panel</p>
        </div>

        <?php if (!empty($error)): ?>
            <div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-3 rounded-xl text-xs font-bold text-center flex items-center justify-center gap-2">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form action="login.php" method="POST" class="space-y-4 relative z-10">
            <div>
                <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5">Username</label>
                <input type="text" name="username" required 
                       class="w-full input-dark rounded-xl px-4 py-3 text-white placeholder-slate-600 focus:outline-none text-sm" 
                       placeholder="Enter your username">
            </div>
            <div>
                <label class="block text-[10px] font-bold text-slate-400 uppercase tracking-wider mb-1.5">Password</label>
                <input type="password" name="password" required 
                       class="w-full input-dark rounded-xl px-4 py-3 text-white placeholder-slate-600 focus:outline-none text-sm" 
                       placeholder="••••••••">
            </div>
            <button type="submit" class="w-full btn-emerald text-white font-black py-3.5 rounded-xl text-xs uppercase tracking-[0.15em] transition shadow-lg">
                Authenticate
            </button>
        </form>

        <div class="text-center text-[9px] text-slate-500 pt-2 border-t border-slate-800/40 relative z-10">
            Restricted area • All access is logged
        </div>
    </div>

</body>
</html>