<?php
session_start();
require 'config.php';

$user_id = $_SESSION['user_id'];
$role = $_SESSION['role']; 

$query = "SELECT id, message, is_read FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT 5";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

$notifications = [];
$unread_count = 0;

while ($row = $result->fetch_assoc()) {
    if ($row['is_read'] == 0) {
        $unread_count++;
    }
    $notifications[] = $row;
}

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

echo json_encode(["notifications" => $notifications, "unread_count" => $unread_count]);
?>
