<?php

namespace App\Services\Cartables;

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

class RoadObservationTableService
{
    use ApiResponse;
    public function supervisorCartableIndex(Request $request)
    {
        $user = auth()->user();

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

        if ($user->hasPermissionTo('show-fast-react')) {
            $query = RoadObserved::query()->leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->whereNotNull('road_observeds.status')
                ->select($fields);
        }
        elseif ($user->hasPermissionTo('show-fast-react-province'))
        {
            if (is_null($user->province_id))
            {
                return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
            }

            $query = RoadObserved::query()->leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
                ->where('road_observeds.province_id', $user->province_id)
                ->whereNotNull('road_observeds.status')
                ->select($fields);
        }

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

    public function operatorCartableIndex(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)
             ->select($fields);

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

    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, 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')
                ->selectRaw($fields);
        }
        elseif ($user->hasPermissionTo('show-fast-react-province')) {

            if (is_null($user->province_id)) {
                return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
            }

            $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)
                ->selectRaw($fields);

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

            if (is_null($user->edarate_shahri_id)) {
                return $this->errorResponse('اداره ای برای شما در سامانه ثبت نشده است!');
            }

            $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)
                ->selectRaw($fields);

        }

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

    public function reportIndex(Request $request)
    {
        $fromDate = $request->from_date . ' 00:00:00';
        $toDate = $request->date_to . ' 23:59:59';

        $fields = [
            'fk_RegisteredEventMessage',
            'road_observeds.id',
            'Title',
            'road_observeds.created_at',
            'lat',
            'lng',
            'FeatureTypeTitle',
            'Description',
            'MobileForSendEventSms',
            'road_observeds.province_id',
            'province_fa',
            'city_fa',
            'edarate_shahri.name_fa',
            'rms_last_activity_fa',
            'rms_description'
        ];

        $query = RoadObserved::leftjoin('edarate_shahri', 'road_observeds.edarate_shahri_id', 'edarate_shahri.id')
            ->where('rms_status', "<>", 0)
            ->whereNotNull('edarate_shahri_id')
            ->whereBetween('road_observeds.created_at', [$fromDate, $toDate])
            ->select($fields);

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

        return [
            'data' => $data,
            'fromDate' => $fromDate,
            'toDate' => $toDate,
        ];
    }
}