<?php

namespace App\Http\Controllers\V3\Dashboard\SafetyAndPrivacy\OperatorReport;

use App\Exports\V3\SafetyAndPrivacy\Country\UtilityPassingReport as ExcelReport;
use App\Exports\V3\SafetyAndPrivacy\Province\UtilityPassingReport;
use App\Http\Controllers\Controller;
use App\Http\Traits\ApiResponse;
use App\Models\EdarateShahri;
use App\Models\Province;
use App\Services\Cartables\SafetyAndPrivacy\OperatorService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class UtilityPassingActivityController extends Controller
{
    use ApiResponse;

    public function countryActivity(Request $request, OperatorService $operatorService): JsonResponse
    {
        $data = $operatorService->countryActivity(91);

        return $this->successResponse([
            'activities' => $data,
            'provinces' => Province::all(['id', 'name_fa']),
        ]);
    }
    public function provinceActivity(Request $request, OperatorService $operatorService): JsonResponse
    {
        $data = $operatorService->provinceActivity(
            info_id: 91,
            province_id: $request->input('province_id')
        );

        return $this->successResponse([
            'activities' => $data,
            'edarateShahri' => EdarateShahri::where('province_id', $request->province_id)->get(['id', 'name_fa']),
        ]);
    }
    public function countryExcelActivity(Request $request, OperatorService $operatorService): BinaryFileResponse
    {
        $data = $operatorService->countryActivity(91);

        $name = 'گزارش نگهداری حریم راه برای عبور تاسیسات زیربنایی غیر مجاز ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ExcelReport($data), $name);
    }

    public function provinceExcelActivity(Request $request, OperatorService $operatorService): BinaryFileResponse
    {
        $data = $operatorService->provinceActivity(
            info_id: 91,
            province_id: $request->input('province_id')
        );
        $name = 'گزارش نگهداری حریم راه برای عبور تاسیسات زیربنایی غیر مجاز ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';

        return Excel::download(new UtilityPassingReport($data, $request), $name);
    }
}
