<?php

namespace App\Controllers;

use App\Models\ModelDashboard;
use App\Models\ModelPengajuanCuti;
use App\Models\ModelUser;
use CodeIgniter\Controller;

class Dashboard extends Controller
{
    protected $ModelDashboard;

    public function __construct()
    {
        $this->ModelDashboard = new ModelDashboard();
    }

    public function index()
    {
        return view('dashboard');
    }

    // Endpoint untuk mengambil data dashboard secara lengkap
    // public function getDashboardData()
    // {

    //     // Mengambil data jumlah balita dan ibu hamil berdasarkan bulan
    //     $monthlycustomerData = $this->ModelDashboard->getMonthlyData('customer');
    //     $monthlybookingsData = $this->ModelDashboard->getMonthlyData('bookings');
    //     $monthlyserviceData = $this->ModelDashboard->getMonthlyData('services');

    //     // Mengambil data statistik untuk customer, ibu hamil, dan service
    //     $customerData = $this->ModelDashboard->getStatistics('customer');
    //     $bookingsData = $this->ModelDashboard->getStatistics('bookings');
    //     $serviceData = $this->ModelDashboard->getStatistics('services');

    //     // Menyiapkan respons
    //     $response = [
    //         'success' => true,
    //         'data' => [
    //             'customer' => $customerData,
    //             'bookings' => $bookingsData,
    //             'service' => $serviceData,
    //             'monthlycustomer' => $monthlycustomerData,
    //             'monthlybookings' => $monthlybookingsData,
    //             'monthlyservice' => $monthlyserviceData
    //         ]
    //     ];

    //     // Mengembalikan data dalam format JSON
    //     return $this->response->setJSON($response);
    // }

    public function getDashboardData()
    {
        $modelUser = new ModelUser();
        $modelCuti = new ModelPengajuanCuti();

        // Jumlah Pegawai
        $pegawaiCount = $modelUser->countAllResults();

        // Jumlah Pengajuan
        $cutiTotal = $modelCuti->countAll();

        // Disetujui
        $cutiDisetujui = $modelCuti->where('status_supervisor', 'disetujui')
            ->where('status_manager', 'disetujui')
            ->countAllResults();

        // Statistik Bulanan
        $monthly = $modelCuti->select("MONTH(created_at) as month, COUNT(*) as total")
            ->groupBy("MONTH(created_at)")
            ->findAll();

        return $this->response->setJSON([
            'success' => true,
            'data' => [
                'pegawai' => ['total' => $pegawaiCount],
                'cuti' => ['total' => $cutiTotal],
                'disetujui' => ['total' => $cutiDisetujui],
                'monthly' => $monthly
            ]
        ]);
    }
}
