<?php
require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
require_once __DIR__ . '/../engine/Logger.php'; // COVERAGE
$ctx = Kernel::handle(['auth' => true]);
$u   = $ctx['user'];
$uid = (int)$u['id'];
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); exit; }
$b = json_decode(file_get_contents('php://input'), true) ?? [];
$notifId = (int)($b['id'] ?? 0);
try {
    if ($notifId > 0) {
        db()->prepare("UPDATE notifications SET is_read=1 WHERE id=? AND user_id=?")->execute([$notifId,$uid]);
    } else {
        db()->prepare("UPDATE notifications SET is_read=1 WHERE user_id=?")->execute([$uid]);
    }
    echo json_encode(['ok'=>true]);
} catch (Throwable $e) {
    echo json_encode(['error' => 'server_error', 'message' => 'خطأ مؤقت — حاول مرة أخرى'], JSON_UNESCAPED_UNICODE);
}
