<?php

namespace Modules\Lpj\Services;

use Modules\Lpj\Models\Laporan;
use Modules\Lpj\Models\Noc;
use Modules\Lpj\Models\Permohonan;
    use Illuminate\Support\Facades\DB;
class DashboardService
{
    public function getDashboardData($start_date, $end_date)
    {
        $countLpjInternal = $this->getTotalLpjInternal($start_date, $end_date);
        $countLpjExternal = $this->getTotalLpjEksternal($start_date, $end_date);
        $countResume = $this->getResumeCabang($start_date, $end_date);
        $countPendapatan = $this->getPendapatanAppraisal($start_date, $end_date);

        return [
            'count_lpj_internal' => $countLpjInternal,
            'count_lpj_eksternal' => $countLpjExternal,
            'count_resume' => $countResume,
            'count_pendapatan' => $countPendapatan
        ];

    }



    public function getTotalLpjInternal($start_date, $end_date)
    {
        $months = $this->getMonthly();
        $regions = $this->getRegion();
        $data = [];

        foreach ($months as $index => $month) {
            $monthNumber = $index + 1;
            $data[$month] = [];

            foreach ($regions as $region) {
                if ($monthNumber > now()->month) {

                    $data[$month][$region] = [
                        'total_laporan' => 0,
                        'total_debiture' => 0,
                    ];
                } else {

                    $totalLaporan = Laporan::whereBetween('created_at', [$start_date, $end_date])
                        ->count();
                    $totalDebitur = Permohonan::whereBetween('created_at', [$start_date, $end_date])
                    ->where('status', 'done')
                    ->distinct()
                    ->count('debiture_id');



                    $data[$month][$region] = [
                        'total_laporan' => $totalLaporan,
                        'total_debiture' => $totalDebitur,
                    ];
                }
            }
        }
        return $data;
    }



    public function getPendapatanAppraisal($start_date, $end_date)
{
    $months = $this->getMonthly();
    $data = [];

    foreach ($months as $index => $month) {
        $monthNumber = $index + 1;

        if ($monthNumber > now()->month) {
            // Bulan belum terjadi
            $data[$month] = [
                'total_jumlah' => 0,
                'total_akumulasi' => 0,
            ];
        } else {
            // Hitung jumlah nominal_bayar pada bulan ini
            $totalJumlah = Noc::whereYear('tanggal_pembayaran', now()->year)
                ->whereMonth('tanggal_pembayaran', $monthNumber)
                ->sum('nominal_bayar');


            $data[$month] = [
                'total_jumlah' => $totalJumlah,
                'total_akumulasi' => $totalJumlah,
            ];
        }
    }

    return $data;
}

    public function getResumeCabang($start_date, $end_date)
    {
        $regions = $this->getRegion();
        $status = ['batal', 'done'];
        $data = [];

        foreach ($status as $item) {
            $data[$item] = [];
            foreach ($regions as $region) {
                $totalLaporan =  DB::table('laporan')
                    ->join('permohonan', 'laporan.permohonan_id', '=', 'permohonan.id')
                    ->where('permohonan.status', $item)
                    ->whereBetween('laporan.created_at', [$start_date, $end_date])
                    ->count();

                $data[$item][$region] = [
                    'count_report' => $totalLaporan,
                ];
            }
        }

        return $data;

    }
    public function getTotalLpjEksternal($start_date, $end_date)
    {
        $months = $this->getMonthly();
        $data = [];
        foreach ($months as $index => $month) {
            $monthNumber = $index + 1;
            if ($monthNumber > now()->month) {
                $data[$month] = [
                    'total_laporan' => 0,
                    'total_debiture' => 0,
                ];
            } else {
                $totalLaporan = Laporan::where('created_at', '>=', $start_date)->where('created_at', '<=', $end_date)->count();

                $totalDebitur = Permohonan::whereBetween('created_at', [$start_date, $end_date])
                    ->where('status', 'done')
                    ->distinct()
                    ->count('debiture_id');

                $data[$month] = [
                    'total_laporan' => $totalLaporan,
                    'total_debiture' => $totalDebitur,
                ];
            }
        }
        return $data;


    }

    private function getMonthly()
    {
        return [
            'januari',
            'februari',
            'maret',
            'april',
            'mei',
            'juni',
            'juli',
            'agustus',
            'september',
            'oktober',
            'november',
            'desember',
        ];
    }

    private function getRegion()
    {
        return [
            'jabodetabek',
            'non-jabodetabek',
        ];
    }
}
