<?php
require __DIR__ . '/../Login/config.php';
if (session_status() === PHP_SESSION_NONE) session_start();
header('Content-Type: application/json');

if (empty($_SESSION['user_id'])) {
  echo json_encode(['count'=>0,'notifications'=>[]]); exit;
}
$user_id = (int)$_SESSION['user_id'];

$stmt = $pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id = :uid AND is_read = 0");
$stmt->execute([':uid'=>$user_id]);
$count = (int)$stmt->fetchColumn();

$stmt2 = $pdo->prepare("SELECT id, message, is_read, created_at 
                        FROM notifications 
                        WHERE user_id = :uid
                        ORDER BY created_at DESC 
                        LIMIT 9");
$stmt2->execute([':uid'=>$user_id]);
$list = $stmt2->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(['count'=>$count,'notifications'=>$list]);
