<?php

namespace Modules\Category\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\Category\Requests\Admin\CategoryRequest;
use Modules\Category\Services\CategoryService;
use Modules\Canteen\Services\CanteenService;

class CategoryController extends Controller
{

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

    public function __construct(
        private CategoryService $categoryService,
        private CanteenService $canteenService,
    )
    {}

    /**
     * Display a listing of the categories.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('categories.head');
        $canteens = $this->canteenService->get();
        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->categoryService->get($request->all());

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

    /**
     * Show the form for creating a new Category.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('categories.store');
        $head= __('categories.new');
        $canteens = $this->canteenService->get();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }
    
    public function canteenCategories(Request $request)
    {
        $categories = $this->categoryService->get($request->all());
        return view( $this->viewDirectory.'categories', get_defined_vars() );
    }
    
    /**
     * Store a newly created Category in storage.
     * @param CategoryRequest $request
     * @return JsonResponse
     */
    public function store(CategoryRequest $request) :JsonResponse
    {
        $this->categoryService->create($request->validated());

        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  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $category = $this->categoryService->getItem($id);
        if (!$category)
        {
            abort(404);
        }
        $action= route('categories.update', $category->id);
        $head= __('categories.edit');
        $canteens = $this->canteenService->get();
        $categories = $this->categoryService->get(['main'=>true]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Category in storage.
     * @param CategoryRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(CategoryRequest $request, int $id) :JsonResponse
    {
        $category = $this->categoryService->checkItem($id);
        if (!$category)
        {
            abort(404);
        }
        $this->categoryService->update($request->validated(), $id);
        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 int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->categoryService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('categories.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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