<?php

namespace App\Services\Cartables\RoadObservation;

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

class ReportService
{
    public function index(Request $request): array
    {
        $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,
        ];
    }
}