<?php
require_once 'config.php';

// Get event from URL parameter
$event_slug = isset($_GET['event']) ? trim($_GET['event']) : '';

if (empty($event_slug)) {
    // Redirect to home page if no event specified
    header('Location: home.php');
    exit;
}

// Sanitize event slug
$event_slug = preg_replace('/[^a-z0-9-]/', '', strtolower($event_slug));

// Load event data from database
$conn = getDBConnection();
$stmt = $conn->prepare("SELECT event_name, celebrant1, celebrant2, programme_filename, event_date, header_image FROM events WHERE event_slug = ?");
$stmt->bind_param("s", $event_slug);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    $stmt->close();
    $conn->close();
    die('Event not found.');
}

$event_data = $result->fetch_assoc();
$stmt->close();

// Check if event has expired (7 days after event_date)
$now = new DateTime();
if (!empty($event_data['event_date'])) {
    $event_date = new DateTime($event_data['event_date']);
    $expiry_date = (clone $event_date)->modify('+7 days');
    
    if ($expiry_date < $now) {
        $conn->close();
        die('This event has expired and is no longer available.');
    }
}
// Events without event_date don't expire

$conn->close();

// Verify programme file exists
$event_dir = __DIR__ . '/events/' . $event_slug;
$programme_file = $event_dir . '/' . $event_data['programme_filename'];

if (!file_exists($programme_file)) {
    die('Programme file not found.');
}

$celebrant1 = htmlspecialchars($event_data['celebrant1']);
$celebrant2 = htmlspecialchars($event_data['celebrant2']);
$event_name = htmlspecialchars($event_data['event_name']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Programme - <?php echo $event_name; ?> | Evently</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background-color: #ffffff;
            color: #333;
            min-height: 100vh;
            padding: 0;
        }

        .navbar {
            background-color: #ffffff;
            padding: 15px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .navbar-brand {
            color: #000000;
            font-family: 'Inter', sans-serif;
            font-size: 20px;
            font-weight: 700;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .navbar-brand img {
            height: 30px;
            width: auto;
        }

        .footer {
            background-color: #ffffff;
            padding: 20px;
            border-top: 1px solid #e0e0e0;
            text-align: center;
            color: #666;
            font-size: 14px;
        }

        .page-content {
            display: flex;
            flex-direction: column;
            min-height: calc(100vh - 60px);
            padding: 0;
        }

        .header-image-container {
            width: 100%;
            max-height: 450px;
            overflow: hidden;
            background-color: #ffffff;
            position: relative;
        }

        .header-image {
            width: 100%;
            height: auto;
            object-fit: cover;
            display: block;
        }

        .content-wrapper {
            background-color: #ffffff;
            position: relative;
            z-index: 1;
        }

        .container {
            background-color: #ffffff;
            border-radius: 0;
            padding: 60px 40px;
            max-width: 600px;
            width: 100%;
            text-align: center;
            margin: 0 auto;
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        h1 {
            font-size: 32px;
            font-weight: 300;
            margin-bottom: 16px;
            color: #1a1a1a;
            letter-spacing: -0.5px;
        }

        .celebrants {
            font-size: 24px;
            color: #2c3e50;
            margin-bottom: 48px;
            font-weight: 400;
            letter-spacing: 0.3px;
        }

        .subtitle {
            font-size: 16px;
            color: #6c757d;
            margin-bottom: 40px;
        }

        .download-section {
            margin-top: 0;
        }

        .download-item {
            margin-bottom: 0;
            padding: 0;
            border: none;
            border-radius: 0;
            background-color: transparent;
        }

        .download-link,
        .view-link {
            display: inline-block;
            padding: 16px 48px;
            background-color: #000000;
            color: #ffffff;
            text-decoration: none;
            border-radius: 0;
            font-size: 15px;
            font-weight: 500;
            letter-spacing: 0.5px;
            transition: all 0.2s ease;
            min-width: 180px;
            text-align: center;
            text-transform: uppercase;
            border: 2px solid #000000;
        }

        .download-link:hover,
        .view-link:hover {
            background-color: #ffffff;
            color: #000000;
        }

        .button-group {
            display: flex;
            flex-direction: column;
            gap: 12px;
            align-items: center;
            margin-top: 0;
        }

        @media (min-width: 500px) {
            .button-group {
                flex-direction: row;
                justify-content: center;
                gap: 16px;
            }
        }

        .create-link {
            margin-top: 48px;
            padding-top: 32px;
            border-top: 1px solid #e8e8e8;
        }

        .create-link a {
            color: #666;
            text-decoration: none;
            font-size: 14px;
            font-weight: 400;
            transition: color 0.2s ease;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .create-link a:hover {
            color: #000000;
        }

        .create-link a::after {
            content: '→';
            transition: transform 0.2s ease;
        }

        .create-link a:hover::after {
            transform: translateX(3px);
        }

        .file-name {
            font-size: 14px;
            color: #6c757d;
            margin-top: 8px;
        }


        @media (max-width: 768px) {
            .container {
                padding: 50px 24px;
                margin: 0 auto;
            }

            .page-content {
                padding: 0;
            }

            .header-image-container {
                max-height: 280px;
            }

            h1 {
                font-size: 28px;
                margin-bottom: 12px;
            }

            .celebrants {
                font-size: 20px;
                margin-bottom: 40px;
            }

            .download-link,
            .view-link {
                padding: 14px 32px;
                font-size: 14px;
                min-width: 100%;
                width: 100%;
            }

            .button-group {
                flex-direction: column;
                gap: 12px;
            }

            .create-link {
                margin-top: 40px;
                padding-top: 28px;
            }
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <a href="home.php" class="navbar-brand">
            <img src="assets/img/logo.svg" alt="Evently">
            <span>Evently</span>
        </a>
    </nav>
    <div class="page-content">
    <?php if (!empty($event_data['header_image'])): 
        $header_image_path = 'events/' . $event_slug . '/' . $event_data['header_image'];
        if (file_exists($header_image_path)): ?>
            <div class="header-image-container">
                <img src="<?php echo htmlspecialchars($header_image_path); ?>" alt="Event Header" class="header-image">
            </div>
        <?php endif; 
    endif; ?>
    <div class="content-wrapper">
        <div class="container">
            <h1>Event Programme</h1>
            <div class="celebrants">
                <?php echo $celebrant1; ?>
                <?php if (!empty($celebrant2)): ?>
                    & <?php echo $celebrant2; ?>
                <?php endif; ?>
            </div>
            
            <div class="download-section">
                <div class="download-item">
                    <div class="button-group">
                        <a href="view.php?event=<?php echo urlencode($event_slug); ?>" class="view-link" target="_blank">View</a>
                        <a href="download.php?event=<?php echo urlencode($event_slug); ?>" class="download-link">Download PDF</a>
                    </div>
                </div>
            </div>
            
            <div class="create-link">
                <a href="register.php">Create your own event</a>
            </div>
        </div>
    </div>
    </div>
    <footer class="footer">
        <p>&copy; <?php echo date('Y'); ?> Evently. All rights reserved.</p>
    </footer>
</body>
</html>


