<?php
require_once '../config/cors.php';
header('Content-Type: application/json');

require_once '../config/db.php';

$raw = file_get_contents('php://input');
$data = json_decode($raw, true);

$userId = isset($data['user_id']) ? (int) $data['user_id'] : 0;
$notificationId = isset($data['notification_id']) ? (int) $data['notification_id'] : 0;

if ($userId < 1 || $notificationId < 1) {
    echo json_encode(['status' => 'error', 'message' => 'user_id and notification_id required']);
    exit;
}

try {
    // Mark as read for this specific recipient
    $stmt = $pdo->prepare("UPDATE notification_recipients SET read_at = CURRENT_TIMESTAMP WHERE user_id = ? AND notification_id = ? AND read_at IS NULL");
    $stmt->execute([$userId, $notificationId]);

    echo json_encode(['status' => 'success', 'message' => 'Notification marked as read']);
} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
