<?php

namespace Modules\Reason\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Reason\Requests\Admin\ReasonRequest;
use Modules\Reason\Services\ReasonService;

class ReasonController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/reasons';
    /** View folder */
    private $viewDirectory= 'admin.reasons.';
    //

    public function __construct(
        private ReasonService $reasonService,
    )
    {}

    /**
     * Display a listing of the reasons.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('reasons.head');
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the reasons.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $reasons= $this->reasonService->get($request->all());

        return view($this->viewDirectory.'grid',get_defined_vars());
    }

    /**
     * Show the form for creating a new Reason.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('reasons.store');
        $head= __('reasons.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Reason in storage.
     * @param ReasonRequest $request
     * @return JsonResponse
     */
    public function store(ReasonRequest $request) :JsonResponse
    {
        $this->reasonService->create($request->validated());

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('reasons.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Reason.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $reason = $this->reasonService->getItem($id);
        if (!$reason)
        {
            abort(404);
        }
        $action= route('reasons.update', $reason->id);
        $head= __('reasons.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Reason in storage.
     * @param ReasonRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(ReasonRequest $request, int $id) :JsonResponse
    {
        $reason = $this->reasonService->checkItem($id);
        if (!$reason)
        {
            abort(404);
        }
        $this->reasonService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('reasons.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Reason from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->reasonService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('reasons.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several reasons by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->reasonService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('reasons.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified Reason from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->reasonService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('reasons.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several reasons by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->reasonService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('reasons.index'),
        ];
        return ResponseService::response($response);
    }
}
