<?php

namespace App\Services\Cartables\Mission;


use App\Facades\DataTable\DataTableFacade;
use App\Http\Traits\ApiResponse;
use App\Models\Mission;
use Illuminate\Http\Request;

class RequestPortalService
{
    use ApiResponse;

    public function datatable(Request $request)
    {
        $user = auth()->user();
        $query = Mission::query();

        if ($user->hasPermissionTo('manage-request-portal-city')) {
            $query->where('edare_shahri_id', '=', $user->edarate_shahri_id);
        }
        elseif ($user->hasPermissionTo('manage-request-portal-province')) {
            $query->where('province_id', '=', $user->province_id);
        }

        return DataTableFacade::run(
            $query,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedSelects: [
                'id', 'user_id', 'username', 'state_name', 'province_name', 'edare_shahri_name',
                'description', 'requested_machines', 'type', 'type_fa', 'start_date', 'state_id',
                'end_date', 'request_date', 'end_point', 'area', 'zone', 'zone_fa', 'start_time', 'finish_time'
            ]
        );
    }
}