<?php

namespace App\Services\Cartables;

use App\Facades\DataTable\DataTableFacade;
use App\Http\Traits\ApiResponse;
use App\Models\Accident;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AccidentReceiptTableService
{
    use ApiResponse;

    public function dataTable(Request $request, $loadRelations = false)
    {
        $user = auth()->user();
        $query = null;

        if ($user->hasPermissionTo('show-receipt')) {
            $query = Accident::query()
                ->when($loadRelations, fn ($query) => $query->with('damages'));
        }
        else {
            if (is_null($user->province_id)) {
                return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
            }

            $query = Accident::query()
                ->where('province_id', '=', $user->province_id)
                ->when($loadRelations, fn ($query) => $query->with('damages'));
        }

        return DataTableFacade::run(
            $query,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*']
        );
    }

    public function countryReport(Request $request): array
    {
        $data = DB::select(
            'SELECT 
            "کشوری" as province_fa,
            -1 as province_id,
            COUNT(id) AS tedade, 
            COUNT(CASE WHEN report_base = 0 THEN 1 END) AS police_rah,
            COUNT(CASE WHEN report_base = 1 THEN 1 END) AS gozaresh_gasht, 
            SUM(`sum`) AS kol_sabt_shode,
            SUM(CASE WHEN STATUS > 3 THEN `sum` END) AS vasel_shode,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_insurance_amount` END) AS vasel_shode_bimeh,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_daghi_amount` END) AS vasel_shode_daghi,
            SUM(CASE WHEN STATUS > 3 THEN `final_amount` END) AS vasel_shode_final
            FROM `accidents` 
            where created_at BETWEEN "'.$request->from_date.'" AND "'.$request->date_to.'"
            union
            SELECT 
            province_fa,
            province_id,
            COUNT(id) AS tedade, 
            COUNT(CASE WHEN report_base = 0 THEN 1 END) AS police_rah,
            COUNT(CASE WHEN report_base = 1 THEN 1 END) AS gozaresh_gasht, 
            SUM(`sum`) AS kol_sabt_shode,
            SUM(CASE WHEN STATUS > 3 THEN `sum` END) AS vasel_shode,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_insurance_amount` END) AS vasel_shode_bimeh,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_daghi_amount` END) AS vasel_shode_daghi,
            SUM(CASE WHEN STATUS > 3 THEN `final_amount` END) AS vasel_shode_final
            FROM `accidents` 
            where created_at BETWEEN "'.$request->from_date.'" AND "'.$request->date_to.'"
            GROUP BY province_id
            '
        );

        return [
            'data' => $data,
            'fromDate' => $request->from_date,
            'toDate' => $request->date_to,
        ];
    }

    public function provinceReport(Request $request): array
    {
        $data =  DB::select(
            'SELECT 
            "استانی" as city_fa,
            -1 as city_id,
            COUNT(id) AS tedade, 
            COUNT(CASE WHEN report_base = 0 THEN 1 END) AS police_rah,
            COUNT(CASE WHEN report_base = 1 THEN 1 END) AS gozaresh_gasht, 
            SUM(`sum`) AS kol_sabt_shode,
            SUM(CASE WHEN STATUS > 3 THEN `sum` END) AS vasel_shode,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_insurance_amount` END) AS vasel_shode_bimeh,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_daghi_amount` END) AS vasel_shode_daghi,
            SUM(CASE WHEN STATUS > 3 THEN `final_amount` END) AS vasel_shode_final
            FROM `accidents` 
            where created_at BETWEEN "'.$request->from_date.'" AND "'.$request->date_to.'" AND province_id ='.$request->province_id.'
            union
            SELECT 
            city_fa,
            city_id,
            COUNT(id) AS tedade, 
            COUNT(CASE WHEN report_base = 0 THEN 1 END) AS police_rah,
            COUNT(CASE WHEN report_base = 1 THEN 1 END) AS gozaresh_gasht, 
            SUM(`sum`) AS kol_sabt_shode,
            SUM(CASE WHEN STATUS > 3 THEN `sum` END) AS vasel_shode,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_insurance_amount` END) AS vasel_shode_bimeh,
            SUM(CASE WHEN STATUS > 3 THEN `deposit_daghi_amount` END) AS vasel_shode_daghi,
            SUM(CASE WHEN STATUS > 3 THEN `final_amount` END) AS vasel_shode_final
            FROM `accidents` 
            where created_at BETWEEN "'.$request->from_date.'" AND "'.$request->date_to.'" AND province_id ='.$request->province_id.'
            GROUP BY city_id
            '
        );

        return [
            'data' => $data,
            'fromDate' => $request->from_date,
            'toDate' => $request->date_to,
        ];
    }
}
