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

$pdo = getDatabaseConnection();
$eventId = $_GET['eventId'] ?? '';
$title = $_GET['title'] ?? 'Supernova Competition';

$error = null;
$success = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_csrf();
    $registrationId = strtoupper(trim($_POST['registrationId'] ?? ''));
    $fullName = trim($_POST['fullName'] ?? '');
    $email = strtolower(trim($_POST['email'] ?? ''));

    if (!$registrationId || !$fullName || !$email) {
        $error = "Missing required fields for venue check-in.";
    } else {
        // Verify registration AND that they registered for THIS specific event
        $stmt = $pdo->prepare("SELECT id FROM registrations WHERE registrationId = ? AND email = ? AND eventId = ?");
        $stmt->execute([$registrationId, $email, $eventId]);
        $reg = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$reg) {
            $error = "Invalid registration. Either the ID/Email is wrong, or you did not register for this specific event.";
        } else {
            // Check if already checked in
            $checkStmt = $pdo->prepare("SELECT id FROM attendance WHERE registrationId = ? AND eventId = ?");
            $checkStmt->execute([$reg['id'], $eventId]);
            
            if ($checkStmt->fetch()) {
                $error = "You have already marked your attendance for this event.";
            } else {
                // Mark attendance
                $insert = $pdo->prepare("INSERT INTO attendance (id, registrationId, eventId) VALUES (UUID(), ?, ?)");
                $insert->execute([$reg['id'], $eventId]);
                
                $success = [
                    'registrationId' => $registrationId,
                    'email' => $email,
                    'fullName' => $fullName,
                    'competition' => $title
                ];
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Venue Check-In - <?= 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">
    <div class="min-h-screen bg-cyber-grid py-10 px-4 flex items-center justify-center relative overflow-hidden font-tech">
        <div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[550px] h-[550px] bg-gradient-to-tr from-cyan-500/20 via-purple-600/20 to-emerald-500/20 rounded-full blur-[140px] pointer-events-none"></div>

        <div class="max-w-lg w-full relative z-10">
            <a href="<?= APP_URL ?>/" class="inline-flex items-center gap-2 text-xs font-mono-cyber text-slate-400 hover:text-cyan-400 mb-6 transition-colors">
                <i data-lucide="arrow-left" class="w-4 h-4"></i> [BACK TO HOME]
            </a>

            <?php if ($success): ?>
            <div class="glass-panel p-8 rounded-3xl border border-emerald-500/40 text-center shadow-glow-cyan">
                <div class="w-16 h-16 rounded-full bg-emerald-500/10 border border-emerald-500/30 text-emerald-400 flex items-center justify-center mx-auto mb-4">
                    <i data-lucide="check-circle-2" class="w-10 h-10"></i>
                </div>

                <span class="px-3.5 py-1 rounded-full bg-emerald-500/10 text-emerald-400 text-xs font-mono-cyber font-bold uppercase tracking-wider">
                    OFFICIAL VENUE CHECK-IN VERIFIED
                </span>

                <h2 class="text-3xl font-heading-cyber font-bold text-slate-100 mt-3 mb-1">
                    WELCOME, <?= strtoupper(htmlspecialchars($success['fullName'])) ?>!
                </h2>
                <p class="text-slate-300 text-sm mb-6">
                    Confirmed Present for competition: <span class="text-cyan-400 font-bold"><?= htmlspecialchars($success['competition']) ?></span>.
                </p>

                <div class="p-5 rounded-2xl bg-slate-900 border border-cyan-500/30 text-xs text-left space-y-2.5 mb-6 font-mono-cyber">
                    <div class="flex justify-between">
                        <span class="text-slate-400">REGISTRATION ID:</span>
                        <span class="font-bold text-cyan-400"><?= htmlspecialchars($success['registrationId']) ?></span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-slate-400">REGISTERED EMAIL:</span>
                        <span class="text-slate-200"><?= htmlspecialchars($success['email']) ?></span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-slate-400">VENUE STATUS:</span>
                        <span class="text-emerald-400 font-bold">PRESENT ✔</span>
                    </div>
                </div>

                <div class="p-4 rounded-xl bg-cyan-500/10 border border-cyan-500/30 text-cyan-300 text-xs flex items-center gap-3 text-left">
                    <i data-lucide="award" class="w-8 h-8 text-amber-400 shrink-0"></i>
                    <div>
                        <strong class="block text-white font-mono-cyber text-xs uppercase">ATTENDANCE VERIFIED & CONFIRMED ✔</strong>
                        Your attendance has been recorded successfully. You can download your official PDF Certificate anytime from the Certificate Portal.
                    </div>
                </div>
            </div>
            <?php else: ?>
            <div class="glass-panel p-8 rounded-3xl border border-cyan-500/30 shadow-glow-cyan">
                <div class="flex justify-center mb-4">
                    <div class="p-3 rounded-2xl bg-cyan-500/10 border border-cyan-500/30 text-cyan-400">
                        <i data-lucide="qr-code" class="w-8 h-8"></i>
                    </div>
                </div>

                <span class="text-cyan-400 text-[10px] font-mono-cyber uppercase tracking-widest block text-center mb-1">
                    [SUPERNOVA 2.0 • ON-SITE VENUE SCANNER]
                </span>
                <h2 class="text-2xl sm:text-3xl font-heading-cyber font-bold text-center text-slate-100 mb-1">
                    VENUE ATTENDANCE CHECK-IN
                </h2>
                <p class="text-slate-400 text-xs text-center mb-6 font-tech">
                    Event: <span class="text-cyan-400 font-bold font-mono-cyber"><?= htmlspecialchars($title) ?></span>
                </p>

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

                <form method="POST" class="space-y-4">
                    <?= csrf_field() ?>
                    <div>
                        <label class="block text-xs font-mono-cyber text-cyan-400 uppercase mb-1">
                            REGISTRATION NUMBER / REGISTRATION ID *
                        </label>
                        <div class="relative">
                            <i data-lucide="ticket" class="w-4 h-4 absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-500"></i>
                            <input type="text" name="registrationId" required placeholder="e.g. 16853166" class="w-full pl-10 pr-4 py-3 rounded-xl bg-slate-900 border border-cyan-500/30 text-slate-100 placeholder-slate-600 focus:outline-none focus:border-cyan-400 text-sm font-mono-cyber" />
                        </div>
                    </div>

                    <div>
                        <label class="block text-xs font-mono-cyber text-cyan-400 uppercase mb-1">
                            PARTICIPANT FULL NAME *
                        </label>
                        <div class="relative">
                            <i data-lucide="user" class="w-4 h-4 absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-500"></i>
                            <input type="text" name="fullName" required placeholder="e.g. Richa Bagdiya" class="w-full pl-10 pr-4 py-3 rounded-xl bg-slate-900 border border-cyan-500/30 text-slate-100 placeholder-slate-600 focus:outline-none focus:border-cyan-400 text-sm" />
                        </div>
                    </div>

                    <div>
                        <label class="block text-xs font-mono-cyber text-cyan-400 uppercase mb-1">
                            REGISTERED EMAIL ADDRESS *
                        </label>
                        <div class="relative">
                            <i data-lucide="mail" class="w-4 h-4 absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-500"></i>
                            <input type="email" name="email" required placeholder="participant@email.com" class="w-full pl-10 pr-4 py-3 rounded-xl bg-slate-900 border border-cyan-500/30 text-slate-100 placeholder-slate-600 focus:outline-none focus:border-cyan-400 text-sm font-mono-cyber" />
                        </div>
                    </div>

                    <button type="submit" class="w-full py-3.5 rounded-xl bg-gradient-to-r from-cyan-500 to-purple-600 text-slate-950 font-mono-cyber font-bold transition-all shadow-glow-cyan flex items-center justify-center gap-2 hover:scale-105 mt-2">
                        CONFIRM CHECK-IN <i data-lucide="check-circle-2" class="w-4 h-4"></i>
                    </button>
                </form>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <script>lucide.createIcons();</script>
</body>
</html>
