<?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 App\Http\Controllers\Controller;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Admin\UserRequest;
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= '/admin/users';
    /** View folder */
    private $viewDirectory= 'admin.users.';
    //

    public function __construct(
        private UserService $userService,
        private ModuleService $moduleService,
    )
    {}

    /**
     * Display a listing of the users.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('users.head');
        $roles = UserRoleEnum::admins();
        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->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('users.store');
        $head= __('users.new');
        $roles = UserRoleEnum::admins();
        $user_modules = [];
        $modules = $this->moduleService->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
    {
        $this->userService->create($request->validated());

        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  string $code
     * @return View
     */
    public function edit(string $code) :View
    {
        $user = $this->userService->getItemByCode($code);
        if (!$user)
        {
            abort(404);
        }
        $action= route('users.update', $code);
        $head= __('users.edit');
        $roles = UserRoleEnum::admins();
        $user_modules = $this->moduleService->get(['user_id'=>$user->id])->pluck('id')->toArray();
        $modules = $this->moduleService->get([]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified User in storage.
     * @param UserRequest $request
     * @param string $code
     * @return JsonResponse
     */
    public function update(UserRequest $request, string $code) :JsonResponse
    {
        $user = $this->userService->getItemByCode($code);
        if (!$user)
        {
            abort(404);
        }
        // dd($request->validated());
        $this->userService->update($request->validated(), $user->id);
        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 string $code
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(string $code) :JsonResponse
    {
        $this->userService->destroy($code);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('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('users.index'),
        ];
        return ResponseService::response($response);
    }

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

    /**
     * Permanently delete the specified user from storage.
     *
     * @param string $code
     * @return JsonResponse
     */
    public function forceDelete(string $code) :JsonResponse
    {
        $this->userService->forceDelete($code);
        session()->flash('message', __('dashboard.deleteDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('users.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Permanently delete several users by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function forceDeleteAll(Request $request) :JsonResponse
    {
        $this->userService->forceDeleteAll($request->ids);
        session()->flash('message', __('dashboard.deleteDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('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('users.index'),
        ];
        return ResponseService::response($response);
    }
}
