<?php

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

use App\Exports\V3\RoadItemsProjects\CountryActivityPerItemReport;
use App\Exports\V3\RoadItemsProjects\CountryActivityPerSubItemReport;
use App\Exports\V3\RoadItemsProjects\ProvinceActivityPerItemReport;
use App\Exports\V3\RoadItemsProjects\ProvinceActivityPerSubItemReport;
use App\Http\Controllers\Controller;
use App\Http\Traits\ApiResponse;
use App\Models\EdarateShahri;
use App\Models\InfoItem;
use App\Models\Province;
use App\Models\RoadItemsProject;
use App\Services\Reports\RoadItemReportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class RoadItemsReportController extends Controller
{
    use ApiResponse;
    public function getSubItems(): JsonResponse
    {
        $data = InfoItem::all();
        $items = InfoItem::query()->groupBy('item')->get();

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

    public function countryActivityPerSubItem(Request $request, RoadItemReportService $roadItemReportService): JsonResponse
    {
        $activities = $roadItemReportService->countryActivityPerSubItem($request);
        $provinces = Province::all(['id', 'name_fa']);
        $subItems = InfoItem::query()
            ->where('item', $request->item)
            ->get(['id', 'item', 'item_str', 'sub_item_str', 'sub_item_unit', 'sub_item']);

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

    public function provinceActivityPerSubItem(Request $request, RoadItemReportService $roadItemReportService): JsonResponse
    {
        $activities = $roadItemReportService->provinceActivityPerSubItem($request);

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

        $subItems = InfoItem::query()
            ->where('item', $request->item)
            ->get(['id', 'item', 'item_str', 'sub_item_str', 'sub_item_unit', 'sub_item']);

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

    public function countryActivityPerItem(Request $request, RoadItemReportService $roadItemReportService): JsonResponse
    {
        if (auth()->check()) {
            auth()->user()->addActivityComplete(1033);
        }

        $activities = $roadItemReportService->countryActivityPerItem($request);
        $provinces = Province::all(['id', 'name_fa']);

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

    public function provinceActivityPerItem(Request $request, RoadItemReportService $roadItemReportService): JsonResponse
    {
        $activities = $roadItemReportService->provinceActivityPerItem($request);
        $edarateShahri = EdarateShahri::query()->where('province_id', '=', $request->province_id)->get(['id', 'name_fa']);

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

    public function map(Request $request): JsonResponse
    {
        $provinceId = $request->province_id;
        $cityId = $request->city_id;
        $subId = $request->sub_id;
        $item = $request->item;
        $dateFrom = $request->date_from ? $request->date_from . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
        $dateTo = $request->date_to ? $request->date_to . ' 23:59:59' : now()->addDay()->startOfDay()->toDateTimeString();

        $data = RoadItemsProject::query()
            ->where('is_new', '=', 1)
            ->when($provinceId, fn($query) => $query->where('province_id', '=', $provinceId))
            ->when($cityId, fn($query) => $query->where('city_id', '=', $cityId))
            ->when($subId, fn($query) => $query->where('sub_items', 'like', '%' . $subId . '%'))
            ->when($item, fn($query) => $query->where('item', '=', $item))
            ->when(!is_null($request->status), fn($query) => $query->where('status', '=', $request->status))
            ->whereBetween('activity_date', [$dateFrom, $dateTo])
            ->leftJoin('users', 'users.id', '=', 'road_items_projects.user_id')
            ->select('road_items_projects.id as i', 'start_lat as l', 'start_lng as g', 'status')
            ->get();

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

    public function countryActivityExcelPerItem(Request $request, RoadItemReportService $roadItemReportService): BinaryFileResponse
    {
        auth()->user()->addActivityComplete(1034);
        $name = 'گزارش کشوری از فعالیت روزانه و جاری - جدول 1 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        $activities = $roadItemReportService->countryActivityPerItem($request);
        return Excel::download(new CountryActivityPerItemReport($activities), $name);
    }

    public function provinceActivityExcelPerItem(Request $request, RoadItemReportService $roadItemReportService): BinaryFileResponse
    {
        auth()->user()->addActivityComplete(1034);
        $name = 'گزارش استانی از فعالیت روزانه و جاری - جدول 1 ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        $activities = $roadItemReportService->provinceActivityPerItem($request);
        return Excel::download(new ProvinceActivityPerItemReport($activities), $name);
    }

    public function countryActivityExcelPerSubItem(Request $request, RoadItemReportService $roadItemReportService): BinaryFileResponse
    {
        auth()->user()->addActivityComplete(1034);
        $name = "گزارش کشوری از فعالیت روزانه و جاری - جدول 2 " . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        $activities = $roadItemReportService->countryActivityPerSubItem($request);
        return Excel::download(new CountryActivityPerSubItemReport($activities), $name);
    }

    public function provinceActivityExcelPerSubItem(Request $request, RoadItemReportService $roadItemReportService): BinaryFileResponse
    {
        auth()->user()->addActivityComplete(1034);
        $activities = $roadItemReportService->provinceActivityPerSubItem($request);
        $name = "گزارش استانی از فعالیت روزانه و جاری - جدول 2 " . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ProvinceActivityPerSubItemReport($activities), $name);
    }
}
