<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BigdataResume extends Model
{
    protected $table = "bigdata_resumes";

    protected $fillable = [
        'import_datasource_id',
        'potention_count',
        'potention_sum',
        'non_verified_count',
        'non_verified_sum',
        'verified_count',
        'verified_sum',
        'business_count',
        'business_sum',
        'non_business_count',
        'non_business_sum',
    ];

    public function importDatasource()
    {
        return $this->belongsTo(ImportDatasource::class, 'import_datasource_id');
    }

    public static function generateResumeData($import_datasource_id){
        $query_verified = 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')
                ->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();
        }); 

        $verified_count = $query_verified->total_data ?? 0;
        $verified_total = $query_verified->total_retribution ?? 0;

        $query_business = 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();
        });

        $business_count = $query_business->total_data ?? 0;
        $business_total = $query_business->total_retribution ?? 0;

        $query_non_business = 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();
        });
        $non_business_count = $query_non_business->total_data ?? 0;
        $non_business_total = $query_non_business->total_retribution ?? 0;

        $query_non_verified = 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();
        });

        $non_verified_count = $query_non_verified->total_data ?? 0;
        $non_verified_total = $query_non_verified->total_retribution ?? 0;

        $query_potention = once( function () {
            return DB::table('pbg_task as pt')
            ->leftJoin('pbg_task_retributions as ptr', 'pt.uuid', '=', 'ptr.pbg_task_uid')
            ->select(
                DB::raw('COUNT(DISTINCT pt.id) as task_count'),
                DB::raw('SUM(ptr.nilai_retribusi_bangunan) as total_retribution')
            )
            ->first();
        });
        $potention_count = $query_potention->task_count ?? 0;
        $potention_total = $query_potention->total_retribution ?? 0;

        return self::create([
            'import_datasource_id' => $import_datasource_id,
            'potention_count' => $potention_count ?? 0,
            'potention_sum' => $potention_total ?? 0.00,
            'non_verified_count' => $non_verified_count ?? 0,
            'non_verified_sum' => $non_verified_total ?? 0.00,
            'verified_count' => $verified_count ?? 0,
            'verified_sum' => $verified_total ?? 0.00,
            'business_count' => $business_count ?? 0,
            'business_sum' => $business_total ?? 0.00,
            'non_business_count' => $non_business_count ?? 0,
            'non_business_sum' => $non_business_total ?? 0.00,
        ]);
    }
}
