<?php
session_start();
header("Content-Type: application/json");

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

if(!isset($_SESSION['id'])){
    echo json_encode(["error" => "Session expired"]);
    exit;
}

$user_id = (int)$_SESSION['id'];
$role = $_SESSION['role'] ?? "";

/* =====================================================
   GET USER FROM PARTICIPANTS 
===================================================== */

$stmt = $conn->prepare("
SELECT 
    u.email,
    p.full_name,
    p.matric_no,
    p.staff_no,
    p.phone
FROM users u
JOIN participants p ON p.user_id = u.id
WHERE u.id = ?
LIMIT 1
");

if(!$stmt){
    echo json_encode(["error" => "Query failed"]);
    exit;
}

$stmt->bind_param("i", $user_id);
$stmt->execute();

$result = $stmt->get_result()->fetch_assoc();

/* =====================================================
   SAFE RESPONSE
===================================================== */

if(!$result){
    echo json_encode(["error" => "User not found"]);
    exit;
}

/* OPTIONAL: normalize output */
echo json_encode([
    "email" => $result["email"],
    "full_name" => $result["full_name"],
    "matric_no" => $result["matric_no"],
    "staff_no" => $result["staff_no"],
    "phone" => $result["phone"]
]);