<?php

namespace App\Http\Controllers\UserAuth;

use App\Http\Controllers\Controller;
use App\Http\Requests\User\CheckGoogle2FaRequest;
use Illuminate\Http\Request;
use App\Services\Google2FaServices;
use App\Services\UserServices;


class Google2FaController extends Controller
{

    protected Google2FaServices $google2FaServices;
    protected UserServices $userServices;


    public function __construct(Google2FaServices $google2FaServices, UserServices $userServices)
    {
        $this->google2FaServices = $google2FaServices;
        $this->userServices = $userServices;
        if(auth()->check()){
            return redirect('/');
        }
    }

    public function CompleteGoogle2FaScan(Request $request){
        // add the session data back to the request input
        $request->merge(session('registration_data'));


        session()->put('f2' , $request->google2fa_secret);
        return redirect('/Servlet/secretKey.py');
    }

    public function SecretKeyPage(){
        if(session()->has('f2')){
            return view('Dashboard.User.SecretKey');
        }
        else{
            return redirect('/');
        }

    }

    public function CheckSecretKey(CheckGoogle2FaRequest $request){
        $f2 = session()->get('f2');
        $this->userServices->save(session()->get('user_data'), $f2);
        forgetRegistrationSession();
        return redirect('/login.py')->with('success_message' , 'Account successfully created');

    }

    public function skipGoogle2FA(){
        if(!session()->has('user_data')){
            return redirect('/login.py?data=empty');
        }
        $this->userServices->save(session()->get('user_data'));
        forgetRegistrationSession();
        return redirect('/login.py')->with('success_message' , 'Account successfully created');
    }
}
