<?php

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

use App\Enums\AxisTypes;
use App\Enums\SafetyAndPrivacyStatus;
use App\Enums\SafetyAndPrivacySteps;
use App\Exports\V3\SafetyAndPrivacy\OperatorCartableReport;
use App\Facades\DataTable\DataTableFacade;
use App\Facades\File\FileFacade;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\SafetyAndPrivacy\FinishRequest;
use App\Http\Requests\V3\SafetyAndPrivacy\FirstStepStoreRequest;
use App\Http\Requests\V3\SafetyAndPrivacy\SecondStepStoreRequest;
use App\Http\Requests\V3\SafetyAndPrivacy\ThirdStepStoreRequest;
use App\Http\Requests\V3\SafetyAndPrivacy\UpdateRequest;
use App\Http\Traits\ApiResponse;
use App\Models\InfoItem;
use App\Models\SafetyAndPrivacy;
use App\Services\Cartables\SafetyAndPrivacy\OperatorService;
use App\Services\NominatimService;
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;
use Throwable;

class OperatorController extends Controller
{
    use ApiResponse;

    /**
     * @throws Throwable
     */
    public function index(Request $request, OperatorService $operatorService): JsonResponse
    {
        return response()->json($operatorService->datatable($request));
    }

    /**
     * @throws Throwable
     */
    public function excelReport(Request $request, OperatorService $operatorService): BinaryFileResponse
    {
        $name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        $data = $operatorService->datatable($request);
        return Excel::download(new OperatorCartableReport($data['data']), $name);
    }

    public function firstStepStore(FirstStepStoreRequest $request, NominatimService $nominatimService): JsonResponse
    {
        $user = auth()->user();

        $coordinates = explode(',', $request->point);
        $item = InfoItem::query()
            ->where('id', $request->info_id)
            ->where('item', 17)
            ->firstOrFail();

        DB::transaction(function () use ($request, $coordinates, $item, $nominatimService, $user)
        {
            $step = SafetyAndPrivacySteps::SHENASAEI->value;
            $safety_and_privacy = SafetyAndPrivacy::query()->create([
                'lat' => $coordinates[0],
                'lon' => $coordinates[1],
                'operator_id' => $user->id,
                'operator_name' => $user->name,
                'province_id' => $user->province_id,
                'province_fa' => $user->province_fa,
                'info_id' => $item->id,
                'info_fa' => $item->sub_item_str,
                'city_id' => $user->city_id,
                'city_fa' => $user->city_fa,
                'activity_date_time' => $request->activity_date." ".$request->activity_time,
                'edare_shahri_id' => $user->edarate_shahri_id,
                'edare_shahri_name' => $user->edarate_shahri_name,
                'way_id' => $nominatimService->get_way_id_from_nominatim($coordinates[0], $coordinates[1]),
                'axis_type_id' => $request->axis_type_id,
                'axis_type_name' => AxisTypes::name($request->axis_type_id),
                'step' => $step,
                'step_fa' => SafetyAndPrivacySteps::name($step),
                'is_finished' => false,
            ]);

            $safety_and_privacy->recognize_picture = $request->has('recognize_picture') ?
                FileFacade::save($request->recognize_picture, "safety_and_privacy/{$safety_and_privacy->id}") :
                null;
            $safety_and_privacy->save();

            $user->addActivityComplete(1134);
        });

        return $this->successResponse();
    }

    public function secondStepStore(SafetyAndPrivacy $safetyAndPrivacy, SecondStepStoreRequest $request): JsonResponse
    {
        $user = auth()->user();

        DB::transaction(function () use ($request, $safetyAndPrivacy, $user) {

            $need_judiciary = $request->need_judiciary;

            $judiciary_document = [];
            if ($need_judiciary) {
                foreach ($request->judiciary_document as $index => $file) {
                    $judiciary_document[] = FileFacade::save(
                        $file,
                        "safety_and_privacy/{$safetyAndPrivacy->id}/judiciary_document"
                    );
                }
            }

            $step = SafetyAndPrivacySteps::MOSTANADAT_GAZAEI->value;
            $safetyAndPrivacy->update([
                'step' => $step,
                'step_fa' => SafetyAndPrivacySteps::name($step),
                'judiciary_document' => $need_judiciary ? serialize($judiciary_document) : null,
                'judiciary_document_upload_date' => $need_judiciary ? now() : null,
                'need_judiciary' => $need_judiciary,
                'operator_description' => $request->operator_description,
            ]);

            $user->addActivityComplete(1135);
        });

        return $this->successResponse();
    }

