<?php

namespace Modules\FaqCategory\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\FaqCategory\Requests\Admin\FaqCategoryRequest;
use Modules\FaqCategory\Services\FaqCategoryService;
use Modules\Currency\Services\CurrencyService;

class FaqCategoryController extends Controller
{

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

    public function __construct(
        private FaqCategoryService $faqCategoryService,
    )
    {}

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

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

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

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

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

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

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

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

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

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

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

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