<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\MemberExport;
use App\Http\Requests\Admin\CategoryRequest;
use App\Models\Category;
use App\Services\Interfaces\CategoryRepositoryInterface;
use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class CategoryController extends Controller
{

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

    public function __construct(CategoryRepositoryInterface $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

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

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

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

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

    /**
     * Store a newly created Category in storage.
     * @param CategoryRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(CategoryRequest $request) :JsonResponse
    {
        $category= $this->categoryRepository->save($request, null);

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

    /**
     * Show the form for editing the specified Category.
     *
     * @param  Category  $category
     * @return View
     */
    public function edit($id) :View
    {
        $action= route('categories.update', $id);
        $category = $this->categoryRepository->getItem($id);
        $head= __('categories.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Category in storage.
     * @param CategoryRequest $request
     * @param Category $category
     * @return RedirectResponse|Redirector
     */
    public function update(CategoryRequest $request, Category $category) :JsonResponse
    {
        $category= $this->categoryRepository->save($request, $category);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('categories.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Category from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->categoryRepository->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several categories by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->categoryRepository->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Category from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->categoryRepository->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several categories by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->categoryRepository->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
