<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
header('Content-Type: application/json');

$uid = gp_current_user_id();
if ($uid <= 0) {
    echo json_encode(['ok' => false]);
    exit;
}

$input = json_decode(file_get_contents('php://input'), true);
if (isset($input['mark_all']) && $input['mark_all'] === true) {
    $stmt = crm_pdo()->prepare("UPDATE user_notifications SET is_read = 1 WHERE user_id = ?");
    $stmt->execute([$uid]);
} elseif (isset($input['id'])) {
    $stmt = crm_pdo()->prepare("UPDATE user_notifications SET is_read = 1 WHERE id = ? AND user_id = ?");
    $stmt->execute([(int)$input['id'], $uid]);
}

echo json_encode(['ok' => true]);
