<?php

namespace App\Http\Controllers;

use App\Models\GeneralSetting;
use App\Models\LoginSecurity;
use Auth;
use Hash;
use Illuminate\Http\Request;
use App\Models\UserInterest;

class LoginSecurityController extends Controller
{
    public function __construct()
    {
        $general = GeneralSetting::first();
        $this->template = $general->theme == 1 ? 'frontend.' : "theme{$general->theme}.";
    }


    public function show2faForm(Request $request){
       
        $pageTitle = '2FA Authenticattion';
        $general = GeneralSetting::first();
        $user = Auth::user();
        $google2fa_url = "";
        $secret_key = "";
        $profit = UserInterest::where('user_id', Auth::id())->sum('interest_amount');

        if($user->loginSecurity()->exists()){
            $google2fa = (new \PragmaRX\Google2FAQRCode\Google2FA());
            $google2fa_url = $google2fa->getQRCodeInline(
                $general->site_name,
                $user->email,
                $user->loginSecurity->google2fa_secret
            );
            $secret_key = $user->loginSecurity->google2fa_secret;
            $profit = UserInterest::where('user_id', Auth::id())->sum('interest_amount');
        }

        $data = array(
            'user' => $user,
            'secret' => $secret_key,
            'google2fa_url' => $google2fa_url,
            'pageTitle' => $pageTitle
        );
$profit = UserInterest::where('user_id', Auth::id())->sum('interest_amount');

          return view($this->template.'user.2fa_settings', compact('data', 'profit'));
    }

    /**
     * Generate 2FA secret key
     */
    public function generate2faSecret(Request $request){
        $user = Auth::user();
        // Initialise the 2FA class
        $google2fa = (new \PragmaRX\Google2FAQRCode\Google2FA());

        // Add the secret key to the registration data
        $login_security = LoginSecurity::firstOrNew(array('user_id' => $user->id));
        $login_security->user_id = $user->id;
        $login_security->google2fa_enable = 0;
        $login_security->google2fa_secret = $google2fa->generateSecretKey();
        $login_security->save();

        return redirect()->route('user.2fa')->with('success',"Secret key is generated.");
    }

    /**
     * Enable 2FA
     */
    public function enable2fa(Request $request){
        $user = Auth::user();
        $google2fa = (new \PragmaRX\Google2FAQRCode\Google2FA());

        $secret = $request->input('secret');
        $valid = $google2fa->verifyKey($user->loginSecurity->google2fa_secret, $secret);

        if($valid){
            $user->loginSecurity->google2fa_enable = 1;
            $user->loginSecurity->save();
            return redirect()->route('user.2fa')->with('success',"2FA is enabled successfully.");
        }else{
            return redirect()->route('user.2fa')->with('error',"Invalid verification Code, Please try again.");
        }
    }

    /**
     * Disable 2FA
     */
    public function disable2fa(Request $request){
        if (!(Hash::check($request->get('current-password'), Auth::user()->password))) {

            return redirect()->back()->with("error","Your password does not matches with your account password. Please try again.");
        }

        $validatedData = $request->validate([
            'current-password' => 'required',
        ]);
        $user = Auth::user();
        $user->loginSecurity->google2fa_enable = 0;
        $user->loginSecurity->save();
        return redirect()->route('user.2fa')->with('success',"2FA is now disabled.");
    }
}
