<?php

namespace App\Services\Cartables\RoadObservation;

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

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

        $fields = [
            'fk_RegisteredEventMessage', 'road_observeds.id', 'Title', 'road_observeds.created_at', 'StartTime_DateTime',
            'lat', 'lng', 'FeatureTypeTitle', 'Description', 'MobileForSendEventSms', 'edarate_shahri_id',
            'rms_description', 'rms_status', 'rms_start_latlng', 'rms_last_activity_fa', 'image_after', 'image_before',
            'rms_last_activity', 'road_observeds.province_id', 'province_fa', 'city_id', 'city_fa',
            'edarate_shahri.name_fa', 'status', 'status_fa', 'supervisor_description', 'supervising_time'
        ];

        $query = RoadObserved::query()
            ->where('rms_status', '!=', 0)
            ->whereNotNull('road_observeds.province_id')
            ->whereNotNull('status')
            ->leftJoin('edarate_shahri', 'road_observeds.edarate_shahri_id', '=', 'edarate_shahri.id')
            ->where('edarate_shahri_id', $user->edarate_shahri_id);

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

    /**
     * @throws Throwable
     */
    public function complaintsIndex(Request $request)
    {
        $user = auth()->user();

        $fields = ['fk_RegisteredEventMessage', 'road_observeds.id', 'Title', 'FeatureTypeTitle', 'MobileForSendEventSms', 'StartTime_DateTime',
              'road_observeds.created_at', 'rms_last_activity', 'rms_last_activity_fa', 'status', 'edarate_shahri_id',
              'status_fa', 'supervisor_description', 'rms_description', 'province_fa', 'city_id', 'city_fa', 'Description', 'road_observeds.province_id',
              'edarate_shahri.name_fa as edarate_shahri_name_fa', 'lat', 'lng'];

        if ($user->hasPermissionTo('show-fast-react')) {
            $query = RoadObserved::query()->leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('rms_status', '=', 0)
                ->whereNotNull('edarate_shahri_id');
        }
        elseif ($user->hasPermissionTo('show-fast-react-province')) {

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

            $query = RoadObserved::query()->leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('rms_status', '=', 0)
                ->whereNotNull('road_observeds.province_id')
                ->where('road_observeds.province_id', '=', $user->province_id);

        }
        elseif ($user->hasPermissionTo('show-fast-react-edarate-shahri')) {

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

            $query = RoadObserved::query()->leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('rms_status', '=', 0)
                ->whereNotNull('edarate_shahri_id')
                ->where('edarate_shahri_id', '=', $user->edarate_shahri_id);

        }

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