<?php
header('Content-Type: application/json; charset=utf-8');
include 'connection.php';
include 'decrypt_and_validate.php';

$response = [];

try {
    if (!isset($_POST['user_id']) || !isset($_POST['API_Key'])) {
        throw new Exception("Missing parameters");
    }

    $uidEnc = $_POST['user_id'];
    $apiEnc = $_POST['API_Key'];

    $userId = decryptData($uidEnc);
    $apiKey = decryptData($apiEnc);

    if (empty($userId)) {
        throw new Exception("Invalid user_id");
    }

    $validApiKey = "AbuTamim";
    if ($apiKey !== $validApiKey) {
        throw new Exception("Unauthorized API key");
    }

    $stmt = $conn->prepare("UPDATE Notification_Bond_Win 
                            SET Show_Or_Not = 1 
                            WHERE User_ID = ?");
    $stmt->bind_param("s", $userId);
    $stmt->execute();

    $response = ["status" => "success", "message" => "Notifications marked as read"];
    $stmt->close();
} catch (Exception $e) {
    $response = ["status" => "error", "message" => $e->getMessage()];
} finally {
    $conn->close();
}

echo json_encode($response);
?>
