<?php

namespace App\Services\Cartables\SafetyAndPrivacy;

use App\Exceptions\ProhibitedAction;
use App\Facades\DataTable\DataTableFacade;
use App\Models\SafetyAndPrivacy;
use Illuminate\Http\Request;

class OperatorService
{
    public function dataTable(Request $request)
    {
        $user = auth()->user();

        $fields = [
            'id','lat','lon', 'recognize_picture','action_picture','created_at','step', 'final_description',
            'info_fa', 'activity_date_time', 'action_picture_document_upload_date', 'judiciary_document_upload_date',
            'step_fa', 'axis_type_id', 'axis_type_name', 'action_date', 'is_finished', 'status_fa', 'supervisor_description', 'operator_description'
        ];

        throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));

        $query = SafetyAndPrivacy::query()->where('edare_shahri_id', '=', $user->edarate_shahri_id);

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

    public function countryActivity(Request $request): array
    {
        $activities = [];
        $countryQuery = SafetyAndPrivacy::query()->selectRaw('COUNT(*) as qty,info_id, step');

        $country = DataTableFacade::run(
            $countryQuery,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedGroupBy: ['info_id', 'step']
        );

        $countryTotalSum = 0;
        $countryData = [
            'name_fa' => 'کشوری',
            'province_id' => -1,
            'sum' => 0,
            '89' => ['1' => 0, '2' => 0, '3' => 0,],
            '90' => ['1' => 0, '2' => 0, '3' => 0,],
            '91' => ['1' => 0, '2' => 0, '3' => 0,],
        ];
        foreach ($country['data'] as $key => $value) {
            $countryTotalSum += $value->qty;
            $countryData[$value->info_id][$value->step] = $value->qty;
            $countryData["sum"] = $countryTotalSum;
        }

        $activities[] = (object) $countryData;
        $provinceQuery = SafetyAndPrivacy::query()->selectRaw('province_id, province_fa, COUNT(*) as qty,info_id, step');
        $provinces = DataTableFacade::run(
            $provinceQuery,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedGroupBy: ['info_id', 'step', 'province_id', 'province_fa']
        );

        foreach ($provinces['data'] as $value)
        {
            $provinceId = $value->province_id;
            if (!isset($activities[$provinceId])) {
                $activities[$provinceId] = (object) [
                    'province_id' => $provinceId,
                    'name_fa' => $value->province_fa,
                    'sum' => 0,
                    '89' => ['1' => 0, '2' => 0, '3' => 0],
                    '90' => ['1' => 0, '2' => 0, '3' => 0],
                    '91' => ['1' => 0, '2' => 0, '3' => 0],
                ];
            }
            $activities[$provinceId]->{$value->info_id}[$value->step] = $value->qty;
            $activities[$provinceId]->sum += $value->qty;
        }

        return array_values($activities);
    }

    public function provinceActivity(Request $request): array
    {
        $activities = [];
        $provinceQuery = SafetyAndPrivacy::query()->selectRaw('COUNT(*) as qty,info_id, step');
        $province = DataTableFacade::run(
            $provinceQuery,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedGroupBy: ['info_id', 'step']
        );

        $provinceTotalSum = 0;
        $provinceData = [
            'name_fa' => 'استانی',
            'edare_shahri_id' => -1,
            'sum' => 0,
            '89' => ['1' => 0, '2' => 0, '3' => 0,],
            '90' => ['1' => 0, '2' => 0, '3' => 0,],
            '91' => ['1' => 0, '2' => 0, '3' => 0,],
        ];
        foreach ($province['data'] as $key => $value) {
            $provinceTotalSum += $value->qty;
            $provinceData["sum"] = $provinceTotalSum;
            $provinceData[$value->info_id][$value->step] = $value->qty;
        }
        $activities[] = (object) $provinceData;

        $citiesQuery = SafetyAndPrivacy::query()->selectRaw('edare_shahri_id, COUNT(*) as qty,info_id, step');
        $cities = DataTableFacade::run(
            $citiesQuery,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedGroupBy: ['info_id', 'step', 'edare_shahri_id']
        );

        foreach ($cities['data'] as $value)
        {
            $cityId = $value->edare_shahri_id;
            if (!isset($activities[$cityId])) {
                $activities[$cityId] = (object) [
                    'edare_shahri_id' => $cityId,
                    'name_fa' => \App\Models\EdarateShahri::query()->find($cityId)->name_fa,
                    'sum' => 0,
                    '89' => ['1' => 0, '2' => 0, '3' => 0],
                    '90' => ['1' => 0, '2' => 0, '3' => 0],
                    '91' => ['1' => 0, '2' => 0, '3' => 0],
                ];
            }
            $activities[$cityId]->{$value->info_id}[$value->step] = $value->qty;
            $activities[$cityId]->sum += $value->qty;
        }

        return array_values($activities);
    }
}