<?php
require_once '../config/database.php';
session_start();

header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'error' => 'Not logged in']);
    exit();
}

$conn = getConnection();
$user_id = intval($_SESSION['user_id']);

// Get unread count
$query = "SELECT COUNT(DISTINCT m.id) as unread_count 
          FROM support_messages m 
          JOIN support_tickets t ON m.ticket_id = t.id 
          WHERE t.user_id = '$user_id' 
          AND m.sender_type = 'admin' 
          AND m.is_read = 0";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);

echo json_encode([
    'success' => true,
    'unread_count' => (int)($data['unread_count'] ?? 0)
]);
exit();
?>