<?php
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/permissions.php';
require_once '../../services/notifications/NotificationService.php';

require_login();
header('Content-Type: application/json; charset=utf-8');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Método no permitido.']);
    exit;
}

$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

try {
    $service = new NotificationService(db());
    $ok = $service->markRead($id, current_user_id());
    echo json_encode(['success' => $ok, 'unread_count' => $service->unreadCount(current_user_id())]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