    public function thirdStepStore(SafetyAndPrivacy $safetyAndPrivacy, ThirdStepStoreRequest $request): JsonResponse
    {
        $user = auth()->user();

        DB::transaction(function () use ($request, $safetyAndPrivacy, $user) {
            $step = SafetyAndPrivacySteps::BARKHORD->value;
            $status_id = SafetyAndPrivacyStatus::PENDING->value;

            $safetyAndPrivacy->update([
                'step' => $step,
                'step_fa' => SafetyAndPrivacySteps::name($step),
                'status' => $status_id,
                'status_fa' => SafetyAndPrivacyStatus::name($status_id),
                'action_picture' => FileFacade::save($request->action_picture, "safety_and_privacy/{$safetyAndPrivacy->id}/action_picture"),
                'action_picture_document_upload_date' => now(),
                'action_date' => $request->action_date,
                'is_finished' => true,
                'final_description' => 'پیام سیستمی : این عملیات پس از طی تمامی گام ها خاتمه یافت'
            ]);

            $user->addActivityComplete(1136);
        });

        return $this->successResponse();
    }

    public function update(SafetyAndPrivacy $safetyAndPrivacy, UpdateRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request, $safetyAndPrivacy) {
            $safetyAndPrivacy->update([
                'axis_type_id' => $request->axis_type_id,
                'axis_type_name' => AxisTypes::name($request->axis_type_id),
            ]);

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

        return $this->successResponse();
    }

    public function show(SafetyAndPrivacy $safetyAndPrivacy): JsonResponse
    {
        return $this->successResponse($safetyAndPrivacy);
    }

    public function destroy(SafetyAndPrivacy $safetyAndPrivacy): JsonResponse
    {
        FileFacade::deleteDirectory("safety_and_privacy/{$safetyAndPrivacy->id}");

        DB::transaction(function () use ($safetyAndPrivacy)
        {
            auth()->user()->addActivityComplete(1155);
            $safetyAndPrivacy->delete();
        });

        return $this->successResponse();
    }

    public function deserialize(SafetyAndPrivacy $safetyAndPrivacy): JsonResponse
    {
        $judiciaries = $safetyAndPrivacy->judiciary_document ? unserialize($safetyAndPrivacy->judiciary_document) : [];

        $result = [];

        foreach ($judiciaries as $judiciary) {
            $result[] = Storage::disk('public')->url($judiciary);
        }

        return $this->successResponse($result);
    }

    public function subItems(): JsonResponse
    {
        return $this->successResponse(InfoItem::query()
            ->where('item', '=', 17)
            ->select('id', 'item', 'item_str', 'sub_item_str as name', 'sub_item_unit as unit', 'needs_image', 'needs_end_point', 'sub_item')
            ->get());
    }

    public function finish(FinishRequest $request, SafetyAndPrivacy $safetyAndPrivacy): JsonResponse
    {
        $status_id = SafetyAndPrivacyStatus::PENDING->value;

        $safetyAndPrivacy->update([
            'is_finished' => 1,
            'final_description' => $request->final_description,
            'status' => $status_id,
            'status_fa' => SafetyAndPrivacyStatus::name($status_id),
        ]);

        return $this->successResponse();
    }

    public function map(Request $request): JsonResponse
    {
        return response()->json(DataTableFacade::run(
            SafetyAndPrivacy::query(),
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedSelects: ['id', 'lat', 'lon', 'step']
        ));
    }
}
