<?php
declare(strict_types=1);
session_start();
require_once '../../config.php';
header('Content-Type: application/json');
if (!isset($_SESSION['parent_id'])) { http_response_code(401); echo json_encode(['notifications'=>[], 'unread_count'=>0]); exit; }
$conn = getDBConnection();
if (!$conn instanceof mysqli) { echo json_encode(['notifications'=>[], 'unread_count'=>0]); exit; }
$parentId = (int) $_SESSION['parent_id'];
$notifications = [];
$stmt = $conn->prepare("SELECT * FROM parent_notifications WHERE parent_id = ? ORDER BY created_at DESC LIMIT 20");
$stmt->bind_param('i', $parentId);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) { $notifications[] = $row; }
$stmt->close();
$countStmt = $conn->prepare("SELECT COUNT(*) AS unread_count FROM parent_notifications WHERE parent_id = ? AND is_read = 0");
$countStmt->bind_param('i', $parentId);
$countStmt->execute();
$unreadCount = (int) (($countStmt->get_result()->fetch_assoc()['unread_count'] ?? 0));
$countStmt->close();
echo json_encode(['notifications' => $notifications, 'unread_count' => $unreadCount]);
