<?php

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

use App\Enums\MissionCategory;
use App\Enums\MissionStates;
use App\Enums\MissionTypes;
use App\Enums\MissionZones;
use App\Http\Controllers\Controller;
use App\Http\Controllers\V3\Dashboard\RoadPatrol\OperatorController;
use App\Http\Requests\V3\Mission\RequestPortal\NoProcessRequest;
use App\Http\Requests\V3\Mission\RequestPortal\StoreRequest;
use App\Http\Requests\V3\Mission\RequestPortal\UpdateRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Mission;
use App\Models\RoadObserved;
use App\Services\Cartables\Mission\RequestPortalService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RequestPortalController extends Controller
{
    use ApiResponse;

    public function index(Request $request, RequestPortalService $requestPortalService): JsonResponse
    {
        return response()->json($requestPortalService->datatable($request));
    }

    public function store(StoreRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request) {
            $user = auth()->user();
            $state = MissionStates::REQUEST_CREATED->value;
            $category = $request->category_id;

            $mission = Mission::query()->create([
                'user_id' => $user->id,
                'username' => $user->username,
                'province_id' => $user->province_id,
                'province_name' => $user->province_fa,
                'edare_shahri_id' => $user->edarate_shahri_id ?? null,
                'edare_shahri_name' => $user->edarate_shahri_name ?? null,
                'state_id' => $state,
                'state_name' => MissionStates::name($state),
                'requested_machines' => json_encode($request->requested_machines),
                'type' => $request->type,
                'type_fa' => MissionTypes::name($request->type),
                'zone' => $request->zone,
                'zone_fa' => MissionZones::name($request->zone),
                'start_date' => $request->start_date,
                'end_date' => $request->end_date,
                'request_date' => now(),
                'description' => $request->description,
                'end_point' => $request->end_point,
                'area' => json_encode($request->area),
                'category_id' => $category,
                'category_name' => MissionCategory::name($category),
                'explanation' => $request->explanation,
                'code' => rand(100000, 999999),
            ]);

            $mission->rahdaran()->sync($request->rahdaran);

            if ($category == 3){
                RoadObserved::query()
                    ->find($request->road_observed_id)
                    ->missions()
                    ->attach($mission->id);
            }
        });

        return $this->successResponse();
    }

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

    public function update(UpdateRequest $request, Mission $mission): JsonResponse
    {
        DB::transaction(function () use ($mission, $request) {
            $state = MissionStates::REQUEST_CREATED->value;
            $mission->update([
                'state_id' => $state,
                'state_name' => MissionStates::name($state),
                'requested_machines' => json_encode($request->requested_machines),
                'type' => $request->type,
                'type_fa' => MissionTypes::name($request->type),
                'zone' => $request->zone,
                'zone_fa' => MissionZones::name($request->zone),
                'start_date' => $request->start_date,
                'end_date' => $request->end_date,
                'description' => $request->description,
                'end_point' => $request->end_point,
                'area' => json_encode($request->area),
                'category_id' => $request->category_id,
                'category_name' => MissionCategory::name($request->category_id),
                'explanation' => $request->explanation,
            ]);

            $mission->rahdaran()->sync($request->rahdaran);
            if ($request->category_id == 3){
                RoadObserved::query()
                    ->find($request->road_observed_id)
                    ->missions()
                    ->sync($mission->id);
            }
        });

        return $this->successResponse();
    }
    public function destroy(Mission $mission): JsonResponse
    {
        DB::transaction(function () use ($mission) {
            $mission->rahdaran()->detach();
            $mission->delete();
        });
        return $this->successResponse();
    }
    public function noProcess(NoProcessRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request) {

            $start = Carbon::parse($request->start_date);
            $end = Carbon::parse($request->end_date);
            $type = $start->diffInMinutes($end) > 480 ? MissionTypes::NO_PROCESS_ROZANE->value : MissionTypes::NO_PROCESS_SAATY->value;
            $user = auth()->user();
            $zone= $request->zone;
            $state = MissionStates::END_MISSION->value;

            $mission = Mission::query()->create([
                'user_id' => $user->id,
                'username' => $user->username,
                'province_id' => $user->province_id,
                'province_name' => $user->province_fa,
                'edare_shahri_id' => $user->edarate_shahri_id ?? null,
                'edare_shahri_name' => $user->edarate_shahri_name ?? null,
                'zone' => $zone,
                'zone_fa' => MissionZones::name($zone),
                'type' => $type,
                'type_fa' => MissionTypes::name($type),
                'start_date' => $request->start_date,
                'end_date' => $request->end_date,
                'end_point' => $request->end_point,
                'area' => json_encode($request->area),
                'category_id' => $request->category_id,
                'category_name' => MissionCategory::name($request->category_id),
                'start_time' => $request->start_date,
                'finish_time' => $request->end_date,
                'state_id' => $state,
                'state_name' => MissionStates::name($state),
                'explanation' => $request->explanation,
                'request_date' => now(),
            ]);

            $mission->rahdaran()->sync($request->rahdaran);
            $mission->machines()->attach($request->machines);
            $mission->rahdaran()->attach($request->driver, ['is_driver' => true]);
        });

        return $this->successResponse();
    }
}
