<?php

namespace App\Http\Controllers\V3\Dashboard\RoadObservation;

use App\Exports\V2\RoadObservation\ReportComplaintsExport;
use App\Facades\DataTable\DataTableFacade;
use App\Http\Traits\ApiResponse;
use App\Models\RoadObserved;
use App\Services\Cartables\RoadObservation\ReportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ReportController
{
    use ApiResponse;

    public function index(Request $request, ReportService $reportService): JsonResponse
    {
        $data = $reportService->index($request);
        return response()->json($data['data']);
    }

    public function excel(Request $request, ReportService $reportService): BinaryFileResponse
    {
        $name = 'گزارش رسیدگی به شکایات واکنش سریع ' . verta()->now()->format('Y-m-d H:i') . '.xlsx';
        $data = $reportService->index($request);
        return Excel::download(new ReportComplaintsExport($data), $name);
    }

    public function map(Request $request): JsonResponse
    {
        return response()->json(DataTableFacade::run(
            RoadObserved::query(),
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedSelects: ['id', 'lat', 'lng', 'status']
        ));
    }
}