<?php

namespace App\Services\Cartables\RoadPatrol;

use App\Facades\DataTable\DataTableFacade;
use App\Models\RoadPatrol;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OperatorService
{
    public function dataTable(Request $request, $loadRelations = false)
    {
        $query = RoadPatrol::query()
            ->where('operator_id', '=', auth()->user()->id)
            ->when($loadRelations, fn ($query) => $query->with(['rahdaran:id,name,code', 'cmmsMachines:id,machine_code,car_name,plak_number']));

        return DataTableFacade::run(
            $query,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedSelects: [
                'province_fa', 'province_id', 'id', 'edare_id', 'edare_name',
                'start_time', 'end_time', 'created_at', 'description',
                'distance', 'vehicle_runtime', 'fuel_consumption', 'stop_points'
            ],
        );
    }
}