<?php
// app/controllers/WalikotaController.php

class WalikotaController extends Controller {

    public function __construct() {
        parent::__construct();
        Auth::require('walikota');
    }

    public function dashboard(): void {
        $stats = $this->db->fetchOne("
            SELECT
                (SELECT COUNT(*) FROM warga_miskin WHERE status='aktif') as total_warga,
                (SELECT COUNT(*) FROM warga_miskin WHERE status='aktif' AND status_kemiskinan='Miskin Ekstrem') as ekstrem,
                (SELECT COUNT(*) FROM warga_miskin WHERE status='aktif' AND status_kemiskinan='Miskin') as miskin,
                (SELECT COUNT(*) FROM warga_miskin WHERE status='aktif' AND status_kemiskinan='Rentan Miskin') as rentan,
                (SELECT COUNT(*) FROM warga_miskin WHERE is_blind_spot=1 AND status='aktif') as blind_spot,
                (SELECT COUNT(*) FROM rumah_ibadah WHERE is_active=1) as total_ri,
                (SELECT COUNT(*) FROM histori_bantuan WHERE status='disalurkan') as total_bantuan,
                (SELECT COUNT(*) FROM histori_bantuan WHERE status='disalurkan' AND YEAR(tanggal_penyaluran)=YEAR(CURDATE())) as bantuan_tahun_ini,
                (SELECT COUNT(*) FROM laporan_masyarakat) as total_laporan,
                (SELECT COUNT(*) FROM laporan_masyarakat WHERE status='terverifikasi') as laporan_terverifikasi
        ");

        $perKecamatan = $this->db->fetchAll("
            SELECT kec.id, kec.nama,
                   COUNT(w.id) as total,
                   SUM(w.status_kemiskinan='Miskin Ekstrem') as ekstrem,
                   SUM(w.status_kemiskinan='Miskin') as miskin,
                   SUM(w.status_kemiskinan='Rentan Miskin') as rentan,
                   SUM(w.is_blind_spot) as blind_spot,
                   COUNT(DISTINCT ri.id) as total_ri
            FROM kecamatan kec
            LEFT JOIN kelurahan k ON k.id_kecamatan = kec.id
            LEFT JOIN warga_miskin w ON w.id_kelurahan = k.id AND w.status='aktif'
            LEFT JOIN rumah_ibadah ri ON ri.id_kelurahan = k.id AND ri.is_active=1
            GROUP BY kec.id, kec.nama
            ORDER BY total DESC
        ");

        $topRi = $this->db->fetchAll("
            SELECT ri.nama, ji.nama as jenis, kec.nama as kecamatan,
                   COUNT(DISTINCT wm.id) as jml_warga,
                   COUNT(DISTINCT hb.id) as jml_bantuan
            FROM rumah_ibadah ri
            JOIN jenis_rumah_ibadah ji ON ri.id_jenis = ji.id
            JOIN kelurahan k ON ri.id_kelurahan = k.id
            JOIN kecamatan kec ON k.id_kecamatan = kec.id
            LEFT JOIN warga_miskin wm ON wm.id_rumah_ibadah = ri.id AND wm.status='aktif'
            LEFT JOIN histori_bantuan hb ON hb.id_rumah_ibadah = ri.id AND hb.status='disalurkan'
            WHERE ri.is_active = 1
            GROUP BY ri.id, ri.nama, ji.nama, kec.nama
            ORDER BY jml_bantuan DESC LIMIT 5
        ");

        $trenTahunan = $this->db->fetchAll("
            SELECT YEAR(tanggal_penyaluran) as tahun, COUNT(*) as bantuan,
                   COUNT(DISTINCT id_warga) as warga_terbantu
            FROM histori_bantuan WHERE status='disalurkan'
            GROUP BY tahun ORDER BY tahun
        ");

        $trenBulanan = $this->db->fetchAll("
            SELECT DATE_FORMAT(tanggal_penyaluran,'%Y-%m') as bulan, COUNT(*) as jumlah
            FROM histori_bantuan WHERE status='disalurkan'
            AND tanggal_penyaluran >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
            GROUP BY bulan ORDER BY bulan
        ");

        $this->view('walikota.dashboard', [
            'title'        => 'Dashboard Eksekutif - Wali Kota',
            'stats'        => $stats,
            'perKecamatan' => $perKecamatan,
            'topRi'        => $topRi,
            'trenTahunan'  => $trenTahunan,
            'trenBulanan'  => $trenBulanan,
            'user'         => Auth::user()
        ], 'walikota');
    }

    public function apiStats(): void {
        $perKec = $this->db->fetchAll("
            SELECT kec.nama,
                   COUNT(w.id) as total,
                   SUM(w.status_kemiskinan='Miskin Ekstrem') as ekstrem,
                   SUM(w.is_blind_spot) as blind_spot
            FROM kecamatan kec
            LEFT JOIN kelurahan k ON k.id_kecamatan = kec.id
            LEFT JOIN warga_miskin w ON w.id_kelurahan = k.id AND w.status='aktif'
            GROUP BY kec.id, kec.nama
        ");
        $this->json(['perKecamatan' => $perKec]);
    }

    public function apiPeta(): void {
        $warga = $this->db->fetchAll("
            SELECT lat, lng, status_kemiskinan, is_blind_spot,
                   CASE status_kemiskinan
                       WHEN 'Miskin Ekstrem' THEN 1.0
                       WHEN 'Miskin' THEN 0.7
                       ELSE 0.4
                   END as intensity
            FROM warga_miskin WHERE status='aktif' AND lat != 0
        ");
        $ri = $this->db->fetchAll("
            SELECT ri.nama, ri.lat, ri.lng, ri.radius_meter, ji.nama as jenis
            FROM rumah_ibadah ri
            JOIN jenis_rumah_ibadah ji ON ri.id_jenis = ji.id
            WHERE ri.is_active = 1
        ");
        $this->json(['warga' => $warga, 'rumah_ibadah' => $ri]);
    }
}
