<?php

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


use App\Exports\V3\RoadPatrols\OperatorCartableReport;
use App\Facades\Sms\Sms;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\RoadPatrol\DeleteRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Mission;
use App\Models\RoadPatrol;
use App\Services\Cartables\RoadPatrol\OperatorService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;


class OperatorController extends Controller
{
    use ApiResponse;

    public function index(Request $request, OperatorService $operatorService): JsonResponse
    {
        $data = $operatorService->dataTable($request);
        return response()->json($data);
    }
    public function excel(Request $request, OperatorService $operatorService): BinaryFileResponse
    {
        $name = 'گزارش از کارتابل عملیات گشت راهداری ' . verta()->now()->format('Y-m-d H:i') . '.xlsx';
        $data = $operatorService->dataTable($request, true);
        return Excel::download(new OperatorCartableReport($data['data']), $name);
    }

    public function store(Mission $mission): JsonResponse
    {
        DB::transaction(function () use ($mission) {
            $user = auth()->user();

            $road_patrol = RoadPatrol::query()->create([
                'operator_id' => $user->id,
                'status'     => 0,
                'status_fa'  => 'در حال بررسی',
                'province_id' => $mission->province_id,
                'province_fa' => $mission->province_name,
                'edare_id'    => $mission->edare_shahri_id,
                'edare_name'  => $mission->edare_shahri_name,
                'start_time'  => $mission->start_time,
                'end_time'    => $mission->finish_time,
            ]);

            $road_patrol->missions()->attach($mission->id);

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

        return $this->successResponse();
    }

    public function delete(DeleteRequest $request, RoadPatrol $roadPatrol): JsonResponse
    {
        if ($request->type == 1) {
            DB::transaction(function () use ($roadPatrol) {
                $roadPatrol->observedItems()->delete();
                $roadPatrol->delete();
            });
        } else {
            DB::transaction(function () use ($roadPatrol) {
                if ($roadPatrol->observedItems) {
                    foreach ($roadPatrol->observedItems as $index => $observed_item) {
                        if ($observed_item->roadItemProject) {
                            $road_item = $observed_item->roadItemProject;

                            if ($road_item->files()->exists()) {
                                Storage::delete('public/' . $road_item->files[0]->path);
                                Storage::delete('public/' . $road_item->files[1]->path);
                                $road_item->files()->delete();
                            }

                            $road_item->delete();
                        }

                        $observed_item->delete();
                    }
                }
                auth()->user()->addActivityComplete(1144);

                $roadPatrol->delete();
            });
        }

        return $this->successResponse();
    }
}
