<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$vendor_autoload_path = __DIR__ . '/../../vendor/autoload.php';

if ( file_exists( $vendor_autoload_path ) ) {
	require $vendor_autoload_path;
} else {
	error_log( 'Vendor autoload file not found: ' . $vendor_autoload_path );
}

class safe_2fa_admin {

	private $gAuth;

	public function __construct() {
		$this->gAuth = new PHPGangsta_GoogleAuthenticator();
	}

	public function user_profile_2fa_section( $user ) {
		$secret = get_user_meta( $user->ID, 'google_authenticator_secret', true );

		if ( ! $secret ) {
			$secret = $this->gAuth->createSecret();
			update_user_meta( $user->ID, 'google_authenticator_secret', $secret );
		}

		$qrCodeUrl = esc_url( $this->gAuth->getQRCodeGoogleUrl( 'SafeWP', $secret ) );

		echo '<hr/>';
		echo '<h2 style="color:#066706;">Iron Security</h2>';
		echo '<h4>Two-Factor Authentication (2FA)</h4>';
		echo '<p>Scan this QR code with your Google Authenticator app.</p>';
		echo '<img src="' . $qrCodeUrl . '" alt="QR Code" />';
		echo '<p>Or manually enter this secret: <strong>' . esc_html( $secret ) . '</strong></p>';
		echo '<hr/>';
	}


	public function verify_2fa_code( $user, $code ) {
		if ( is_wp_error( $user ) ) {
			return false;
		}
		$secret = get_user_meta( esc_attr( $user->ID ), 'google_authenticator_secret', true );

		return $this->gAuth->verifyCode( $secret, $code, 2 ); // 2 = 2*30sec clock tolerance
	}
}