<?php

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

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\RoadPatrol;
use App\Services\Cartables\RoadPatrol\ReportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ReportController extends Controller
{
    use ApiResponse;
    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']));
    }

    public function getReport(RoadPatrol $roadPatrol): JsonResponse
    {
        $data = [
            'serial_no' => $roadPatrol->id,
            'province_fa' => $roadPatrol->province_fa,
            'edare_name' => $roadPatrol->edare_name,
            'start_time' => $roadPatrol->start_time,
            'end_time' => $roadPatrol->end_time,
            'officer_name' => $roadPatrol->officer_name,
            'officer_plaque' => $roadPatrol->officer_plaque,
            'officer_phone_number' => $roadPatrol->officer_phone_number,
            'created_at' => $roadPatrol->created_at
        ];

        $observed_items = array();
        if($roadPatrol->observedItems) {
            foreach ($roadPatrol->observedItems as $index => $observed_item) {
                $observed_items[] = [
                    'item' => $observed_item->item_name,
                    'sub_item' => $observed_item->sub_item_name,
                    'local_name' => $observed_item->local_name,
                    'description' => $observed_item->description,
                    'priority_fa' => $observed_item->priority_fa,
                ];
            }
        }

        $data['observed_items'] = $observed_items;
        return $this->successResponse($data);
    }
    public function countryPatrolActivity(Request $request, ReportService $reportService): JsonResponse
    {
        $activities = $reportService->countryActivity($request);

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

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

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

        $activities = $reportService->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, ReportService $reportService): BinaryFileResponse
    {
        $activities = $reportService->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, ReportService $reportService): BinaryFileResponse
    {
        $activities = $reportService->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);
    }
}
