<?php

namespace Modules\Option\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\Option\Requests\Admin\OptionRequest;
use Modules\Option\Services\OptionService;

class OptionController extends Controller
{

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

    public function __construct(
        private OptionService $optionService,
    )
    {}

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

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

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

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

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

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

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

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

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

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

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

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