<?php

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

use App\Exports\V3\RoadObservation\SupervisorCartableReport;
use App\Facades\File\FileFacade;
use App\Http\Requests\V3\RoadObservation\RestoreRequest;
use App\Http\Requests\V3\RoadObservation\VerifyBySupervisorRequest;
use App\Http\Traits\ApiResponse;
use App\Models\RoadObservationHistory;
use App\Models\RoadObserved;
use App\Services\Cartables\RoadObservation\SupervisorService;
use App\Services\Cartables\RoadObservationTableService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class SupervisorController
{
    use ApiResponse;

    public function index(Request $request, SupervisorService $supervisorService): JsonResponse
    {
        return response()->json($supervisorService->index($request));
    }

    public function verify(VerifyBySupervisorRequest $request, RoadObserved $roadObserved): JsonResponse
    {
        $statusFa = $request->verify == 1 ? 'تایید شده' : 'عدم تایید';

        DB::transaction(function () use ($request, $statusFa, $roadObserved) {
            $roadObserved->update([
                'status' => $request->verify,
                'status_fa' => $statusFa,
                'supervisor_id' => auth()->user()->id,
                'supervisor_name' => auth()->user()->name,
                'supervisor_description' => $request->description,
                'supervising_time' => now(),
            ]);

            auth()->user()->addActivityComplete(1138);
        });

        return $this->successResponse();
    }

    public function report(Request $request, SupervisorService $supervisorService): BinaryFileResponse
    {
        $name = 'گزارش از کارتابل ارزیابی واکنش سریع '. verta()->now()->format('Y-m-d H:i') . '.xlsx';
        $data = $supervisorService->index($request);

        return Excel::download(new SupervisorCartableReport($data['data']), $name);
    }

    public function restore(RestoreRequest $request, RoadObserved $roadObserved): JsonResponse
    {
        if ($roadObserved->image_before) {
            FileFacade::delete($roadObserved->image_before, true);
        }

        if ($roadObserved->image_after) {
            FileFacade::delete($roadObserved->image_after, true);
        }

        DB::transaction(function () use ($request, $roadObserved) {
            $roadObserved->update([
                'image_before' => null,
                'image_after' => null,
                'rms_last_activity' => null,
                'rms_last_activity_fa' => null,
                'updated_at_fa' => null,
                'updated_at' => null,
                'rms_status' => 0,
                'status' => null,
                'status_fa' => null,
            ]);

            RoadObservationHistory::query()->create([
                'id' => $roadObserved->id,
                'user_id' => auth()->user()->id,
                'action' => 'restore',
                'description' => $request->restore_description,
            ]);

            auth()->user()->addActivityComplete(1141);
        });

        return $this->successResponse();
    }
}