<?php
session_start();
include($_SERVER['DOCUMENT_ROOT']."/SEAT/config/db.php");

header("Content-Type: application/json");

if(!isset($_SESSION['user_id'])){
echo json_encode(["error"=>"No session"]);
exit;
}

$user_id = intval($_SESSION['user_id']);

/* GET EVENTS */
$sql = "
SELECT 
e.id,
e.title,
e.event_date,
e.organizer_id,
o.user_id,
o.club_name
FROM events e
JOIN organizers o ON e.organizer_id = o.id
WHERE o.user_id = '$user_id'
AND e.status = 'upcoming'
ORDER BY e.event_date ASC
";

$res = mysqli_query($conn,$sql);

$data = [];

while($row = mysqli_fetch_assoc($res)){
$data[] = $row;
}

echo json_encode([
"user_session"=>$user_id,
"total"=>count($data),
"events"=>$data
], JSON_PRETTY_PRINT);