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

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

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

$data = [
  "summary" => [
    "total_pengunjung" => 0,
    "pengunjung_hari_ini" => 0,
    "sedang_online" => 0,
    "halaman_dibuka_hari_ini" => 0
  ],
  "grafik_7_hari" => [],
  "halaman_populer" => [],
  "role_aktif" => []
];

$sqlSummary = "
  SELECT
    (SELECT COUNT(DISTINCT visitor_id) FROM visitor_logs) AS total_pengunjung,
    (SELECT COUNT(DISTINCT visitor_id) FROM visitor_logs WHERE DATE(created_at) = CURDATE()) AS pengunjung_hari_ini,
    (SELECT COUNT(DISTINCT visitor_id) FROM visitor_logs WHERE last_active >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)) AS sedang_online,
    (SELECT COUNT(*) FROM visitor_logs WHERE DATE(created_at) = CURDATE()) AS halaman_dibuka_hari_ini
";
$resultSummary = $conn->query($sqlSummary);
if ($resultSummary && $row = $resultSummary->fetch_assoc()) {
  $data["summary"] = [
    "total_pengunjung" => (int) $row["total_pengunjung"],
    "pengunjung_hari_ini" => (int) $row["pengunjung_hari_ini"],
    "sedang_online" => (int) $row["sedang_online"],
    "halaman_dibuka_hari_ini" => (int) $row["halaman_dibuka_hari_ini"]
  ];
}

$sqlGrafik = "
  SELECT DATE(created_at) AS tanggal, COUNT(DISTINCT visitor_id) AS pengunjung
  FROM visitor_logs
  WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 6 DAY)
  GROUP BY DATE(created_at)
  ORDER BY tanggal ASC
";
$resultGrafik = $conn->query($sqlGrafik);
$grafikMap = [];
if ($resultGrafik) {
  while ($row = $resultGrafik->fetch_assoc()) {
    $grafikMap[$row["tanggal"]] = (int) $row["pengunjung"];
  }
}
for ($i = 6; $i >= 0; $i--) {
  $tanggal = date("Y-m-d", strtotime("-$i day"));
  $data["grafik_7_hari"][] = ["tanggal" => $tanggal, "pengunjung" => $grafikMap[$tanggal] ?? 0];
}

$sqlHalaman = "
  SELECT page_url, COUNT(*) AS total_dibuka, COUNT(DISTINCT visitor_id) AS total_pengunjung
  FROM visitor_logs
  GROUP BY page_url
  ORDER BY total_dibuka DESC
  LIMIT 8
";
$resultHalaman = $conn->query($sqlHalaman);
if ($resultHalaman) {
  while ($row = $resultHalaman->fetch_assoc()) {
    $data["halaman_populer"][] = [
      "page_url" => $row["page_url"],
      "total_dibuka" => (int) $row["total_dibuka"],
      "total_pengunjung" => (int) $row["total_pengunjung"]
    ];
  }
}

$sqlRole = "
  SELECT role, COUNT(*) AS total_aktivitas, COUNT(DISTINCT visitor_id) AS total_pengunjung
  FROM visitor_logs
  GROUP BY role
  ORDER BY total_aktivitas DESC
";
$resultRole = $conn->query($sqlRole);
$roleLabels = [
  "publik" => "Publik",
  "admin" => "Admin",
  "sppg" => "SPPG",
  "validator" => "Pihak Sekolah",
  "pemerintah" => "Pemerintah"
];
if ($resultRole) {
  while ($row = $resultRole->fetch_assoc()) {
    $role = $row["role"] ?: "publik";
    $data["role_aktif"][] = [
      "role" => $role,
      "label" => $roleLabels[$role] ?? ucfirst($role),
      "total_aktivitas" => (int) $row["total_aktivitas"],
      "total_pengunjung" => (int) $row["total_pengunjung"]
    ];
  }
}

response(true, "Statistik pengunjung berhasil diambil", $data);
?>
