<?php

namespace Modules\User\Controllers\Admin;

/** Ajax Request */

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 Modules\User\Enums\UserRoleEnum;
use Modules\User\Models\User;
use Modules\User\Requests\Admin\UserRequest;
use Modules\User\Services\ModuleService;
use Modules\User\Services\UserService;

class UserController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/users';
    /** View folder */
    private $viewDirectory= 'admin.users.';
    //
    private UserService $userService;
    private ModuleService $moduleService;

    public function __construct(UserService $userService , ModuleService $moduleService)
    {
        $this->userService = $userService;
        $this->moduleService = $moduleService;
    }

    /**
     * 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,'roles'=>UserRoleEnum::admins()]);
        $users= $this->userService->get($request);

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

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

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

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

    /**
     * Show the form for editing the specified User.
     *
     * @param  User  $user
     * @return View
     */
    public function edit(User $user) :View
    {
        // dd($user->id);
        $action= route('users.update', $user->id);
        $data= $this->userService->getItem($user);
        $head= __('users.edit');
        $modules = $this->moduleService->get(request());
        $user_modules= old('modules',  $user->modules()->pluck('modules.id')->toArray() ?? []);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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