<?php

namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Web\FogetPasswordRequest;
use Modules\User\Requests\Web\LoginRequest;
use Modules\User\Requests\Web\RegistRequest;
use Modules\User\Requests\Web\VerifyCodeRequest;
use Modules\User\Requests\Web\ResetPasswordRequest;
use Modules\User\Services\UserService;

class AuthController extends Controller
{
    private $view = 'web.pages.auth.';

    private UserService $userService;

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

    public function login(Request $request) :View
    {
        $title = __('web.login');
        $action = route('web.login.check');
        return view($this->view.'login', get_defined_vars());
    }

    public function loginCheck(LoginRequest $request) :JsonResponse
    {
        $response = $this->userService->login($request->all());

        return ResponseService::response($response);
    }


    public function regist(Request $request) :View
    {
        $title = __('web.regist');
        $action = route('web.regist.store');
        return view($this->view.'regist', get_defined_vars());
    }

    public function userStore(RegistRequest $request) :JsonResponse
    {
        $credentials = [
            'email' => $request->email,
            'password' => $request->password
        ];
        $request->merge(['role'=>UserRoleEnum::USER->value]);
        $this->userService->save($request,null);


        $response = $this->userService->login($credentials);

        return ResponseService::response($response);
    }


    public function forgetpassword(Request $request) :View
    {
        $title = __('web.forgetpassword');
        $action = route('web.forgetpassword.check');
        return view($this->view.'forget_password', get_defined_vars());
    }


    public function forgetpasswordCheck(FogetPasswordRequest $request)  :JsonResponse
    {
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('web.verify.code').'?email='.$request->email
        ];
        return ResponseService::response($response);
    }

    public function verifyCode(FogetPasswordRequest $request) :View
    {
        $title = __('web.verify');
        $email = $request->email;
        $resend_url = route('web.verify.code').'?email='.$request->email;
        $action = route('web.verify.code.check');
        $this->userService->sendCode($request->all());
        return view($this->view.'verify_code', get_defined_vars());
    }

    public function verifyCodeCheck(VerifyCodeRequest $request) :JsonResponse
    {
        $response = $this->userService->checkCode($request->all());

        return ResponseService::response($response);
    }

    public function resetpassword(Request $request)
    {
        $email = Session::get('email');
        if (!$email)
        {
            return redirect()->to(route('web.home'));
        }

        $title = __('web.resetpassword');
        $action = route('web.resetpassword.store');
        return view($this->view.'reset_password', get_defined_vars());
    }

    public function resettingPassword(ResetPasswordRequest $request) :JsonResponse
    {
        $email = Session::get('email');
        if (!$email)
        {
            return redirect()->to(route('web.home'));
        }
        $credentials = [
            'email' => $email,
            'password' => $request->password
        ];
        $user = $this->userService->getItemByEmail($email);
        $this->userService->save($request,$user);

        $response = $this->userService->login($credentials);

        return ResponseService::response($response);
    }

    public function logout()
    {
        $user = Auth::user();
        Auth::logout($user);
        return redirect(route('web.home'));
    }
}
