<?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\Reports\RoadPatrolReportService;
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, RoadPatrolReportService $roadPatrolReportService): JsonResponse
    {
        $fromDate = $request->from_date ? $request->from_date . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
        $toDate = $request->date_to ? $request->date_to . ' 23:59:59' : now()->endOfDay()->toDateTimeString();

        $activities = $roadPatrolReportService->countryActivity($fromDate, $toDate);

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

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

    public function provincePatrolActivity(Request $request, RoadPatrolReportService $roadPatrolReportService): JsonResponse
    {
        $request->validate(["province_id" => "required",]);
        $fromDate = $request->from_date ? $request->from_date . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
        $toDate = $request->date_to ? $request->date_to . ' 23:59:59' : now()->endOfDay()->toDateTimeString();
        $provinceId = $request->province_id;

        $activities = $roadPatrolReportService->provinceActivity($fromDate, $toDate, $provinceId);

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

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

    public function countryActivityExcel(Request $request, RoadPatrolReportService $roadPatrolReportService): BinaryFileResponse
    {
        $activities = $roadPatrolReportService->countryActivity($request->from_date, $request->to_date);

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

        return Excel::download(new CountryActivity($activities), $name);
    }

    public function provinceActivityExcel(Request $request, RoadPatrolReportService $roadPatrolReportService): BinaryFileResponse
    {
        $activities = $roadPatrolReportService->provinceActivity($request->from_date, $request->to_date, $request->province_id);

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

        return Excel::download(new ProvinceActivity($activities), $name);
    }

    public function activitiesOnMap(Request $request): JsonResponse
    {
        $date_from = $request->date_from ? $request->date_from .' 00:00:00' : Date('Y-m-d') .' 00:00:00';
        $date_to = $request->date_to ? $request->date_to.' 23:59:59' : (new \DateTime('tomorrow'))->format('Y-m-d').' 23:59:59';

        $data = RoadPatrol::query()->when($request->province_id, function ($query, $province) {
            return $query->where('province_id', '=', $province);
        })
            ->when($request->edare_id, function ($query, $edare_id) {
                return $query->where('edare_id', '=', $edare_id);
            })
            ->whereBetween('created_at', [
                $date_from,
                $date_to,
            ])
            ->select('id', 'start_lat', 'start_lon')
            ->get();

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