<?php
// get_event_participants.php
// Returns a list of users registered for a specific event

header('Content-Type: application/json');
require_once 'db.php';

$event_id = isset($_GET['event_id']) ? intval($_GET['event_id']) : 0;

if ($event_id <= 0) {
    echo json_encode(["status" => "error", "message" => "Invalid event ID"]);
    exit();
}

// Fetch participants with user info
$sql = "SELECT ep.user_id, ep.status as registration_status, u.username, u.profile_pic 
        FROM event_participants ep 
        JOIN users u ON ep.user_id = u.id 
        WHERE ep.event_id = ? 
        ORDER BY ep.created_at DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $event_id);
$stmt->execute();
$result = $stmt->get_result();

$participants = [];
while ($row = $result->fetch_assoc()) {
    // Format profile pic URL if needed
    if (!empty($row['profile_pic']) && !filter_var($row['profile_pic'], FILTER_VALIDATE_URL)) {
        // Assuming profile pics are in assets/images or similar standard directory
        // For now, return as is or adjust base path if known
    }
    $participants[] = $row;
}

echo json_encode([
    "status" => "success",
    "event_id" => $event_id,
    "participants" => $participants
]);

$stmt->close();
$conn->close();
?>