<?php

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

use App\Enums\HarimAction;
use App\Enums\HarimStates;
use App\Facades\DataTable\DataTableFacade;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\Dashboard\Harim\Deputy\ConfirmRequest;
use App\Http\Requests\V3\Dashboard\Harim\Deputy\ConfirmRoadAccessRequest;
use App\Http\Requests\V3\Dashboard\Harim\Deputy\ReferRequest;
use App\Http\Requests\V3\Dashboard\Harim\Deputy\ReferRoadAccessRequest;
use App\Http\Requests\V3\Dashboard\Harim\Deputy\ShowRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Harim;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TechnicalDeputyController extends Controller
{
    use ApiResponse;
    public function index(Request $request): JsonResponse
    {
        $query = Harim::query()
            ->whereIn('state_id', [
                HarimStates::BARESI_FILE_APLODE_SHODE_TAVASOTE_MOAVEN->value,
                HarimStates::BARESI_TAVASOT_MOAVEN->value,
            ])
            ->where('edareh_shahri_id', '=', auth()->user()->edarate_shahri_id);

        $data = DataTableFacade::run(
                $query,
                $request,
                allowedFilters: ['*'],
                allowedSortings: ['*'],
        );

        return response()->json($data);
    }
    public function confirmRequest(ConfirmRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request,$harim) {
            $action = HarimAction::CONFIRM->value;

            $harim->histories()->create([
                'expert_id' => auth()->user()->id,
                'previous_state_id' => $harim->state_id,
                'previous_state_name' => $harim->state_name,
                'expert_description' => $request->expert_description,
                'action_id'  => $action,
                'action_name' => HarimAction::name($action),
            ]);

            $state = HarimStates::BARESI_TAVASOT_MODIR_KOL->value;

            $harim ->update([
                'state_id' => $state,
                'state_name' => HarimStates::name($state),
            ]);
        });
        return $this->successResponse();

    }
    public function referRequest(ReferRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request,$harim) {
            $action = HarimAction::REFER->value;

            $harim->histories()->create([
                'expert_id' => auth()->user()->id,
                'previous_state_id' => $harim->state_id,
                'previous_state_name' => $harim->state_name,
                'expert_description' => $request->expert_description,
                'action_id'  => $action,
                'action_name' => HarimAction::name($action),
            ]);

            $state = HarimStates::BARESI_TAVASOT_DAFTAR_HARIM->value;

            $harim->update([
                'state_id' => $state,
                'state_name' => HarimStates::name($state),
            ]);
        });
        return $this->successResponse();
    }
    public function confirmRoadAccess(ConfirmRoadAccessRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request, $harim) {
            $action = HarimAction::CONFIRM->value;
            $harim->histories()->create([
                'expert_id' => auth()->user()->id,
                'previous_state_id' => $harim->state_id,
                'previous_state_name' => $harim->state_name,
                'expert_description' => $request->expert_description,
                'action_id' => $action,
                'action_name' => HarimAction::name($action),
            ]);

            $state = HarimStates::BARESI_FILE_APLODE_SHODE_TAVASOTE_MODIR_KOL->value;

            $harim->update([
                'state_id' => $state,
                'state_name' => HarimStates::name($state),
            ]);
        });
        return $this->successResponse();
    }

    public function referRoadAccess(ReferRoadAccessRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request, $harim) {
            $action = HarimAction::REFER->value;
            $harim->histories()->create([
                'expert_id' => auth()->user()->id,
                'previous_state_id' => $harim->state_id,
                'previous_state_name' => $harim->state_name,
                'expert_description' => $request->expert_description,
                'action_id' => $action,
                'action_name' => HarimAction::name($action),
            ]);

            $state = HarimStates::BARESI_FILE_APLODE_SHODE_TAVASOTE_DAFTAR_HARIM->value;

            $harim->update([
                'state_id' => $state,
                'state_name' => HarimStates::name($state),
            ]);
        });
        return $this->successResponse();
    }
    public function show(ShowRequest $request, Harim $harim): JsonResponse
    {
        return $this->successResponse($harim->load('histories'));

    }
}
