<?php

namespace App\Controllers;

class Home extends BaseController
{
    protected $db;

    public function __construct()
    {
        $this->db = \Config\Database::connect();
        date_default_timezone_set('Asia/Jakarta');
    }

    public function index(): string
    {
        $level = session()->get('level');
        $id_opd_admin = session()->get('id_opd');
        $tanggal_hari_ini = date('Y-m-d');
        $akses_global = ['Superadmin', 'kabupaten', 'Administrator'];
        $hari_ini = date('Y-m-d');

        // --- 1. QUERY DATA ---
        $builder = $this->db->table('presensi')
            ->select('presensi.*, pegawai.id_opd')
            ->join('pegawai', 'presensi.nip = pegawai.nip', 'left')
            ->where('presensi.tanggal', $tanggal_hari_ini);

        if (!in_array($level, $akses_global)) {
            $builder->where('pegawai.id_opd', $id_opd_admin);
            $data['nama_kantor'] = session()->get('nama_opd') ?? 'Instansi Anda';
        } else {
            $data['nama_kantor'] = "Kabupaten Polewali Mandar";
        }

        $all_stats = $builder->get()->getResultArray();

        // --- 2. LOGIKA HITUNG KATEGORI BERDASARKAN DATABASE DINAMIS ---
        $total_hadir = 0;
        $total_izin = 0;
        $total_cepat = 0; 
        
        $cat_istimewa = 0; 
        $cat_baik     = 0; 
        $cat_cukup    = 0; 
        $cat_telat    = 0; 
        $cat_bina     = 0;
        $cat_teguran  = 0;

        foreach ($all_stats as $row) {
            // Hitung Izin/Sakit/Cuti
            if (stripos($row['kategori'] ?? '', 'Izin') !== false || stripos($row['kategori'] ?? '', 'Sakit') !== false || stripos($row['kategori'] ?? '', 'Cuti') !== false || stripos($row['kategori'] ?? '', 'Luar') !== false) {
                $total_izin++;
                continue;
            }

            if (ucwords(strtolower($row['status_pulang'] ?? '')) == 'Cepat Pulang') {
                $total_cepat++;
            }

            $jam_masuk = $row['jam_masuk_aktual'];
            
            if ($jam_masuk != null && $jam_masuk != '00:00:00') {
                $total_hadir++; 
                $status_db = $row['status_masuk'] ?? '';

                // Mapping Langsung Berdasarkan Teks di Database
                if ($status_db == 'Sangat Baik / Istimewa' || $status_db == 'Sangat Baik' || strpos($status_db, 'Apel') !== false) {
                    $cat_istimewa++;
                } elseif ($status_db == 'Baik') {
                    $cat_baik++;
                } elseif ($status_db == 'Cukup Baik') {
                    $cat_cukup++;
                } elseif ($status_db == 'Terlambat') {
                    $cat_telat++;
                } elseif ($status_db == 'Pembinaan') {
                    $cat_bina++;
                } elseif ($status_db == 'Teguran') {
                    $cat_teguran++;
                } else {
                    // Fallback jika statusnya "Tepat Waktu" (Inputan Manual Admin)
                    $cat_baik++;
                }
            }
        }

        // --- 3. OPD PERFORMANCE ---
        $opd_stats = [];
        if (in_array($level, $akses_global)) {
            $opd_list = $this->db->table('opd')->get()->getResultArray();
            foreach ($opd_list as $o) {
                $total_peg_opd = $this->db->table('pegawai')->where('id_opd', $o['id_opd'])->countAllResults();
                if ($total_peg_opd > 0) {
                    $hadir_opd = $this->db->table('presensi')->join('pegawai', 'presensi.nip = pegawai.nip')
                        ->where(['presensi.tanggal' => $tanggal_hari_ini, 'pegawai.id_opd' => $o['id_opd']])
                        ->where('presensi.jam_masuk_aktual !=', null)->countAllResults();
                    $persen = round(($hadir_opd / $total_peg_opd) * 100);
                    $opd_stats[] = ['nama' => $o['nama_opd'], 'persen' => $persen];
                }
            }
            usort($opd_stats, fn($a, $b) => $b['persen'] <=> $a['persen']);
        }

        $p_builder = $this->db->table('pegawai');
        if (!in_array($level, $akses_global)) $p_builder->where('id_opd', $id_opd_admin);
        $total_pegawai = $p_builder->countAllResults();
        $belum_absen = max(0, $total_pegawai - ($total_hadir + $total_izin));

        // --- 4. KIRIM DATA KE VIEW ---
        $data['total_pegawai'] = $total_pegawai;
        $data['total_hadir']   = $total_hadir;
        $data['total_telat']   = $cat_telat + $cat_bina + $cat_teguran;
        $data['total_izin']    = $total_izin;
        $data['total_cepat']   = $total_cepat;
        $data['opd_performance'] = array_slice($opd_stats, 0, 50);
        
        // Kirim Array Lengkap Untuk Pie Chart
        $data['chart_data'] = [$cat_istimewa, $cat_baik, $cat_cukup, $cat_telat, $cat_bina, $cat_teguran, $total_izin, $belum_absen];
        
        // Kirim Detail Variabel Kategori untuk Widget Card
        $data['cat_istimewa'] = $cat_istimewa;
        $data['cat_baik']     = $cat_baik;
        $data['cat_cukup']    = $cat_cukup;
        $data['cat_telat']    = $cat_telat;
        $data['cat_bina']     = $cat_bina;
        $data['cat_teguran']  = $cat_teguran;
        $data['belum_absen']  = $belum_absen;

        return view('dashboard', $data);
    }

