<?php

namespace Modules\User\Controllers\Canteen;

/** 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\User\Enums\UserRoleEnum;
use Modules\User\Requests\Canteen\UserRequest;
use Modules\User\Services\CanteenModuleService;
use Modules\User\Services\ModuleService;
use Modules\User\Services\UserService;
use Modules\Currency\Services\CurrencyService;

class UserController extends Controller
{

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

    public function __construct(
        private UserService $userService,
        private CanteenModuleService $canteenModuleService,
    )
    {}

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

    /**
     * return data of the users.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true,'canteen_id'=>auth()->user()->canteen_id,'role'=>UserRoleEnum::CANTEEN_MODERATOR->value]);
        $users= $this->userService->get($request->all());

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

    /**
     * Show the form for creating a new User.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('canteen.users.store');
        $head= __('users.new');
        $roles = UserRoleEnum::admins();
        $user_modules = [];
        $modules = $this->canteenModuleService->get([]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created User in storage.
     * @param UserRequest $request
     * @return JsonResponse
     */
    public function store(UserRequest $request) :JsonResponse
    {
        $data = $request->validated();
        $data['role'] = UserRoleEnum::CANTEEN_MODERATOR->value;
        $data['canteen_id'] = auth()->user()->canteen_id;
        $this->userService->create($data);

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

    /**
     * Show the form for editing the specified User.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $user = $this->userService->getItem($id);
        if (!$user)
        {
            abort(404);
        }
        $action= route('canteen.users.update', $user->id);
        $head= __('users.edit');
        $user_modules = $this->canteenModuleService->get(['user_id'=>$id])->pluck('id')->toArray();
        $modules = $this->canteenModuleService->get([]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified User in storage.
     * @param UserRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(UserRequest $request, int $id) :JsonResponse
    {
        $user = $this->userService->checkItem($id);
        if (!$user)
        {
            abort(404);
        }
        $data = $request->validated();
        $data['role'] = UserRoleEnum::CANTEEN_MODERATOR->value;
        $data['canteen_id'] = auth()->user()->canteen_id;
        $this->userService->update($data, $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteen.users.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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

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