<?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\PhoneRequest;
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\Requests\Web\VerifyPhoneRequest;
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)
    {
        if (!Session::get('regist'))
        {
            return redirect(route('web.regist.phone'));
        }
        $title = __('web.regist');
        $action = route('web.regist.store');
        return view($this->view.'regist', get_defined_vars());
    }
    public function registPhone(Request $request) :View
    {
        $title = __('web.regist');
        $action = route('web.regist.phone.check');
        return view($this->view.'phone', get_defined_vars());
    }

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

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

        Session::forget(['regist','phone']);

        $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').'?phone='.$request->phone
        ];
        return ResponseService::response($response);
    }

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

    public function verifyCode(VerifyPhoneRequest $request) :View
    {
        $title = __('web.verify');
        $phone = $request->phone;
        $resend_url = route('web.verify.code').'?phone='.$request->phone;
        $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)
    {
        $reset = Session::get('resetpassword');
        if (!$reset)
        {
            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
    {
        $phone = Session::get('phone');
        if (!$phone)
        {
            return redirect()->to(route('web.home'));
        }
        $credentials = [
            'phone' => $phone,
            'password' => $request->password
        ];
        $user = $this->userService->getItemByPhone($phone);
        $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'));
    }
}