    // --- 5. DATA TABLES (AJAX) ---
    public function ajax_aktivitas()
    {
        $request = \Config\Services::request();
        $level = session()->get('level');
        $id_opd_admin = session()->get('id_opd');
        $tanggal_hari_ini = date('Y-m-d');
        $akses_global = ['Superadmin', 'kabupaten', 'Administrator'];

        $builder = $this->db->table('presensi')
            ->select('presensi.*, pegawai.nama_lengkap, opd.nama_opd')
            ->join('pegawai', 'presensi.nip = pegawai.nip', 'left')
            ->join('opd', 'pegawai.id_opd = opd.id_opd', 'left')
            ->where('presensi.tanggal', $tanggal_hari_ini);

        if (!in_array($level, $akses_global)) {
            $builder->where('pegawai.id_opd', $id_opd_admin);
        }

        $searchValue = $request->getPost('search')['value'] ?? '';
        if ($searchValue) {
            $builder->groupStart()
                ->like('pegawai.nama_lengkap', $searchValue)
                ->orLike('pegawai.nip', $searchValue)
                ->groupEnd();
        }

        $totalFiltered = $builder->countAllResults(false);
        
        // Sorting limit
        $length = $request->getPost('length') ?? 10;
        $start = $request->getPost('start') ?? 0;
        
        $result = $builder->orderBy('presensi.jam_masuk_aktual', 'DESC')
            ->limit($length, $start)
            ->get()->getResultArray();

        $data = [];
        foreach ($result as $row) {
            $nestedData = [];
            
            // 1. Profil Pegawai
            $avatar = "https://ui-avatars.com/api/?name=" . urlencode($row['nama_lengkap']) . "&background=random&color=fff&bold=true";
            $nestedData[] = '<div class="d-flex align-items-center text-start ps-3">
                                <img src="' . $avatar . '" class="avatar-circle me-3">
                                <div>
                                    <div class="fw-bold text-dark mb-0">' . $row['nama_lengkap'] . '</div>
                                    <div class="text-muted small" style="font-size: 0.7rem;">' . $row['nama_opd'] . '</div>
                                </div>
                             </div>';

            // LOGIKA PENYEMBUNYIAN JAM UNTUK IZIN/SAKIT/TL/CUTI
            $is_absen_khusus = ($row['kategori'] && $row['kategori'] != 'Hadir' && strpos($row['kategori'], 'Apel') === false);

            if ($is_absen_khusus) {
                // KOSONGKAN JAM
                $nestedData[] = '<span class="text-muted">--:--</span>'; // Jam Masuk
                $nestedData[] = '<span class="text-muted">--:--</span>'; // Jam Siang
                $nestedData[] = '<span class="text-muted">--:--</span>'; // Jam Pulang
                
                // STATUS MASUK = NAMA KATEGORI
                $nestedData[] = '<span class="badge bg-izin rounded-pill px-3">' . strtoupper($row['kategori']) . '</span>';
                
                // STATUS PULANG = STRIP
                $nestedData[] = '<span class="badge bg-light text-muted border">-</span>';
            } else {
                // JIKA HADIR NORMAL / APEL: TAMPILKAN JAM
                $nestedData[] = ($row['jam_masuk_aktual'] ? date('H:i', strtotime($row['jam_masuk_aktual'])) : '<span class="text-muted">--:--</span>');
                $nestedData[] = ($row['jam_siang'] ? date('H:i', strtotime($row['jam_siang'])) : '<span class="text-muted">--:--</span>');
                $nestedData[] = ($row['jam_pulang_aktual'] ? date('H:i', strtotime($row['jam_pulang_aktual'])) : '<span class="text-muted">--:--</span>');

                // Status Masuk (Membaca Teks Dari Database)
                $jamMasuk = $row['jam_masuk_aktual'];
                if (!$jamMasuk) {
                    $badgeMasuk = '<span class="badge bg-light text-muted border">Belum Absen</span>';
                } else {
                    $status_db = $row['status_masuk'] ?? '';
                    
                    if ($status_db == 'Sangat Baik / Istimewa' || $status_db == 'Sangat Baik') {
                        $badgeMasuk = '<span class="badge bg-istimewa rounded-pill px-3">' . $status_db . '</span>';
                    } elseif ($status_db == 'Baik') {
                        $badgeMasuk = '<span class="badge bg-baik rounded-pill px-3">' . $status_db . '</span>';
                    } elseif ($status_db == 'Cukup Baik') {
                        $badgeMasuk = '<span class="badge bg-cukup text-dark rounded-pill px-3">' . $status_db . '</span>';
                    } elseif ($status_db == 'Terlambat') {
                        $badgeMasuk = '<span class="badge bg-telat text-dark rounded-pill px-3">' . $status_db . '</span>';
                    } elseif ($status_db == 'Pembinaan') {
                        $badgeMasuk = '<span class="badge bg-bina text-dark rounded-pill px-3">' . $status_db . '</span>';
                    } elseif ($status_db == 'Teguran') {
                        $badgeMasuk = '<span class="badge bg-teguran rounded-pill px-3">' . $status_db . '</span>';
                    } elseif (strpos($status_db, 'Apel') !== false) {
                        $badgeMasuk = '<span class="badge bg-primary rounded-pill px-3">' . $status_db . '</span>';
                    } else {
                        $badgeMasuk = '<span class="badge bg-secondary rounded-pill px-3">' . ($status_db ?: 'Hadir') . '</span>';
                    }
                }
                $nestedData[] = $badgeMasuk;

                // Status Pulang (Cepat Pulang / Selesai)
                $statusPulangDB = ucwords(strtolower($row['status_pulang'] ?? ''));
                if ($statusPulangDB == 'Cepat Pulang') {
                    $badgePulang = '<span class="badge bg-cepul rounded-pill px-3">Cepat Pulang</span>';
                } elseif ($statusPulangDB == 'Selesai') {
                    $badgePulang = '<span class="badge bg-selesai rounded-pill px-3">Selesai</span>';
                } else {
                    $badgePulang = '<span class="badge bg-light text-muted border">-</span>';
                }
                $nestedData[] = $badgePulang;
            }

            $data[] = $nestedData;
        }

        return $this->response->setJSON([
            "draw" => intval($request->getPost('draw') ?? 1),
            "recordsTotal" => $totalFiltered,
            "recordsFiltered" => $totalFiltered,
            "data" => $data
        ]);
    }
}