<?php
require_once '../config/koneksi.php';
require_once '../config/jwt.php';

// ================= AUTH =================
function getAuthHeader() {
    return $_SERVER['HTTP_AUTHORIZATION'] 
        ?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] 
        ?? '';
}

$auth = getAuthHeader();

if (!$auth) {
    echo json_encode(["success"=>false,"message"=>"Unauthorized"]);
    exit;
}

$token = str_replace("Bearer ", "", $auth);
$userData = verifyJWT($token);

if (!$userData) {
    echo json_encode(["success"=>false,"message"=>"Token invalid"]);
    exit;
}

$userId = $userData['user_id'];

// ================= QUERY =================
$stmt = $conn->prepare("
    SELECT COUNT(*) as total
    FROM notifications
    WHERE user_id=? AND is_read=0 AND is_deleted=0
");

$stmt->bind_param("s", $userId);
$stmt->execute();
$res = $stmt->get_result()->fetch_assoc();

echo json_encode([
    "success" => true,
    "total" => (int)$res['total']
]);