<?php

namespace Modules\Category\Controllers\Employee;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Category\Exports\CategoryExport;
use Modules\Category\Requests\Employee\CategoryRequest;
use Modules\Category\Services\CategoryService;
use Modules\MainService\Enums\ViewEnum;
use Modules\Sector\Services\SectorService;

class CategoryController extends Controller
{

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

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

    /**
     * Display a listing of the categories.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $typeView = $request->typeView ?? ViewEnum::LIST->value;
        $data =
            [
                'head'=>__('categories.head'),
                'view' => ViewEnum::INDEX->value,
                'route'=>$this->viewDirectory,
                'typeView' => $typeView,
                'kanbanView'=>ViewEnum::KANBAN->value,
                'listView'=>ViewEnum::LIST->value,
                'request' => $request,
            ];
        return $this->categoryService->render($data);
    }

    /**
     * return data of the categories.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request)
    {
        if(isset($request->orderBy))
        {
            $orderBy = json_decode($request->orderBy, true);
            $request->merge(['orderBy'=>['key'=>$orderBy['key'], 'value'=>$orderBy['value']]]);
        }
        if($request->export)
        {
            $categories = $this->categoryService->get($request->all());
            return Excel::download(new CategoryExport($categories,Auth::user()), 'categories.xlsx');
        }
        $typeView = $request->typeView ?? ViewEnum::LIST->value;
        $request->merge([
            'route' => $this->viewDirectory,
            'view' => $typeView,
            'request'=>$request,
            'groupSector' => $typeView == ViewEnum::KANBAN->value ? true : false,
            'paginate' => $typeView != ViewEnum::KANBAN->value ? true : false,
        ]);
        return $this->categoryService->renderView($request->all());
    }

    /**
     * Show the form for creating a new Category.
     *
     * @return View
     */
    public function create() :View
    {
        $data =
            [
                'head'=>__('categories.new'),
                'action' => route($this->actionDirectory.'store'),
                'view' => ViewEnum::FORM->value,
                'route'=> $this->viewDirectory,
            ];
        return $this->categoryService->render($data);
    }

    /**
     * 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'=> url()->previous(),
        ];
        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);
        }
        $data =
            [
                'head'=>__('categories.edit'),
                'action' => route($this->actionDirectory.'update',$category->id),
                'view' => ViewEnum::FORM->value,
                'route'=> $this->viewDirectory,
                'category' => $category
            ];
        return $this->categoryService->render($data);
    }

    public function show(int $id) :View
    {
        $category = $this->categoryService->getItem($id);
        if (!$category)
        {
            abort(404);
        }
        $data =
            [
                'head'=>__('categories.show'),
                'view' => ViewEnum::SHOW->value,
                'route'=> $this->viewDirectory,
                'category' => $category
            ];
        return $this->categoryService->render($data);
    }

    /**
     * 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'=> url()->previous(),
        ];
        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($this->actionDirectory.'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($this->actionDirectory.'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($this->actionDirectory.'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($this->actionDirectory.'index'),
        ];
        return ResponseService::response($response);
    }
}
