<?php

namespace App\Http\Controllers\UserAuth;

use App\Http\Controllers\Controller;
use App\Http\Requests\VerifyServletOTPReqeust;
use App\Mail\ServletMail;
use App\Mail\WelcomeEmail;
use App\Rules\User\Google2FaSecretKeyVerificationRule;
use App\Services\Google2FaServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;

class TwoFAVerificationController extends Controller
{

    protected Google2FaServices $google2FaServices;
    public function __construct(Google2FaServices $google2FaServices)
    {
        $this->google2FaServices = $google2FaServices;
    }

    public function GoogleF2(){
        if($this->checkGoogle2FaSession()){
            return redirect('/');
        }
        return view('Dashboard.GoogleF2Check');
    }

    public function servlet(){
        if($this->checkServletSession()){
            return redirect('/');
        }
        return view('Dashboard.servlet');
    }

    public function resendServletVerificationCode(Request $request){
        $generator =  substr(md5(uniqid(rand(), true)) , 0 , 6);
        session()->put('otp' , $generator);
        $ip = $request->ip();
        Mail::send(new ServletMail($ip));
        return redirect()->back();
    }


    public function verifyServlet(VerifyServletOTPReqeust $request){
        session()->put('servlet' , 1);
        DB::table('users')->update([
            'otp' => md5(session()->get('otp'))
        ]); 
        
        Mail::send(new WelcomeEmail($request->ip()));
        
        if (session()->has('login_redirect') && session()->get('login_redirect') == 'hub') {
            session()->forget('login_redirect');
            return redirect()->route('redirect.to.hub.cart');
        }
        return redirect('/');
    }

    public function verifyGoogle2FA(Request $request){
         
        $this->validate($request, [
            'secret' => ['required', new Google2FaSecretKeyVerificationRule($this->google2FaServices, auth()->user()->google2fa_secret)]
        ],[
            'secret.required' => 'OTP is Required',
            'secret' => 'Your OTP is incorrect!'
        ]);
        session()->put('GoogleOtp' , '1');
        Mail::send(new WelcomeEmail($request->ip()));
        if (session()->has('login_redirect') && session()->get('login_redirect') == 'hub') {
            session()->forget('login_redirect');
            return redirect()->route('redirect.to.hub.cart');
        }
        return redirect('/');

    }










    // helper funcitons
    protected function checkServletSession(){
        $status = false;
        if(session()->has('servlet') and session()->get('servlet') == '1'){
            $status = true;
            // return redirect('/');
        }

        if(session()->has('GoogleOtp') and session()->get('GoogleOtp') == '0'){
            $status = true;
            // return redirect('/');
        }

        if(session()->has('GoogleOtp') and session()->get('GoogleOtp') == '1'){
            $status = true;
            // return redirect('/');
        } 

        return $status;
    }

    protected function checkGoogle2FaSession(){
        $status = false;
        if(session()->has('servlet') and session()->get('servlet') == '1'){
            $status = true;
            // return redirect('/');
        }

        if(session()->has('servlet') and session()->get('servlet') == '0'){
            $status = true;
            // return redirect('/');
        }

        if(session()->has('GoogleOtp') and session()->get('GoogleOtp') == '1'){
            $status = true;
            // return redirect('/');
        }
        return $status;
    }


}
