<?php

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

use App\Exports\V3\SafetyAndPrivacy\CountryActivityReport;
use App\Exports\V3\SafetyAndPrivacy\ProvinceActivityReport;
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 App\Services\Cartables\SafetyAndPrivacyTableService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class OperatorReportController extends Controller
{
    use ApiResponse;

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

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

    public function countryExcelActivity(Request $request, OperatorService $operatorService): BinaryFileResponse
    {
        $data = $operatorService->countryActivity($request);
        $name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new CountryActivityReport($data), $name);
    }

    public function provinceExcelActivity(Request $request, OperatorService $operatorService): BinaryFileResponse
    {
        $data = $operatorService->provinceActivity($request);
        $name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        return Excel::download(new ProvinceActivityReport($data, $request), $name);
    }
}
