<?php
define('SECURE_ACCESS', true);

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
requireLogin();

$eventId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$event = null;

try {
    if ($pdo && $eventId > 0) {
        $stmt = $pdo->prepare("
            SELECT e.*, u.username as organizer_name 
            FROM events e 
            LEFT JOIN users u ON e.created_by = u.id 
            WHERE e.id = ?
        ");
        $stmt->execute([$eventId]);
        $event = $stmt->fetch(PDO::FETCH_ASSOC);
    }
} catch (PDOException $e) {
    // Ignore
}

$isAdminOrOrganizer = false;
if (isLoggedIn()) {
    $currUser = getCurrentUser();
    if ($currUser && ($currUser['role'] === 'admin' || $currUser['role'] === 'organizer')) {
        $isAdminOrOrganizer = true;
    }
}

if ($event && isset($event['status']) && $event['status'] === 'draft' && !$isAdminOrOrganizer) {
    $event = null;
}

if ($event) {
    $meta_title = "Event: " . $event['title'];
    $meta_description = substr(strip_tags($event['description']), 0, 150) . '...';
    $meta_image = !empty($event['banner_url']) ? $event['banner_url'] : '';
}

$pageTitle = $event ? $event['title'] : "Event Details";
require_once __DIR__ . '/../includes/header.php';

if (!$event) {
    echo '<div class="container-fluid px-md-5 py-5 text-center">';
    echo '<div class="alert alert-danger d-inline-block p-4 border-0 shadow-sm" role="alert">';
    echo '<i class="bi bi-exclamation-triangle-fill fs-3 mb-2 d-block text-danger"></i>';
    echo '<h4 class="fw-bold">Event Not Found</h4>';
    echo '<p class="text-secondary small mb-0">The conference details you requested could not be resolved or does not exist.</p>';
    echo '<a href="public_events" class="btn btn-primary fw-bold btn-sm mt-3"><i class="bi bi-arrow-left me-1"></i> Back to Start Here</a>';
    echo '</div>';
    echo '</div>';
    include __DIR__ . '/../includes/footer.php';
    exit();
}

$banner_img = !empty($event['banner_url']) ? $event['banner_url'] : 'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=1200&auto=format&fit=crop';
?>

<div class="container-fluid px-md-5 py-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="public_events" class="text-primary text-decoration-none">Start Here</a></li>
            <li class="breadcrumb-item active" aria-current="page"><?php echo htmlspecialchars($event['title']); ?></li>
        </ol>
    </nav>

    <div class="row g-4">
        <!-- Event Details Column -->
        <div class="col-12 col-lg-8">
            <div class="card border-0 shadow-sm overflow-hidden mb-4 bg-white">
                <img src="<?php echo htmlspecialchars($banner_img); ?>" class="img-fluid" style="width: 100%; max-height: 380px; object-fit: cover;" alt="<?php echo htmlspecialchars($event['title']); ?>">
                <div class="card-body p-4 p-md-5">
                    <span class="badge bg-primary-subtle text-primary mb-3 font-monospace" style="font-size: 0.75rem;"><?php echo htmlspecialchars($event['category'] ?? 'General Event'); ?></span>
                    <h1 class="fw-bold text-dark mb-4"><?php echo htmlspecialchars($event['title']); ?></h1>
                    
                    <h5 class="fw-bold text-dark mb-3"><i class="bi bi-info-circle-fill text-primary me-2"></i>Conference Description</h5>
                    <div class="text-secondary leading-relaxed mb-4">
                        <?php echo nl2br(htmlspecialchars($event['description'])); ?>
                    </div>

                    <!-- Event Resources / Downloadable Attachments -->
                    <?php if (!empty($event['attachment_url'])): ?>
                        <div class="card border-0 bg-light-subtle shadow-sm mb-4 rounded-3 border-start border-primary border-4 mt-4">
                            <div class="card-body p-3">
                                <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
                                    <div class="d-flex align-items-center">
                                        <div class="p-2 bg-primary bg-opacity-10 text-primary rounded-circle me-3">
                                            <i class="bi bi-file-earmark-arrow-down fs-4"></i>
                                        </div>
                                        <div>
                                            <h6 class="fw-bold mb-0 text-dark">Event Resource Materials</h6>
                                            <small class="text-secondary font-monospace"><?php echo htmlspecialchars(basename($event['attachment_url'])); ?></small>
                                        </div>
                                    </div>
                                    <div>
                                        <a href="<?php echo htmlspecialchars($event['attachment_url']); ?>" download class="btn btn-primary btn-sm fw-bold px-3">
                                            <i class="bi bi-cloud-arrow-down-fill me-1"></i> Download File
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Sidebar Actions Column -->
        <div class="col-12 col-lg-4">
            <div class="card border-0 shadow-sm p-4 bg-white mb-4 position-sticky" style="top: 100px;">
                <h5 class="fw-bold text-dark mb-3 border-bottom pb-2">Event Coordination</h5>
                
                <div class="d-flex align-items-center mb-3 text-secondary">
                    <i class="bi bi-calendar-check fs-4 text-primary me-3"></i>
                    <div>
                        <small class="text-muted d-block font-monospace text-uppercase" style="font-size: 0.65rem;">Schedule</small>
                        <span class="small fw-semibold"><?php echo date('M d, Y', strtotime($event['start_date'])); ?></span>
                        <span class="text-muted d-block small" style="font-size: 0.75rem;"><?php echo date('H:i A', strtotime($event['start_date'])) . ' - ' . date('H:i A', strtotime($event['end_date'])); ?></span>
                    </div>
                </div>

                <div class="d-flex align-items-center mb-3 text-secondary">
                    <i class="bi bi-geo-alt fs-4 text-primary me-3"></i>
                    <div>
                        <small class="text-muted d-block font-monospace text-uppercase" style="font-size: 0.65rem;">Venue Location</small>
                        <span class="small fw-semibold"><?php echo htmlspecialchars($event['location'] ?? 'Virtual'); ?></span>
                    </div>
                </div>

                <div class="d-flex align-items-center mb-4 text-secondary">
                    <i class="bi bi-person-circle fs-4 text-primary me-3"></i>
                    <div>
                        <small class="text-muted d-block font-monospace text-uppercase" style="font-size: 0.65rem;">Organizer</small>
                        <span class="small fw-semibold"><?php echo htmlspecialchars($event['organizer_name'] ?? 'System Admin'); ?></span>
                    </div>
                </div>

                <div class="border-top pt-4 mb-4 text-center">
                    <small class="text-muted d-block font-monospace text-uppercase mb-1" style="font-size: 0.65rem;">Registration Pass Price</small>
                    <span class="display-6 fw-bold text-dark"><?php echo (float)$event['price'] > 0 ? '₹' . number_format($event['price'], 2) : '<span class="text-success">FREE</span>'; ?></span>
                </div>

                <div class="d-grid gap-2">
                    <?php if (isLoggedIn()): ?>
                        <a href="event/<?php echo $event['id']; ?>" class="btn btn-primary btn-lg py-2.5 fw-bold"><i class="bi bi-ticket-perforated-fill me-2"></i> Register for Event</a>
                    <?php else: ?>
                        <a href="login?redirect=event/<?php echo $event['id']; ?>" class="btn btn-primary btn-lg py-2.5 fw-bold"><i class="bi bi-box-arrow-in-right me-2"></i> Log In to Register</a>
                        <span class="text-muted small text-center mt-1">Don't have an account? <a href="register" class="text-primary text-decoration-none fw-semibold">Sign Up</a></span>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
include __DIR__ . '/../includes/footer.php';
?>
