<?php

namespace Modules\Faq\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\Faq\Requests\Admin\FaqRequest;
use Modules\Faq\Services\FaqService;
use Modules\Country\Services\CountryService;
use Modules\FaqCategory\Services\FaqCategoryService;

class FaqController extends Controller
{

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

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

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

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

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

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

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

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

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

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

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

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

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

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