<?php
/**
 * POST /api/notifications/mark_read.php
 * Body (JSON): { id } to mark one, or { mark_all: true } to mark everything read.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';
require_once __DIR__ . '/../includes/auth.php';

only_post_allowed();
$userId = require_login();

$input = get_json_input();
$pdo = Database::connect();

if (!empty($input['mark_all'])) {
    $stmt = $pdo->prepare('UPDATE notifications SET is_read = 1 WHERE user_id = ?');
    $stmt->execute([$userId]);
    send_json(true, 'All notifications marked as read.');
}

$id = (int) ($input['id'] ?? 0);
if ($id <= 0) {
    send_error('A valid notification id (or mark_all) is required.', 422);
}

$stmt = $pdo->prepare('UPDATE notifications SET is_read = 1 WHERE id = ? AND user_id = ?');
$stmt->execute([$id, $userId]);

send_json(true, 'Notification marked as read.');
