<?php

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");

if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
    exit;
}

require "../../config/database.php";
require "../../helpers/response.php";

$id = isset($_POST["id"]) ? (int) $_POST["id"] : 0;

if ($id <= 0) {
    response(false, "ID notifikasi tidak valid", null);
}

$query = "
    UPDATE notifikasi
    SET is_read = 1
    WHERE id = ?
";

$stmt = $conn->prepare($query);

if (!$stmt) {
    response(false, "Prepare update notifikasi gagal: " . $conn->error, null);
}

$stmt->bind_param("i", $id);

if ($stmt->execute()) {
    response(true, "Notifikasi berhasil ditandai dibaca", [
        "id" => $id
    ]);
}

response(false, "Gagal menandai notifikasi", null);

?>