<?php
// File: API_KONEKSI/notifikasi/read_notifikasi.php

header('Content-Type: application/json');
include '../config/koneksi.php';
include '../config/middleware.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $notif_id = $_POST['notif_id'] ?? 0;
    // Jika notif_id = 'all', berarti mark all as read
    
    if ($notif_id === 'all') {
        $payload = authenticateJWT();
    $user_id = $payload['user_id'] ?? '';
        $stmt = $conn->prepare("UPDATE dat_notifikasi SET is_read = 1 WHERE user_id = ?");
        $stmt->bind_param("s", $user_id);
    } else {
        $stmt = $conn->prepare("UPDATE dat_notifikasi SET is_read = 1 WHERE notif_id = ?");
        $stmt->bind_param("s", $notif_id);
    }

    if ($stmt->execute()) {
        echo json_encode(["status" => "sukses", "pesan" => "Status diperbarui", "data" => []]);
    } else {
        echo json_encode(["status" => "gagal", "pesan" => "Gagal memperbarui status", "data" => []]);
    }
    $stmt->close();
}
$conn->close();
?>
