<?php
session_start();
require_once 'db/config.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

$attendee = null;
$webinar = null;
$error = '';

$user_id = $_SESSION['user_id'];

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

    if ($attendee) {
        // Fetch webinar details
        $stmt = db()->prepare("SELECT * FROM webinars WHERE id = ?");
        $stmt->execute([$attendee['webinar_id']]);
        $webinar = $stmt->fetch();
    } else {
        $error = 'Could not find your registration details.';
    }
} catch (PDOException $e) {
    $error = 'Database error. Please try again later.';
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Webinar Dashboard</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;
        }
        .dashboard-box {
            background-color: #2d3748;
            border-radius: 0.5rem;
            padding: 2rem;
            border: 1px solid #4a5568;
        }
        h1 {
            font-size: 2rem;
            margin-bottom: 1rem;
        }
        .webinar-title {
            font-size: 1.5rem;
            font-weight: bold;
            color: #f6e05e;
            margin-bottom: 0.5rem;
        }
        .webinar-date {
            font-size: 1.125rem;
            margin-bottom: 1.5rem;
        }
        .join-button {
            display: inline-block;
            background-color: #f6e05e;
            color: #1a202c;
            padding: 0.75rem 1.5rem;
            border-radius: 0.375rem;
            text-decoration: none;
            font-weight: bold;
            font-size: 1.125rem;
            transition: background-color 0.2s;
        }
        .join-button:hover {
            background-color: #f6d32d;
        }
        .message {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 0.25rem;
            background-color: #c53030;
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="dashboard-box">
            <h1>Your Dashboard</h1>
            <?php if ($error): ?>
                <div class="message"><?= htmlspecialchars($error) ?></div>
            <?php elseif ($attendee && $webinar): ?>
                <p>Hello, <strong><?= htmlspecialchars($attendee['first_name']) ?></strong>!</p>
                <p>You are registered for the following webinar:</p>
                <div class="webinar-title"><?= htmlspecialchars($webinar['title']) ?></div>
                <div class="webinar-date">
                    <?php 
                        $date = new DateTime($webinar['scheduled_at']);
                        echo $date->format('l, F j, Y \a\t g:i A T');
                    ?>
                </div>
                <a href="join.php?id=<?= htmlspecialchars($attendee['id']) ?>" class="join-button">Join Webinar</a>
            <?php else: ?>
                 <div class="message">Could not find your registration details.</div>
            <?php endif; ?>
        </div>
         <footer style="margin-top: 2rem; font-size: 0.9rem; color: #a0aec0;">
            <a href="index.php" style="color: #a0aec0; text-decoration: none;">← Back to Home</a>
        </footer>
    </div>
</body>
</html>
