<?php

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

use App\Enums\MissionStates;
use App\Enums\MissionTypes;
use App\Enums\MissionZones;
use App\Http\Controllers\Controller;
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\Services\Cartables\Mission\RequestPortalService;
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;

            $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->edare_shahri_id ?? null,
                'edare_shahri_name' => $user->edare_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),
            ]);

            $mission->rahdaran()->attach($request->rahdaran);
        });

        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,
                'request_date' => $request->request_date,
                'description' => $request->description,
                'end_point' => $request->end_point,
                'area' => json_encode($request->area),
            ]);

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

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