<?php
// File: API_KONEKSI/notifikasi/cek_unread.php
header('Content-Type: application/json');
include '../config/koneksi.php';

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $user_id = $_GET['user_id'] ?? '';

    if (empty($user_id)) {
        echo json_encode(["status" => "gagal", "unread_count" => 0]);
        exit;
    }

    // Hitung jumlah notifikasi yang is_read = 0 (belum dibaca)
    $stmt = $conn->prepare("SELECT COUNT(*) as unread_count FROM dat_notifikasi WHERE user_id = ? AND is_read = 0 AND deleted_st = 0");
    $stmt->bind_param("s", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();

    echo json_encode([
        "status" => "sukses", 
        "unread_count" => $row['unread_count']
    ]);
    
    $stmt->close();
}
$conn->close();
?>