<?php
require_once('../../../'.$_REQUEST['tenant'].'/config.php');

session_start();
$userid = $_SESSION['userid']; // Current user's ID
$response = [];

// Fetch new messages or assignment updates
$sql = "SELECT m.message, m.status, CONCAT(u.forename, ' ', u.surname) AS sender 
        FROM messages m 
        JOIN users u ON m.sender_id = u.id
        WHERE m.receiver_id = ? 
        ORDER BY m.created_at DESC";
$stmt = $con->prepare($sql);
$stmt->bind_param('i', $userid);
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    $response['notifications'][] = [
        'message' => $row['message'],
        'status' => $row['status'],
        'sender' => $row['sender']
    ];
}

echo json_encode($response);
?>