<?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";

$totalSppg = 0;
$totalSekolah = 0;
$totalSiswa = 0;
$totalDana = 0;

$totalKaryawan = 0;
$totalGajiKaryawan = 0;

$totalBahan = 0;
$totalNilaiBahan = 0;

$totalOmprengan = 0;
$totalBarangOmprengan = 0;

$totalLaporan = 0;
$totalPorsi = 0;
$totalTerdistribusi = 0;

$totalPengaduan = 0;
$pengaduanBaru = 0;
$pengaduanDiproses = 0;
$pengaduanSelesai = 0;

$resultSppg = $conn->query("SELECT COUNT(*) AS total FROM sppg");
if ($resultSppg) {
    $row = $resultSppg->fetch_assoc();
    $totalSppg = (int) $row["total"];
}

$resultDana = $conn->query("SELECT COALESCE(SUM(total_dana), 0) AS total FROM sppg");
if ($resultDana) {
    $row = $resultDana->fetch_assoc();
    $totalDana = (float) $row["total"];
}

$resultSekolah = $conn->query("SELECT COUNT(*) AS total FROM sekolah");
if ($resultSekolah) {
    $row = $resultSekolah->fetch_assoc();
    $totalSekolah = (int) $row["total"];
}

$resultSiswa = $conn->query("SELECT COALESCE(SUM(jumlah_siswa), 0) AS total FROM sekolah");
if ($resultSiswa) {
    $row = $resultSiswa->fetch_assoc();
    $totalSiswa = (int) $row["total"];
}

$resultKaryawan = $conn->query("
    SELECT 
        COUNT(*) AS total,
        COALESCE(SUM(gaji), 0) AS total_gaji
    FROM karyawan
");
if ($resultKaryawan) {
    $row = $resultKaryawan->fetch_assoc();
    $totalKaryawan = (int) $row["total"];
    $totalGajiKaryawan = (float) $row["total_gaji"];
}

$resultBahan = $conn->query("
    SELECT 
        COUNT(*) AS total,
        COALESCE(SUM(stok * harga_satuan), 0) AS total_nilai
    FROM bahan_makanan
");
if ($resultBahan) {
    $row = $resultBahan->fetch_assoc();
    $totalBahan = (int) $row["total"];
    $totalNilaiBahan = (float) $row["total_nilai"];
}

$resultOmprengan = $conn->query("
    SELECT 
        COUNT(*) AS total_barang,
        COALESCE(SUM(jumlah), 0) AS total_unit
    FROM omprengan
");
if ($resultOmprengan) {
    $row = $resultOmprengan->fetch_assoc();
    $totalBarangOmprengan = (int) $row["total_barang"];
    $totalOmprengan = (int) $row["total_unit"];
}

$resultLaporan = $conn->query("
    SELECT
        COUNT(*) AS total,
        COALESCE(SUM(jumlah_porsi), 0) AS total_porsi,
        COALESCE(SUM(jumlah_terdistribusi), 0) AS total_terdistribusi
    FROM laporan_operasional
");
if ($resultLaporan) {
    $row = $resultLaporan->fetch_assoc();
    $totalLaporan = (int) $row["total"];
    $totalPorsi = (int) $row["total_porsi"];
    $totalTerdistribusi = (int) $row["total_terdistribusi"];
}

$resultPengaduan = $conn->query("SELECT COUNT(*) AS total FROM pengaduan");
if ($resultPengaduan) {
    $row = $resultPengaduan->fetch_assoc();
    $totalPengaduan = (int) $row["total"];
}

$resultBaru = $conn->query("SELECT COUNT(*) AS total FROM pengaduan WHERE status = 'Baru'");
if ($resultBaru) {
    $row = $resultBaru->fetch_assoc();
    $pengaduanBaru = (int) $row["total"];
}

$resultDiproses = $conn->query("SELECT COUNT(*) AS total FROM pengaduan WHERE status = 'Diproses'");
if ($resultDiproses) {
    $row = $resultDiproses->fetch_assoc();
    $pengaduanDiproses = (int) $row["total"];
}

$resultSelesai = $conn->query("SELECT COUNT(*) AS total FROM pengaduan WHERE status = 'Selesai'");
if ($resultSelesai) {
    $row = $resultSelesai->fetch_assoc();
    $pengaduanSelesai = (int) $row["total"];
}

$persentaseDistribusi = 0;

if ($totalPorsi > 0) {
    $persentaseDistribusi = ($totalTerdistribusi / $totalPorsi) * 100;
}

response(true, "Ringkasan dashboard berhasil diambil", [
    "total_sppg" => $totalSppg,
    "total_sekolah" => $totalSekolah,
    "total_siswa" => $totalSiswa,
    "total_dana" => $totalDana,

    "total_karyawan" => $totalKaryawan,
    "total_gaji_karyawan" => $totalGajiKaryawan,

    "total_bahan" => $totalBahan,
    "total_nilai_bahan" => $totalNilaiBahan,

    "total_barang_omprengan" => $totalBarangOmprengan,
    "total_omprengan" => $totalOmprengan,

    "total_laporan" => $totalLaporan,
    "total_porsi" => $totalPorsi,
    "total_terdistribusi" => $totalTerdistribusi,
    "persentase_distribusi" => round($persentaseDistribusi, 2),

    "total_pengaduan" => $totalPengaduan,
    "pengaduan_baru" => $pengaduanBaru,
    "pengaduan_diproses" => $pengaduanDiproses,
    "pengaduan_selesai" => $pengaduanSelesai
]);

?>