<?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\HarimOffice\FileRejectedRequest;
use App\Http\Requests\V3\Dashboard\Harim\HarimOffice\FileAcceptedRequest;
use App\Http\Requests\V3\Dashboard\Harim\HarimOffice\RejectRequest;
use App\Http\Requests\V3\Dashboard\Harim\HarimOffice\ShowRequest;
use App\Http\Requests\V3\Dashboard\Harim\HarimOffice\NeedRoadAccessRequest;
use App\Http\Requests\V3\Dashboard\Harim\HarimOffice\WithoutRoadAccessRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Harim;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HarimOfficeController extends Controller
{
    use ApiResponse ;

    public function index(Request $request): JsonResponse
    {
        $query = Harim::query()
            ->whereIn('state_id', [
                HarimStates::BARESI_TAVASOT_DAFTAR_HARIM->value,
                HarimStates::BARESI_FILE_APLODE_SHODE_TAVASOTE_DAFTAR_HARIM->value,
            ])
            ->where('edareh_shahri_id', '=', auth()->user()->edarate_shahri_id);

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

        return response()->json($data);
    }


    public function rejectRequest(RejectRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request, $harim) {
            $action = HarimAction::NO->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_MOAVEN->value;

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


    public function needRoadAccess(NeedRoadAccessRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request,$harim) {
            $action = HarimAction::YES->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_MOAVEN->value;

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


    public function withoutRoadAccess(WithoutRoadAccessRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request,$harim) {
            $action = HarimAction::YES->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_MOAVEN->value;

            $harim ->update([
                'state_id' => $state,
                'state_name' => HarimStates::name($state),
                'is_possible' => true,
                'need_access_road' => false,
            ]);
        });
        return $this->successResponse();
    }
    public function fileAccepted(FileAcceptedRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request,$harim) {
            $action = HarimAction::YES->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_MOAVEN->value;

            $harim ->update([
                'state_id' => $state,
                'state_name' => HarimStates::name($state),
                'access_road_allowed' => true,
            ]);
        });
        return $this->successResponse();
    }
    public function fileRejected(FileRejectedRequest $request, Harim $harim): JsonResponse
    {
        DB::transaction(function () use ($request,$harim) {
            $action = HarimAction::NO->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_MOAVEN->value;

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

    }
}
