<?php

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

use App\Exports\V3\RoadPatrols\CountryActivity;
use App\Exports\V3\RoadPatrols\ProvinceActivity;
use App\Http\Controllers\Controller;
use App\Http\Traits\ApiResponse;
use App\Models\EdarateShahri;
use App\Models\Province;
use App\Models\RoadObserved;
use App\Models\RoadPatrol;
use App\Services\Cartables\RoadPatrolTableService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class RoadPatrolReportController extends Controller
{
    use ApiResponse;
    public function countryPatrolActivity(Request $request, RoadPatrolTableService $roadPatrolTableService): JsonResponse
    {
        $activities = $roadPatrolTableService->countryActivity($request);

        $provinces = Province::all(['id', 'name_fa']);

        return $this->successResponse([
            'activities' => $activities['data'],
            'provinces' => $provinces
        ]);
    }

    public function provincePatrolActivity(Request $request, RoadPatrolTableService $roadPatrolTableService): JsonResponse
    {
        $request->validate(["province_id" => "required",]);

        $activities = $roadPatrolTableService->provinceActivity($request);

        $edarateShahri = EdarateShahri::query()->where('province_id', '=', $request->province_id)->get(['id', 'name_fa']);

        return $this->successResponse([
            'activities' => $activities['data'],
            'edarateShahri' => $edarateShahri,
        ]);
    }

    public function countryActivityExcel(Request $request, RoadPatrolTableService $roadPatrolTableService): BinaryFileResponse
    {
        $activities = $roadPatrolTableService->countryActivity($request);
        $provinces = Province::all(['id', 'name_fa']);

        $name = 'گزارش از گشت راهداری و ترابری ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';

        return Excel::download(new CountryActivity([
            'activities' => $activities,
            'provinces' => $provinces,
        ]), $name);
    }

    public function provinceActivityExcel(Request $request, RoadPatrolTableService $roadPatrolTableService): BinaryFileResponse
    {
        $activities = $roadPatrolTableService->provinceActivity($request);
        $edarateShahri = EdarateShahri::query()->where('province_id', '=', $request->province_id)->get(['id', 'name_fa']);

        $name = 'گزارش از گشت راهداری و ترابری ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';

        return Excel::download(new ProvinceActivity([
            'activities' => $activities,
            'edarateShahri' => $edarateShahri,
        ]), $name);
    }

    public function activitiesOnMap(Request $request): JsonResponse
    {
        $date_from = $request->from_date ? $request->from_date .' 00:00:00' : now()->startOfDay()->toDateTimeString();
        $date_to = $request->date_to ? $request->date_to.' 23:59:59' : now()->addDay()->startOfDay()->toDateTimeString();
        $provinceId = $request->province_id;
        $edareId = $request->edare_id;

        $data = RoadPatrol::query()
            ->when($provinceId, fn($query, $provinceId) => $query->where('province_id', '=', $provinceId))
            ->when($edareId, fn($query, $edareId) => $query->where('edare_id', '=', $edareId))
            ->whereBetween('created_at', [$date_from, $date_to])
            ->select('id', 'start_lat', 'start_lon')
            ->get();

        return $this->successResponse($data);
    }

    public function showOnMap(RoadPatrol $roadPatrol): JsonResponse
    {
        return $this->successResponse($roadPatrol->load(['rahdaran:id,name,code', 'cmmsMachines:id,machine_code,car_name,plak_number']));
    }
}
