<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Traits\GlobalApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    use GlobalApiResponse;

    public function businnessDocument(Request $request){
        $query = once(function () {
            return DB::table('pbg_task AS pt')
                ->leftJoin('pbg_task_google_sheet AS ptgs', 'pt.registration_number', '=', 'ptgs.no_registrasi')
                ->leftJoin('pbg_task_retributions AS ptr', 'pt.uuid', '=', 'ptr.pbg_task_uid')
                ->where(function ($query) {
                    $query->whereRaw('LOWER(TRIM(ptgs.status_verifikasi)) != ?', [strtolower(trim('Selesai Verifikasi'))])
                        ->orWhereNull('ptgs.status_verifikasi');
                })
                ->where(function ($query) {
                    $query->whereRaw('LOWER(TRIM(pt.function_type)) = ?', [strtolower(trim('Sebagai Tempat Usaha'))]);
                })
                ->selectRaw('COUNT(pt.id) AS total_data, 
                            SUM(ptr.nilai_retribusi_bangunan) AS total_retribution')
                ->first();
        });

        $taskCount = $query->total_data ?? 0;
        $taskTotal = $query->total_retribution ?? 0;

        return $this->resSuccess([
            "count" => $taskCount,
            "total" => $taskTotal
        ]);
    }
    public function nonBusinnessDocument(Request $request){

        $query = once( function () {
            return DB::table('pbg_task AS pt')
            ->leftJoin('pbg_task_google_sheet AS ptgs', 'pt.registration_number', '=', 'ptgs.no_registrasi')
            ->leftJoin('pbg_task_retributions AS ptr', 'pt.uuid', '=', 'ptr.pbg_task_uid') // Join ke pbg_task_retributions
            ->where(function ($query) {
                $query->whereRaw('LOWER(TRIM(ptgs.status_verifikasi)) != ?', [strtolower(trim('Selesai Verifikasi'))])
                    ->orWhereNull('ptgs.status_verifikasi'); // Include NULL values
            })
            ->where(function ($query) {
                $query->whereRaw('LOWER(TRIM(pt.function_type)) != ?', [strtolower(trim('Sebagai Tempat Usaha'))])
                    ->orWhereNull('pt.function_type'); // Include NULL values
            })
            ->selectRaw('COUNT(pt.id) AS total_data, 
                        SUM(ptr.nilai_retribusi_bangunan) AS total_retribution') // Menambahkan SUM dari pbg_task_retributions
            ->first();
        });
        $taskCount = $query->total_data ?? 0;
        $taskTotal = $query->total_retribution ?? 0;
        return $this->resSuccess([
            "count" => $taskCount,
            "total" => $taskTotal
        ]);
    }
    public function allTaskDocuments(){
        $query = once( function () {
            return DB::table('pbg_task')
            ->leftJoin('pbg_task_retributions', 'pbg_task.uuid', '=', 'pbg_task_retributions.pbg_task_uid')
            ->select(
                DB::raw('COUNT(DISTINCT pbg_task.id) as task_count'),
                DB::raw('SUM(pbg_task_retributions.nilai_retribusi_bangunan) as total_retribution')
            )
            ->first();
        });
        $taskCount = $query->task_count ?? 0;
        $taskTotal = $query->total_retribution ?? 0;
        return $this->resSuccess([
            "count" => $taskCount,
            "total" => $taskTotal
        ]);
    }

    public function verificationDocuments(){
        $query = once( function (){
            return DB::table('pbg_task AS pt')
                ->leftJoin('pbg_task_google_sheet AS ptgs', 'pt.registration_number', '=', 'ptgs.no_registrasi')
                ->leftJoin('pbg_task_retributions AS ptr', 'pt.uuid', '=', 'ptr.pbg_task_uid') // Menambahkan join ke pbg_task_retributions
                ->whereRaw('LOWER(TRIM(ptgs.status_verifikasi)) = ?', [strtolower(trim('Selesai Verifikasi'))])
                ->selectRaw('COUNT(pt.id) AS total_data, 
                            SUM(ptr.nilai_retribusi_bangunan) AS total_retribution') 
                ->first();
        }); 

        $taskCount = $query->total_data ?? 0;
        $taskTotal = $query->total_retribution ?? 0;

        return $this->resSuccess([
            "count"=> $taskCount,
            "total"=> $taskTotal
        ]);
    }

    public function nonVerificationDocuments(){
        $query = once(function () {
            return DB::table('pbg_task AS pt')
            ->leftJoin('pbg_task_google_sheet AS ptgs', 'pt.registration_number', '=', 'ptgs.no_registrasi')
            ->leftJoin('pbg_task_retributions AS ptr', 'pt.uuid', '=', 'ptr.pbg_task_uid') // Join tabel pbg_task_retributions
            ->where(function ($query) {
                $query->whereRaw('LOWER(TRIM(ptgs.status_verifikasi)) != ?', [strtolower(trim('Selesai Verifikasi'))])
                    ->orWhereNull('ptgs.status_verifikasi'); // Include NULL values
            })
            ->selectRaw('COUNT(pt.id) AS total_data, 
                        SUM(ptr.nilai_retribusi_bangunan) AS total_retribution') // Menambahkan SUM dari pbg_task_retributions
            ->first();
        });

        $taskCount = $query->total_data ?? 0;
        $taskTotal = $query->total_retribution ?? 0;

        return $this->resSuccess([
            "count"=> $taskCount,
            "total"=> $taskTotal
        ]);
    }

    public function pbgTaskDocuments(Request $request){
        $request->validate([
            'status' => 'required|string'
        ]);
        
        $businessData = DB::table('pbg_task')
        ->leftJoin('pbg_task_retributions', 'pbg_task.uuid', '=', 'pbg_task_retributions.pbg_task_uid')
        ->select(
            DB::raw('COUNT(DISTINCT pbg_task.id) as task_count'),
            DB::raw('SUM(pbg_task_retributions.nilai_retribusi_bangunan) as total_retribution')
        )
        ->where(function ($query) use ($request) {
            $query->where("pbg_task.status", "=", $request->get('status'));
        })
        ->first();
        $taskCount = $businessData->task_count;
        $taskTotal = $businessData->total_retribution;
        $result = [
            "count" => $taskCount,
            "series" => [$taskCount],
            "total" => $taskTotal
        ];
        return $this->resSuccess($result);
    }
}
