<?php

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

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\SafetyAndPrivacyTableService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class SafetyAndPrivacyReportController extends Controller
{
    use ApiResponse;

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

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

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

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