<?php
require_once 'db/config.php';

$attendee_id_encoded = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_STRING);
if (!$attendee_id_encoded) {
    http_response_code(400);
    echo "Invalid link.";
    exit;
}

$attendee_id = base64_decode($attendee_id_encoded);

$attendee = null;
$webinar = null;

try {
    $stmt = db()->prepare("SELECT * FROM attendees WHERE id = ?");
    $stmt->execute([$attendee_id]);
    $attendee = $stmt->fetch();

    if ($attendee) {
        $stmt = db()->prepare("SELECT * FROM webinars WHERE id = ?");
        $stmt->execute([$attendee['webinar_id']]);
        $webinar = $stmt->fetch();
    }

} catch (PDOException $e) {
    // Log error
}

if (!$attendee || !$webinar) {
    http_response_code(404);
    echo "Registration not found.";
    exit;
}

// For now, just a welcome message. In a real scenario, this would redirect to the webinar platform.

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to the Webinar</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #1a202c;
            color: #e2e8f0;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            text-align: center;
        }
        .container {
            max-width: 800px;
            padding: 2rem;
        }
        h1 {
            color: #f6e05e;
            font-size: 2.5rem;
        }
        p {
            font-size: 1.25rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Welcome, <?= htmlspecialchars($attendee['name']) ?>!</h1>
        <p>You are now joining the webinar: <strong><?= htmlspecialchars($webinar['title']) ?></strong></p>
        <p>The webinar is scheduled for: <strong><?= (new DateTime($webinar['scheduled_at']))->format('l, F j, Y \a\t g:i A T') ?></strong></p>
    </div>
</body>
</html>
