<?php
session_start();
include_once __DIR__ . "/../config/db.php";

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$user_id = $_SESSION['user_id'];

if (isset($_POST['all'])) {
    $sql = "UPDATE user_notifications SET is_read = 1 WHERE user_id = $user_id";
    if (mysqli_query($conn, $sql)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => mysqli_error($conn)]);
    }
    exit();
}

if (isset($_POST['id'])) {
    $notif_id = mysqli_real_escape_string($conn, $_POST['id']);
    $sql = "UPDATE user_notifications SET is_read = 1 WHERE id = '$notif_id' AND user_id = $user_id";
    if (mysqli_query($conn, $sql)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => mysqli_error($conn)]);
    }
    exit();
}

echo json_encode(['success' => false, 'message' => 'Invalid Request']);
?>
