<?php
include '../koneksi.php';

header('Content-Type: application/json');

// Default data
$data = [
  'baik' => 0,
  'cukup' => 0,
  'kurang' => 0
];

// Pastikan koneksi tidak error
if (!$conn) {
    echo json_encode($data);
    exit;
}

$sql = "SELECT rating, COUNT(*) as total FROM survey GROUP BY rating";
$result = mysqli_query($conn, $sql);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $ratingKey = strtolower($row['rating']);
        if (array_key_exists($ratingKey, $data)) {
            $data[$ratingKey] = (int)$row['total'];
        }
    }
}

echo json_encode($data);
?>
