<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/auth.php';

// If already logged in, redirect to correct dashboard
if (isLoggedIn()) {
    $role = $_SESSION['user_role'] ?? '';
    if ($role === 'ADMIN') header('Location: ' . APP_URL . '/admin/dashboard.php');
    elseif ($role === 'ORGANIZER') header('Location: ' . APP_URL . '/organizer/dashboard.php');
    elseif ($role === 'FINANCE') header('Location: ' . APP_URL . '/finance/dashboard.php');
    else header('Location: ' . APP_URL . '/');
    exit;
}

$error = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_csrf();
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (empty($email) || empty($password)) {
        $error = 'Email and password are required.';
    } else {
        $result = loginUser($email, $password);
        if ($result['success']) {
            $role = $result['role'];
            if ($role === 'ADMIN') header('Location: ' . APP_URL . '/admin/dashboard.php');
            elseif ($role === 'ORGANIZER') header('Location: ' . APP_URL . '/organizer/dashboard.php');
            elseif ($role === 'FINANCE') header('Location: ' . APP_URL . '/finance/dashboard.php');
            else header('Location: ' . APP_URL . '/');
            exit;
        } else {
            $error = $result['message'];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Staff Portal - <?= APP_NAME ?></title>
    <link rel="stylesheet" href="<?= APP_URL ?>/assets/css/style.css">
    <script src="https://unpkg.com/lucide@latest"></script>
</head>
<body class="bg-slate-950 text-slate-200 antialiased selection:bg-cyan-500/30 selection:text-cyan-200 min-h-screen flex flex-col font-sans">
    <div class="min-h-screen flex items-center justify-center p-4 relative overflow-hidden bg-slate-950">
        <!-- Background ambient lighting -->
        <div class="absolute top-1/4 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[500px] h-[500px] bg-gradient-to-tr from-purple-900/40 to-cyan-500/20 rounded-full blur-[120px] pointer-events-none"></div>

        <div class="w-full max-w-md relative z-10">
            <a href="<?= APP_URL ?>/" class="inline-flex items-center gap-2 text-sm text-slate-400 hover:text-cyan-400 mb-6 transition-colors font-tech">
                <i data-lucide="arrow-left" class="w-4 h-4"></i> Back to Supernova 2.0
            </a>

            <div class="glass-panel p-8 rounded-2xl border border-white/10 shadow-2xl backdrop-blur-xl">
                <div class="flex justify-center mb-4">
                    <div class="p-3 rounded-full bg-cyan-500/10 border border-cyan-500/30 text-cyan-400">
                        <i data-lucide="shield-check" class="w-8 h-8"></i>
                    </div>
                </div>

                <h2 class="text-2xl font-bold text-center text-slate-100 mb-1 font-heading-cyber">
                    Supernova 2.0 Staff Portal
                </h2>
                <p class="text-center text-slate-400 text-sm mb-6 font-tech">
                    Authorized Organizer, Finance & Admin Login
                </p>

                <?php if ($error): ?>
                <div class="mb-6 p-3 rounded-lg bg-red-500/10 border border-red-500/30 text-red-400 text-sm flex items-center gap-2 font-mono-cyber">
                    <i data-lucide="alert-circle" class="w-4 h-4 shrink-0"></i>
                    <span><?= htmlspecialchars($error) ?></span>
                </div>
                <?php endif; ?>

                <form method="POST" action="login.php" class="space-y-4 font-tech">
                    <?= csrf_field() ?>
                    <div>
                        <label class="block text-xs font-semibold text-slate-300 uppercase mb-2 font-mono-cyber">
                            Official Email Address
                        </label>
                        <div class="relative">
                            <i data-lucide="mail" class="w-5 h-5 absolute left-3 top-1/2 -translate-y-1/2 text-slate-500"></i>
                            <input
                                type="email"
                                name="email"
                                required
                                placeholder="admin@supernova2026.com"
                                class="w-full pl-10 pr-4 py-3 rounded-xl bg-slate-900/80 border border-white/10 text-slate-100 placeholder-slate-600 focus:outline-none focus:border-cyan-400 focus:ring-1 focus:ring-cyan-400 transition-all text-sm"
                            />
                        </div>
                    </div>

                    <div>
                        <label class="block text-xs font-semibold text-slate-300 uppercase mb-2 font-mono-cyber">
                            Password
                        </label>
                        <div class="relative">
                            <i data-lucide="lock" class="w-5 h-5 absolute left-3 top-1/2 -translate-y-1/2 text-slate-500"></i>
                            <input
                                type="password"
                                name="password"
                                required
                                placeholder="••••••••••••"
                                class="w-full pl-10 pr-4 py-3 rounded-xl bg-slate-900/80 border border-white/10 text-slate-100 placeholder-slate-600 focus:outline-none focus:border-cyan-400 focus:ring-1 focus:ring-cyan-400 transition-all text-sm"
                            />
                        </div>
                    </div>

                    <button type="submit" class="w-full py-3.5 rounded-xl bg-gradient-to-r from-cyan-500 to-blue-600 hover:from-cyan-400 hover:to-blue-500 text-slate-950 font-bold transition-all shadow-glow flex items-center justify-center font-mono-cyber mt-2">
                        Sign In to Dashboard
                    </button>
                </form>

                <p class="text-center text-slate-500 text-xs mt-6 font-tech">
                    Note: Event Participants do not require account login to register.
                </p>
            </div>
        </div>
    </div>
    
    <script>
        lucide.createIcons();
    </script>
</body>
</html>
