<?php
/**
 * Signup Class.
 *
 * @package automation-web-platform
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


if ( ! class_exists( 'WAWP_Signup' ) ) {

	/**
	 * Class WAWP_Signup
	 */
	class WAWP_Signup {
		use WAWP_Security_Trait;
		use WAWP_Phone_Helper_Trait;
		use WAWP_Settings_Handler;
		use WAWP_Logging_Trait;

		/**
		 * Instance.
		 *
		 * @var WAWP_Signup|null
		 */
		private static $instance = null;

		/**
		 * Nonce action.
		 *
		 * @var string
		 */
		private $nonce_action = 'wp_rest';

		/**
		 * Nonce field.
		 *
		 * @var string
		 */
		private $nonce_field = 'wawp_signup_nonce_field';

		/**
		 * Get instance.
		 *
		 * @return WAWP_Signup
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Constructor.
		 */
		public function __construct() {
			$this->init_hooks();
		}

		/**
		 * Init hooks.
		 */
		public function init_hooks() {
			add_shortcode( 'wawp_signup_form', array( $this, 'render_signup_form' ) );
			add_action( 'wp_login', array( $this, 'user_logged_in' ), 10, 2 );
			add_action( 'wawp_cron_clear_expired_otps', array( $this, 'clear_expired_otps' ) );
			add_action( 'show_user_profile', array( $this, 'add_custom_user_profile_fields' ) );
			add_action( 'edit_user_profile', array( $this, 'add_custom_user_profile_fields' ) );
			add_action( 'personal_options_update', array( $this, 'save_custom_user_profile_fields' ) );
			add_action( 'edit_user_profile_update', array( $this, 'save_custom_user_profile_fields' ) );
			add_filter( 'wawp_frontend_global_vars', array( $this, 'add_frontend_global_vars' ) );
		}

		/**
		 * Clone.
		 */
		public function __clone() {
		}

		/**
		 * Wakeup.
		 */
		public function __wakeup() {
		}

		/**
		 * Enqueue scripts.
		 */
		public function enqueue_scripts() {
			wp_enqueue_style( 'wawp-frontend-css', WAWP_PLUGIN_URL . 'assets/css/wawp-frontend.css', array(), filemtime( WAWP_PLUGIN_DIR . 'assets/css/wawp-frontend.css' ) );
			wp_enqueue_style( 'bootstrap-icons', WAWP_PLUGIN_URL . 'assets/css/resources/bootstrap-icons.css', array(), '1.11.3' );
			wp_enqueue_style( 'wawp-signup-style', WAWP_PLUGIN_URL . 'assets/css/wawp-signup.css', array(), WAWP_PLUGIN_VERSION );
			wp_enqueue_style( 'jquery-ui-autocomplete', WAWP_PLUGIN_URL . 'assets/css/resources/jquery-ui.css', array(), '1.14.1' );

			wp_enqueue_script( 'jquery-ui-autocomplete' );
			wp_enqueue_script( 'wawp-signup-script', WAWP_PLUGIN_URL . 'assets/js/wawp-signup.js', array( 'jquery', 'jquery-ui-autocomplete', 'wawp-global-data-hub' ), time(), true );

			$settings = $this->get_settings();

			$fb_configured = WAWP_Connector()->is_firebase_active();
			if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_ENABLE_OTP ] ) && $fb_configured ) {
				wp_enqueue_script( 'firebase-app', WAWP_PLUGIN_URL . 'assets/js/resources/firebase-app-compat.js', array(), '9.1.1', true );
				wp_enqueue_script( 'firebase-auth', WAWP_PLUGIN_URL . 'assets/js/resources/firebase-auth-compat.js', array( 'firebase-app' ), '9.1.1', true );
			}

			add_action( 'wp_head', array( $this, 'inject_custom_button_styles' ) );
		}

		/**
		 * Add global variables to the central data hub.
		 *
		 * @param array $vars Global variables array.
		 * @return array
		 */
		public function add_frontend_global_vars( $vars ) {
			$settings = $this->get_settings();

			$vars['WAWP_Signup_Params'] = array(
				'ajax_url'            => admin_url( 'admin-ajax.php' ),
				'rest_url'            => esc_url_raw( rest_url( 'wawp/v1/signup' ) ),
				'nonce'               => wp_create_nonce( $this->nonce_action ),
				'redirect_url'        => esc_url( $settings[ WAWP_Settings_Registry::SIGNUP_REDIRECT_URL ] ?? '' ),
				'otp_method'          => $settings[ WAWP_Settings_Registry::SIGNUP_OTP_METHOD ] ?? 'whatsapp',
				'password_strong'     => $settings[ WAWP_Settings_Registry::SIGNUP_STRONG_PASSWORD ] ?? 0,
				'password_enabled'    => ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_ENABLED ] ),
				'password_required'   => ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED ] ),
				'recaptcha_enabled'   => $this->get_wawp_setting( WAWP_Settings_Registry::GOOGLE_RECAPTCHA_ENABLE_WAWP_SIGNUP ),
				'recaptcha_site_key'  => $this->get_wawp_setting( WAWP_Settings_Registry::GOOGLE_RECAPTCHA_SITE_KEY ),
				'recaptcha_type'      => $this->get_wawp_setting( WAWP_Settings_Registry::GOOGLE_RECAPTCHA_TYPE ),
				'firebase_config'     => ( function () {
					if ( ! class_exists( 'WAWP_Firebase_Sender' ) && file_exists( WAWP_PLUGIN_DIR . 'includes/senders/class-wawp-firebase-sender.php' ) ) {
						include_once WAWP_PLUGIN_DIR . 'includes/senders/class-wawp-firebase-sender.php';
					}
					return class_exists( 'WAWP_Firebase_Sender' ) ? WAWP_Firebase_Sender::get_active_account() : array();
				} )(),
				'whatsapp_configured' => ( WAWP_Connector()->is_whatsapp_active() ? 1 : 0 ),
				'firebase_configured' => ( WAWP_Connector()->is_firebase_active() ? 1 : 0 ),
				'email_configured'    => ( function () {
					$smtp = $this->get_wawp_setting( WAWP_Settings_Registry::SMTP_SETTINGS, array() );
					return ! empty( $smtp['enabled'] ) ? 1 : 0;
				} )(),
				'email_enabled'       => ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_EMAIL_ENABLED ] ) ? 1 : 0,
				'fallback_enabled'    => ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_FALLBACK ] ) ? 1 : 0,
				'is_rtl'              => is_rtl(),
				'initial_cooldown'    => $settings[ WAWP_Settings_Registry::AUTH_INITIAL_COOLDOWN ] ?? 30,
				'max_cooldown'        => $settings[ WAWP_Settings_Registry::AUTH_MAX_COOLDOWN ] ?? 300,
				'polling_interval'    => $settings[ WAWP_Settings_Registry::AUTH_POLLING_INTERVAL ] ?? 1500,
				'email_domains'       => $settings[ WAWP_Settings_Registry::AUTH_EMAIL_DOMAINS ] ?? array(),
			);

			$vars['WAWP_Signup_L10n'] = array(
				'fieldRequired'         => esc_html__( 'This field is required.', 'automation-web-platform' ),
				'checkYourEmail'        => esc_html__( 'Check your Email', 'automation-web-platform' ),
				'checkYourWhatsApp'     => esc_html__( 'Check your WhatsApp', 'automation-web-platform' ),
				'checkYourSMS'          => esc_html__( 'Check your SMS', 'automation-web-platform' ),
				/* translators: %s: The recipient phone or email */
				'weSentCode'            => __( 'We’ve sent you a 6-digit code to <br><b>%s</b>', 'automation-web-platform' ),
				'resendCode'            => esc_html__( 'Resend Code', 'automation-web-platform' ),
				'wrongEmail'            => esc_html__( 'Wrong e-mail?', 'automation-web-platform' ),
				'reEnterEmail'          => esc_html__( 'Please re-enter your email', 'automation-web-platform' ),
				'wrongNumber'           => esc_html__( 'Wrong phone number?', 'automation-web-platform' ),
				'reEnterNumber'         => esc_html__( 'Re-enter phone number', 'automation-web-platform' ),
				'sendOTPWhatsApp'       => esc_html__( 'Send code via WhatsApp', 'automation-web-platform' ),
				'sendOTPSMS'            => esc_html__( 'Send code via SMS', 'automation-web-platform' ),
				/* translators: %s: number of seconds */
				'waitSeconds'           => esc_html__( 'Wait %s seconds', 'automation-web-platform' ),
				'sendingCode'           => esc_html__( 'Sending code...', 'automation-web-platform' ),
				'checkYourPhone'        => esc_html__( 'Check your Phone', 'automation-web-platform' ),
				'unexpectedError'       => esc_html__( 'An unexpected error occurred. Please try again.', 'automation-web-platform' ),
				'unexpectedErrorResend' => esc_html__( 'An unexpected error occurred while resending OTP. Please try again.', 'automation-web-platform' ),
				'invalidEmail'          => esc_html__( 'Please enter a valid email address.', 'automation-web-platform' ),
				'invalidNumber'         => esc_html__( 'Please enter a valid number.', 'automation-web-platform' ),
				'verifyYourIdentity'    => esc_html__( 'Verify Your Identity', 'automation-web-platform' ),
				'weSentSecureRequest'   => esc_html__( 'We sent a secure verification request to', 'automation-web-platform' ),
				'pleaseCheckDevice'     => esc_html__( 'Please check your device and approve it to continue.', 'automation-web-platform' ),
				'weSentLink'            => esc_html__( 'We sent a secure verification link to your email', 'automation-web-platform' ),
				'pleaseClickLink'       => esc_html__( 'Please check your inbox and click the link to verify.', 'automation-web-platform' ),
				'changeDetailsOrRetry'  => esc_html__( 'Change details or retry', 'automation-web-platform' ),
			);

			return $vars;
		}

		/**
		 * Get color presets.
		 *
		 * @return array
		 */
		public static function get_color_presets() {
			return array(
				'custom'   => array(
					'name'   => esc_html__( 'Custom / Current', 'automation-web-platform' ),
					'colors' => array(),
				),
				'default'  => array(
					'name'   => __( 'Default (WhatsApp Green)', 'automation-web-platform' ),
					'colors' => array(
						'whatsapp_button_color'           => '#22c55e',
						'whatsapp_button_text_color'      => '#ffffff',
						'firebase_button_color'           => '#3b82f6',
						'firebase_button_text_color'      => '#ffffff',
						'email_otp_button_color'          => '#22c55e',
						'email_otp_button_text_color'     => '#ffffff',
						'verify_button_color'             => '#3b82f6',
						'verify_button_text_color'        => '#ffffff',
						'resend_button_color'             => '#64748b',
						'resend_button_text_color'        => '#ffffff',
						'show_password_button_color'      => '#ef4444',
						'show_password_button_text_color' => '#ffffff',
					),
				),
				'wawp'     => array(
					'name'   => __( 'Wawp Official (Teal)', 'automation-web-platform' ),
					'colors' => array(
						'whatsapp_button_color'           => '#004444',
						'whatsapp_button_text_color'      => '#ffffff',
						'firebase_button_color'           => '#141b38',
						'firebase_button_text_color'      => '#ffffff',
						'email_otp_button_color'          => '#004444',
						'email_otp_button_text_color'     => '#ffffff',
						'verify_button_color'             => '#004444',
						'verify_button_text_color'        => '#ffffff',
						'resend_button_color'             => '#cbd5e1',
						'resend_button_text_color'        => '#0f172a',
						'show_password_button_color'      => '#141b38',
						'show_password_button_text_color' => '#ffffff',
					),
				),
				'ocean'    => array(
					'name'   => __( 'Modern Ocean (Blue/Teal)', 'automation-web-platform' ),
					'colors' => array(
						'whatsapp_button_color'           => '#0891b2',
						'whatsapp_button_text_color'      => '#ffffff',
						'firebase_button_color'           => '#2563eb',
						'firebase_button_text_color'      => '#ffffff',
						'email_otp_button_color'          => '#0891b2',
						'email_otp_button_text_color'     => '#ffffff',
						'verify_button_color'             => '#2563eb',
						'verify_button_text_color'        => '#ffffff',
						'resend_button_color'             => '#64748b',
						'resend_button_text_color'        => '#ffffff',
						'show_password_button_color'      => '#0f172a',
						'show_password_button_text_color' => '#ffffff',
					),
				),
				'midnight' => array(
					'name'   => __( 'Midnight (Dark/Purple)', 'automation-web-platform' ),
					'colors' => array(
						'whatsapp_button_color'           => '#7c3aed',
						'whatsapp_button_text_color'      => '#ffffff',
						'firebase_button_color'           => '#4f46e5',
						'firebase_button_text_color'      => '#ffffff',
						'email_otp_button_color'          => '#7c3aed',
						'email_otp_button_text_color'     => '#ffffff',
						'verify_button_color'             => '#4f46e5',
						'verify_button_text_color'        => '#ffffff',
						'resend_button_color'             => '#475569',
						'resend_button_text_color'        => '#ffffff',
						'show_password_button_color'      => '#1e293b',
						'show_password_button_text_color' => '#ffffff',
					),
				),
				'sunset'   => array(
					'name'   => __( 'Sunset (Orange/Red)', 'automation-web-platform' ),
					'colors' => array(
						'whatsapp_button_color'           => '#ea580c',
						'whatsapp_button_text_color'      => '#ffffff',
						'firebase_button_color'           => '#dc2626',
						'firebase_button_text_color'      => '#ffffff',
						'email_otp_button_color'          => '#ea580c',
						'email_otp_button_text_color'     => '#ffffff',
						'verify_button_color'             => '#dc2626',
						'verify_button_text_color'        => '#ffffff',
						'resend_button_color'             => '#78350f',
						'resend_button_text_color'        => '#ffffff',
						'show_password_button_color'      => '#991b1b',
						'show_password_button_text_color' => '#ffffff',
					),
				),
				'minimal'  => array(
					'name'   => __( 'Minimal (Black/Gray)', 'automation-web-platform' ),
					'colors' => array(
						'whatsapp_button_color'           => '#111827',
						'whatsapp_button_text_color'      => '#ffffff',
						'firebase_button_color'           => '#374151',
						'firebase_button_text_color'      => '#ffffff',
						'email_otp_button_color'          => '#111827',
						'email_otp_button_text_color'     => '#ffffff',
						'verify_button_color'             => '#111827',
						'verify_button_text_color'        => '#ffffff',
						'resend_button_color'             => '#9ca3af',
						'resend_button_text_color'        => '#111827',
						'show_password_button_color'      => '#4b5563',
						'show_password_button_text_color' => '#ffffff',
					),
				),
			);
		}

		/**
		 * Get settings.
		 *
		 * @return array
		 */
		public function get_settings() {
			static $cache = null;
			if ( null !== $cache ) {
				return $cache;
			}

			$defaults = array(
				WAWP_Settings_Registry::SIGNUP_SELECTED_INSTANCE => 0,
				WAWP_Settings_Registry::SIGNUP_ENABLE_OTP  => 1,
				WAWP_Settings_Registry::SIGNUP_OTP_METHOD  => 'whatsapp',
				WAWP_Settings_Registry::SIGNUP_OTP_MSG     => esc_html__( 'Your OTP code is: {{otp}}', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_OTP_MSG_EMAIL => esc_html__( 'Please click the link below to verify your identity: {{magic_link}}', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_OTP_SUBJECT_EMAIL => esc_html__( 'Verification Code from {{site_name}}', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_FALLBACK    => 1,
				WAWP_Settings_Registry::SIGNUP_FIELD_ORDER => 'first_name,last_name,email,phone,password',
				WAWP_Settings_Registry::SIGNUP_REDIRECT_URL => home_url(),
				WAWP_Settings_Registry::SIGNUP_LOGO        => '',
				WAWP_Settings_Registry::SIGNUP_TITLE       => esc_html__( 'Create Account', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_DESCRIPTION => esc_html__( 'Join our community to access exclusive features and automated tools.', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_COLOR_THEME => 'default',
				WAWP_Settings_Registry::SIGNUP_STRONG_PASSWORD => 0,
				WAWP_Settings_Registry::SIGNUP_PASSWORD_RESET => 1,
				WAWP_Settings_Registry::SIGNUP_AUTO_LOGIN  => 1,
				WAWP_Settings_Registry::SIGNUP_CUSTOM_CSS  => '',
				WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS => array(),
				WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE => array(),
				WAWP_Settings_Registry::SIGNUP_OTP_EMAIL_TEMPLATE_ID => '',
				WAWP_Settings_Registry::SIGNUP_PREMIUM_DESIGN => 0,
				WAWP_Settings_Registry::SIGNUP_BTN_BG_COLOR => '#0073aa',
				WAWP_Settings_Registry::SIGNUP_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_BTN_HOVER_BG_COLOR => '#005177',
				WAWP_Settings_Registry::SIGNUP_BTN_HOVER_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_WA_BTN_COLOR => '#22c55e',
				WAWP_Settings_Registry::SIGNUP_WA_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_FB_BTN_COLOR => '#3b82f6',
				WAWP_Settings_Registry::SIGNUP_FB_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_EMAIL_BTN_COLOR => '#22c55e',
				WAWP_Settings_Registry::SIGNUP_EMAIL_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_COLOR => '#3b82f6',
				WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_RESEND_BTN_COLOR => '#64748b',
				WAWP_Settings_Registry::SIGNUP_RESEND_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_COLOR => '#ef4444',
				WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_BACK_BTN_COLOR => '#64748b',
				WAWP_Settings_Registry::SIGNUP_BACK_BTN_TEXT_COLOR => '#ffffff',
				WAWP_Settings_Registry::SIGNUP_FIRST_NAME_ENABLED => 1,
				WAWP_Settings_Registry::SIGNUP_FIRST_NAME_REQUIRED => 1,
				WAWP_Settings_Registry::SIGNUP_LAST_NAME_ENABLED => 1,
				WAWP_Settings_Registry::SIGNUP_LAST_NAME_REQUIRED => 1,
				WAWP_Settings_Registry::SIGNUP_EMAIL_ENABLED => 1,
				WAWP_Settings_Registry::SIGNUP_EMAIL_REQUIRED => 1,
				WAWP_Settings_Registry::SIGNUP_PHONE_ENABLED => 1,
				WAWP_Settings_Registry::SIGNUP_PHONE_REQUIRED => 1,
				WAWP_Settings_Registry::SIGNUP_PASSWORD_ENABLED => 1,
				WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED => 1,
				WAWP_Settings_Registry::SIGNUP_WA_AUTH_TYPE => 'otp',
				WAWP_Settings_Registry::AUTH_INITIAL_COOLDOWN => 30,
				WAWP_Settings_Registry::AUTH_MAX_COOLDOWN  => 300,
				WAWP_Settings_Registry::AUTH_POLLING_INTERVAL => 1500,
				WAWP_Settings_Registry::AUTH_EMAIL_DOMAINS => array( 'gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'aol.com' ),
				WAWP_Settings_Registry::SIGNUP_WA_OTP_LANG_TYPE => 'site',
				WAWP_Settings_Registry::SIGNUP_WA_OTP_CUSTOM_LANG => 'ar',
				WAWP_Settings_Registry::SIGNUP_WA_OTP_FOOT_EN => 0,
				WAWP_Settings_Registry::SIGNUP_WA_OTP_FOOT => '',
				WAWP_Settings_Registry::SIGNUP_WA_LIST_TITLE => esc_html__( 'Secure Registration', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_DESC => esc_html__( 'Please confirm your identity to complete registration.', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_FOOTER => esc_html__( 'Secure authentication via Wawp', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_BUTTON => esc_html__( 'Choose Action', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM1 => esc_html__( 'Approve Signup', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM1_DESC => esc_html__( 'Verify this is you', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM2 => __( 'Reject & Stop', 'automation-web-platform' ),
				WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM2_DESC => esc_html__( 'Cancel this attempt', 'automation-web-platform' ),
			);

			// Load registration form settings object if it exists.
			$form_settings = $this->get_wawp_setting( WAWP_Settings_Registry::REGISTRATION_FORM_SETTINGS, array() );
			$form_settings = $this->from_db( $form_settings );
			if ( ! is_array( $form_settings ) ) {
				$form_settings = array();
			}

			// Map of pretty keys to database keys.
			$pretty_to_db = array(
				'selectedInstance'                => WAWP_Settings_Registry::SIGNUP_SELECTED_INSTANCE,
				'selected_instance'               => WAWP_Settings_Registry::SIGNUP_SELECTED_INSTANCE,
				'enable_otp'                      => WAWP_Settings_Registry::SIGNUP_ENABLE_OTP,
				'otp_method'                      => WAWP_Settings_Registry::SIGNUP_OTP_METHOD,
				'otp_message_whatsapp'            => WAWP_Settings_Registry::SIGNUP_OTP_MSG,
				'otp_message_email'               => WAWP_Settings_Registry::SIGNUP_OTP_MSG_EMAIL,
				'otp_subject_email'               => WAWP_Settings_Registry::SIGNUP_OTP_SUBJECT_EMAIL,
				'otp_email_template_id'           => WAWP_Settings_Registry::SIGNUP_OTP_EMAIL_TEMPLATE_ID,
				'enable_verification_fallback'    => WAWP_Settings_Registry::SIGNUP_FALLBACK,
				'field_order'                     => WAWP_Settings_Registry::SIGNUP_FIELD_ORDER,
				'signup_redirect_url'             => WAWP_Settings_Registry::SIGNUP_REDIRECT_URL,
				'signup_logo'                     => WAWP_Settings_Registry::SIGNUP_LOGO,
				'signup_title'                    => WAWP_Settings_Registry::SIGNUP_TITLE,
				'signup_description'              => WAWP_Settings_Registry::SIGNUP_DESCRIPTION,
				'color_theme'                     => WAWP_Settings_Registry::SIGNUP_COLOR_THEME,
				'enable_strong_password'          => WAWP_Settings_Registry::SIGNUP_STRONG_PASSWORD,
				'enable_password_reset'           => WAWP_Settings_Registry::SIGNUP_PASSWORD_RESET,
				'auto_login'                      => WAWP_Settings_Registry::SIGNUP_AUTO_LOGIN,
				'custom_css'                      => WAWP_Settings_Registry::SIGNUP_CUSTOM_CSS,
				'custom_fields'                   => WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS,
				'custom_shortcode'                => WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE,
				'enable_premium_design'           => WAWP_Settings_Registry::SIGNUP_PREMIUM_DESIGN,
				'first_name_enabled'              => WAWP_Settings_Registry::SIGNUP_FIRST_NAME_ENABLED,
				'first_name_required'             => WAWP_Settings_Registry::SIGNUP_FIRST_NAME_REQUIRED,
				'last_name_enabled'               => WAWP_Settings_Registry::SIGNUP_LAST_NAME_ENABLED,
				'last_name_required'              => WAWP_Settings_Registry::SIGNUP_LAST_NAME_REQUIRED,
				'email_enabled'                   => WAWP_Settings_Registry::SIGNUP_EMAIL_ENABLED,
				'email_required'                  => WAWP_Settings_Registry::SIGNUP_EMAIL_REQUIRED,
				'phone_enabled'                   => WAWP_Settings_Registry::SIGNUP_PHONE_ENABLED,
				'phone_required'                  => WAWP_Settings_Registry::SIGNUP_PHONE_REQUIRED,
				'password_enabled'                => WAWP_Settings_Registry::SIGNUP_PASSWORD_ENABLED,
				'password_required'               => WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED,
				'button_bg_color'                 => WAWP_Settings_Registry::SIGNUP_BTN_BG_COLOR,
				'button_text_color'               => WAWP_Settings_Registry::SIGNUP_BTN_TEXT_COLOR,
				'button_hover_bg_color'           => WAWP_Settings_Registry::SIGNUP_BTN_HOVER_BG_COLOR,
				'button_hover_text_color'         => WAWP_Settings_Registry::SIGNUP_BTN_HOVER_TEXT_COLOR,
				'whatsapp_button_color'           => WAWP_Settings_Registry::SIGNUP_WA_BTN_COLOR,
				'whatsapp_button_text_color'      => WAWP_Settings_Registry::SIGNUP_WA_BTN_TEXT_COLOR,
				'firebase_button_color'           => WAWP_Settings_Registry::SIGNUP_FB_BTN_COLOR,
				'firebase_button_text_color'      => WAWP_Settings_Registry::SIGNUP_FB_BTN_TEXT_COLOR,
				'email_otp_button_color'          => WAWP_Settings_Registry::SIGNUP_EMAIL_BTN_COLOR,
				'email_otp_button_text_color'     => WAWP_Settings_Registry::SIGNUP_EMAIL_BTN_TEXT_COLOR,
				'verify_button_color'             => WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_COLOR,
				'verify_button_text_color'        => WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_TEXT_COLOR,
				'resend_button_color'             => WAWP_Settings_Registry::SIGNUP_RESEND_BTN_COLOR,
				'resend_button_text_color'        => WAWP_Settings_Registry::SIGNUP_RESEND_BTN_TEXT_COLOR,
				'show_password_button_color'      => WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_COLOR,
				'show_password_button_text_color' => WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_TEXT_COLOR,
				'back_button_color'               => WAWP_Settings_Registry::SIGNUP_BACK_BTN_COLOR,
				'back_button_text_color'          => WAWP_Settings_Registry::SIGNUP_BACK_BTN_TEXT_COLOR,
				'whatsappAuthType'                => WAWP_Settings_Registry::SIGNUP_WA_AUTH_TYPE,
				'whatsappOtpLangType'             => WAWP_Settings_Registry::SIGNUP_WA_OTP_LANG_TYPE,
				'whatsappOtpCustomLang'           => WAWP_Settings_Registry::SIGNUP_WA_OTP_CUSTOM_LANG,
				'whatsappOtpFooterEnabled'        => WAWP_Settings_Registry::SIGNUP_WA_OTP_FOOT_EN,
				'whatsappOtpFooter'               => WAWP_Settings_Registry::SIGNUP_WA_OTP_FOOT,
				'whatsappListTitle'               => WAWP_Settings_Registry::SIGNUP_WA_LIST_TITLE,
				'whatsappListDesc'                => WAWP_Settings_Registry::SIGNUP_WA_LIST_DESC,
				'whatsappListFooter'              => WAWP_Settings_Registry::SIGNUP_WA_LIST_FOOTER,
				'whatsappListButton'              => WAWP_Settings_Registry::SIGNUP_WA_LIST_BUTTON,
				'whatsappListItem1'               => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM1,
				'whatsappListItem1Desc'           => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM1_DESC,
				'whatsappListItem2'               => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM2,
				'whatsappListItem2Desc'           => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM2_DESC,
				'initialCooldown'                 => WAWP_Settings_Registry::AUTH_INITIAL_COOLDOWN,
				'maxCooldown'                     => WAWP_Settings_Registry::AUTH_MAX_COOLDOWN,
				'pollingInterval'                 => WAWP_Settings_Registry::AUTH_POLLING_INTERVAL,
			);

			$settings = array();
			foreach ( $defaults as $key => $default ) {
				// 1. Try to read from the JSON form settings first.
				$val = null;
				// Search by db key.
				if ( isset( $form_settings[ $key ] ) ) {
					$val = $form_settings[ $key ];
				} else {
					// Search by pretty key.
					$pretty_key = array_search( $key, $pretty_to_db, true );
					if ( false !== $pretty_key && isset( $form_settings[ $pretty_key ] ) ) {
						$val = $form_settings[ $pretty_key ];
					}
				}

				// 2. Fallback to individual database option.
				if ( null === $val || '' === $val ) {
					$val = $this->get_wawp_setting( $key, $default );
				}

				if ( null === $val || '' === $val ) {
					$val = $default;
				}
				if ( WAWP_Settings_Registry::SIGNUP_OTP_METHOD === $key && 'wa' === $val ) {
					$val = 'whatsapp';
				}
				$settings[ $key ] = $val;

				// Map database key to pretty keys for compatibility with the general OTP Service.
				foreach ( $pretty_to_db as $pretty => $db_key ) {
					if ( $db_key === $key ) {
						$settings[ $pretty ] = $val;
					}
				}
			}

			if ( ! is_array( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE ] ) ) {
				$settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE ] = $this->from_db( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE ] );
			}

			if ( ! is_array( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ] ) ) {
				$settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ] = $this->from_db( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ] );
			}

			$cache = $settings;
			return $cache;
		}

		/**
		 * Get active instance.
		 *
		 * @return object|null
		 */
		private function get_active_instance() {
			$settings = $this->get_settings();
			$raw_ids  = $settings[ WAWP_Settings_Registry::SIGNUP_SELECTED_INSTANCE ] ?? '';

			if ( empty( $raw_ids ) ) {
				$online = WAWP()->instance_manager->get_online_instances_cached();
				return ! empty( $online ) ? $online[0] : null;
			}

			$ids = is_array( $raw_ids ) ? $raw_ids : explode( ',', (string) $raw_ids );
			$ids = array_filter( array_map( 'trim', $ids ) );

			if ( empty( $ids ) ) {
				$online = WAWP()->instance_manager->get_online_instances_cached();
				return ! empty( $online ) ? $online[0] : null;
			}

			$online_instances = array();
			foreach ( $ids as $id ) {
				// Try internal ID first.
				$inst = WAWP()->instance_manager->get_instance_by_id( (int) $id );

				// If not found, try external instance_id.
				if ( ! $inst ) {
					$inst = WAWP()->instance_manager->get_instance_by_instance_id( (string) $id );
				}

				if ( $inst && strtolower( (string) $inst->status ) === 'online' ) {
					$online_instances[] = $inst;
				}
			}

			if ( ! empty( $online_instances ) ) {
				return $online_instances[ array_rand( $online_instances ) ];
			}

			$online = WAWP()->instance_manager->get_online_instances_cached();
			return ! empty( $online ) ? $online[0] : null;
		}

		/**
		 * Render signup form.
		 *
		 * @param array $atts Shortcode attributes.
		 * @return string
		 */
		public function render_signup_form( $atts = array() ) {
			$atts = shortcode_atts(
				array(
					'hide_login_link' => 'false',
				),
				$atts,
				'wawp_signup_form'
			);

			$conn_status = WAWP_Connector()->get_connection_status();
			$site_active = is_array( $conn_status ) && ! empty( $conn_status['siteActive'] );
			$banned      = is_array( $conn_status ) && ! empty( $conn_status['banned'] );
			if ( $banned ) {
				$msg = class_exists( 'WAWP_Global_Messages' ) ? WAWP_Global_Messages::get( 'blocked_generic' ) : esc_html__( 'Your account or site has been blocked.', 'automation-web-platform' );
				return '<p class="wawp-text-red">' . esc_html( $msg ) . '</p>';
			}

			$settings = $this->get_settings();
			if ( ! $site_active ) {
				// Bypass OTP options and force standard Email/Password fields.
				$settings[ WAWP_Settings_Registry::SIGNUP_ENABLE_OTP ]        = 0;
				$settings[ WAWP_Settings_Registry::SIGNUP_EMAIL_ENABLED ]     = 1;
				$settings[ WAWP_Settings_Registry::SIGNUP_EMAIL_REQUIRED ]    = 1;
				$settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_ENABLED ]  = 1;
				$settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED ] = 1;
			}

			$password_strong = isset( $settings[ WAWP_Settings_Registry::SIGNUP_STRONG_PASSWORD ] ) ? $settings[ WAWP_Settings_Registry::SIGNUP_STRONG_PASSWORD ] : 0;
			$this->enqueue_scripts();

			if ( is_user_logged_in() ) {
				return '<p>' . esc_html__( 'You are already logged in.', 'automation-web-platform' ) . '</p>';
			}

			$is_active = true;
			if ( $site_active && ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_ENABLE_OTP ] ) && 'whatsapp' === $settings[ WAWP_Settings_Registry::SIGNUP_OTP_METHOD ] ) {
				$active_instance = $this->get_active_instance();
				if ( ! $active_instance ) {
					$is_active = false;
				}
			}

			if ( ! $is_active && ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_ENABLE_OTP ] ) ) {
				return '<p>' . esc_html__( 'Verification services are currently unavailable. Please contact the administrator.', 'automation-web-platform' ) . '</p>';
			}

			$field_order = isset( $settings[ WAWP_Settings_Registry::SIGNUP_FIELD_ORDER ] ) ? explode( ',', $settings[ WAWP_Settings_Registry::SIGNUP_FIELD_ORDER ] ) : array();

			if ( ! $site_active ) {
				if ( ! in_array( 'email', $field_order, true ) ) {
					$field_order[] = 'email';
				}
				if ( ! in_array( 'password', $field_order, true ) ) {
					$field_order[] = 'password';
				}
			}

			$standard_fields_renderers = array(
				'first_name' => function () use ( $settings ) {
					if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_FIRST_NAME_ENABLED ] ) ) {
						echo '<div class="wawp-form-group">
                                <label for="wawp_first_name">' . esc_html__( 'First Name', 'automation-web-platform' ) . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_FIRST_NAME_REQUIRED ] ) ? '<span class="wawp-required">*</span>' : '' ) . '</label>
                                <input type="text" name="wawp_first_name" id="wawp_first_name" class="wawp-form-control" ' . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_FIRST_NAME_REQUIRED ] ) ? 'required' : '' ) . ' />
                                <div class="wawp-error-message"></div>
                              </div>';
					}
				},
				'last_name'  => function () use ( $settings ) {
					if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_LAST_NAME_ENABLED ] ) ) {
						echo '<div class="wawp-form-group">
                                <label for="wawp_last_name">' . esc_html__( 'Last Name', 'automation-web-platform' ) . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_LAST_NAME_REQUIRED ] ) ? '<span class="wawp-required">*</span>' : '' ) . '</label>
                                <input type="text" name="wawp_last_name" id="wawp_last_name" class="wawp-form-control" ' . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_LAST_NAME_REQUIRED ] ) ? 'required' : '' ) . ' />
                                <div class="wawp-error-message"></div>
                              </div>';
					}
				},
				'email'      => function () use ( $settings ) {
					if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_EMAIL_ENABLED ] ) ) {
						echo '<div class="wawp-form-group">
                                <label for="wawp_email">' . esc_html__( 'Email', 'automation-web-platform' ) . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_EMAIL_REQUIRED ] ) ? '<span class="wawp-required">*</span>' : '' ) . '</label>
                                <input type="email" name="wawp_email" id="wawp_email" class="wawp-form-control" ' . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_EMAIL_REQUIRED ] ) ? 'required' : '' ) . ' placeholder="' . esc_attr__( 'Enter your email address', 'automation-web-platform' ) . '" autocomplete="off" />
                                <div class="wawp-error-message"></div>
                              </div>';
					}
				},
				'phone'      => function () use ( $settings ) {
					if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PHONE_ENABLED ] ) ) {
						echo '<div class="wawp-form-group">
                                <label for="wawp_phone">' . esc_html__( 'Phone', 'automation-web-platform' ) . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PHONE_REQUIRED ] ) ? '<span class="wawp-required">*</span>' : '' ) . '</label>
                                <input type="tel" name="wawp_phone" id="wawp_phone" class="wawp-form-control" placeholder="' . esc_attr__( 'Enter your phone number', 'automation-web-platform' ) . '" autocomplete="tel" inputmode="numeric" pattern="[0-9]*" ' . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PHONE_REQUIRED ] ) ? 'required' : '' ) . ' />
                                <div class="wawp-error-message"></div>
                              </div>';
					}
				},
				'password'   => function () use ( $settings, $password_strong ) {
					if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_ENABLED ] ) ) {
						echo '<div class="wawp-form-group">
                                <label for="wawp_password">' . esc_html__( 'Password', 'automation-web-platform' ) . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED ] ) ? '<span class="wawp-required">*</span>' : '' ) . '</label>
                                <div class="wawp-password-container">
                                    <input type="password" name="wawp_password" id="wawp_password" class="wawp-form-control" ' . ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED ] ) ? 'required' : '' ) . ' />
                                    <span class="wawp-toggle-password"><i class="ri-eye-line"></i></span>
                                </div>
                                <div id="password-strength-bar"><div id="password-strength-meter" class="wawp-password-strength"></div></div>
                                <div id="password-strength-text" class="wawp-password-strength-text"></div>';
						if ( $password_strong ) {
							echo '<ul id="password-requirements" class="wawp-password-requirements">
                                    <span class="wawp-w100">' . esc_html__( 'Your password must contain:', 'automation-web-platform' ) . '</span>
                                    <li class="wawp-requirement"><span class="wawp-check"></span>' . esc_html__( 'Upper case letter (A-Z)', 'automation-web-platform' ) . '</li>
                                    <li class="wawp-requirement"><span class="wawp-check"></span>' . esc_html__( 'Lower case letter (a-z)', 'automation-web-platform' ) . '</li>
                                    <li class="wawp-requirement"><span class="wawp-check"></span>' . esc_html__( 'Numbers (0-9)', 'automation-web-platform' ) . '</li>
                                    <li class="wawp-requirement"><span class="wawp-check"></span>' . esc_html__( 'At least 8 characters', 'automation-web-platform' ) . '</li>
                                    <li class="wawp-requirement"><span class="wawp-check"></span>' . esc_html__( 'Special characters (e.g. !@#$%^&*)', 'automation-web-platform' ) . '</li>
                                  </ul>';
						}
						echo '<div class="wawp-error-message"></div></div>';
					}
				},
			);

			ob_start();
			echo '<div id="wawp-signup-container">';
			if ( ! $site_active ) {
				echo '<div class="wawp-error-message" style="background-color: #fef2f2; border: 1px solid #fca5a5; color: #b91c1c; padding: 12px; border-radius: 6px; margin-bottom: 20px; font-size: 14px; text-align: center; direction: rtl;">';
				echo esc_html__( 'Site connection is not active. Local signup enabled.', 'automation-web-platform' );
				echo '</div>';
			}
			echo '<div id="wawp-signup-branding">';
			$logo_url = $settings[ WAWP_Settings_Registry::SIGNUP_LOGO ] ?? '';
			if ( $logo_url ) {
				echo '<div class="wawp-signup-logo-container"><img src="' . esc_url( $logo_url ) . '" alt="" class="wawp-signup-logo wawp-maxw-200"/></div>';
			}
			$title = $settings[ WAWP_Settings_Registry::SIGNUP_TITLE ] ?? '';
			if ( $title ) {
				echo '<h3 class="wawp-signup-title">' . esc_html( $title ) . '</h3>';
			}
			$description = $settings[ WAWP_Settings_Registry::SIGNUP_DESCRIPTION ] ?? '';
			if ( $description ) {
				echo '<p class="wawp-description">' . wp_kses_post( $description ) . '</p>';
			}
			echo '</div>';

			global $wawp_recaptcha_rendered;
			if ( $this->get_wawp_setting( WAWP_Settings_Registry::GOOGLE_RECAPTCHA_ENABLE_WAWP_SIGNUP ) && empty( $wawp_recaptcha_rendered['signup'] ) ) {
				echo '<div class="wawp-recaptcha-container" id="wawp-recaptcha-signup"></div>';
				$wawp_recaptcha_rendered['signup'] = true;
			}

			if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_ENABLE_OTP ] ) && WAWP_Connector()->is_firebase_active() ) {
				echo '<div id="wawp-firebase-recaptcha-signup" class="wawp-mb-20"></div>';
			}

			echo '<form id="wawp-signup-form" class="wawp-form">';
			echo '<input type="hidden" name="' . esc_attr( $this->nonce_field ) . '" value="' . esc_attr( wp_create_nonce( $this->nonce_action ) ) . '" />';

			foreach ( $field_order as $field_key ) {
				if ( isset( $standard_fields_renderers[ $field_key ] ) ) {
					$standard_fields_renderers[ $field_key ]();
				} elseif ( isset( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ][ $field_key ] ) && $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ][ $field_key ]['enabled'] ) {
					$field         = $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ][ $field_key ];
					$field_id      = 'wawp_custom_field_' . esc_attr( $field_key );
					$field_name    = 'wawp_custom_fields[' . esc_attr( $field_key ) . ']';
					$is_required   = ! empty( $field['required'] ) ? ' required' : '';
					$required_span = ! empty( $field['required'] ) ? '<span class="wawp-required">*</span>' : '';

					echo '<div class="wawp-form-group">
                            <label for="' . esc_attr( $field_id ) . '">' . esc_html( $field['label'] ) . wp_kses_post( $required_span ) . '</label>';
					switch ( $field['type'] ) {
						case 'text':
						case 'email':
						case 'number':
							echo '<input type="' . esc_attr( $field['type'] ) . '" name="' . esc_attr( $field_name ) . '" id="' . esc_attr( $field_id ) . '" class="wawp-form-control" ' . esc_attr( $is_required ) . ' />';
							break;
						case 'textarea':
							echo '<textarea name="' . esc_attr( $field_name ) . '" id="' . esc_attr( $field_id ) . '" class="wawp-form-control" ' . esc_attr( $is_required ) . '></textarea>';
							break;
						case 'checkbox':
							if ( ! empty( $field['options'] ) && is_array( $field['options'] ) ) {
								foreach ( $field['options'] as $option ) {
									echo '<label class="wawp-checkbox-label">
                                            <input type="checkbox" name="' . esc_attr( $field_name ) . '[]" value="' . esc_attr( $option['value'] ) . '" class="wawp-form-control-checkbox" ' . esc_attr( $is_required ) . ' /> ' . esc_html( $option['label'] ) . '
                                          </label><br />';
								}
							}
							break;
						case 'radio':
							if ( ! empty( $field['options'] ) && is_array( $field['options'] ) ) {
								foreach ( $field['options'] as $option ) {
									echo '<label class="wawp-radio-label">
                                            <input type="radio" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $option['value'] ) . '" class="wawp-form-control-radio" ' . esc_attr( $is_required ) . ' /> ' . esc_html( $option['label'] ) . '
                                          </label><br />';
								}
							}
							break;
					}
					echo '<div class="wawp-error-message"></div></div>';
				}
			}

			WAWP_OTP_Service::get_instance()->render_dynamic_styles( $settings );
			echo '<div class="wawp-form-group"><button type="submit" class="wawp-submit-button wawp-primary-btn wawp-btn"><i class="ri-user-add-line"></i> ' . esc_html__( 'Create New Account', 'automation-web-platform' ) . '</button></div>';
			echo '</form>';

			if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_ENABLE_OTP ] ) ) {
				echo '<div id="wawp-otp-section" class="wawp-hide">
                        <div class="wawp-otp-header">
                            <div class="wawp-icon-wrapper"><i id="otp-icon"></i></div>
                            <h3 id="wawp-otp-sent-heading"></h3>
                            <p id="wawp-otp-sent-message"></p>
                        </div>
                        <form id="wawp-otp-form" class="wawp-form">
                            <input type="hidden" name="' . esc_attr( $this->nonce_field ) . '" value="' . esc_attr( wp_create_nonce( $this->nonce_action ) ) . '" />
                            <input type="hidden" name="wawp_otp_nonce_field" value="' . esc_attr( wp_create_nonce( $this->nonce_action ) ) . '" />
                            <div class="wawp-form-group">
                                <input type="text" name="wawp_otp_code" id="wawp_otp_code" class="wawp-form-control" placeholder="' . esc_attr__( 'Enter OTP code..', 'automation-web-platform' ) . '" inputmode="numeric" pattern="[0-9]*" autocomplete="one-time-code" maxlength="6" required />
                            </div>
                            <div class="wawp-error-message"></div>
                            <div class="wawp-form-group wawp-verify-row" style="margin-top: 10px;">
                                <button type="submit" class="wawp-submit-button wawp-verify-btn wawp-btn" style="flex: 65;">' . esc_html__( 'Confirm', 'automation-web-platform' ) . '</button>
                                <button type="button" id="wawp-resend-otp-btn" class="wawp-resend-otp-btn wawp-btn wawp-resend-btn" style="flex: 35;">' . esc_html__( 'Resend Code', 'automation-web-platform' ) . '</button>
                            </div>
                            <input type="hidden" id="wawp_otp_transient" name="wawp_otp_transient" value="" />
                        </form>
                        <p class="wawp-otp-resend"><span id="wawp-resend-message"></span><a id="wawp-edit-contact-btn" class="wawp-edit-contact-btn"></a></p>
                      </div>';
			}

			echo '<div class="wawp-success-message wawp-hide"></div>';
			echo '<div class="wawp-password-reset-status wawp-hide wawp-text-blue wawp-mt-10"></div>';

			if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE ] ) && is_array( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE ] ) ) {
				echo '<div class="wawp-separator"><span>' . esc_html__( 'OR', 'automation-web-platform' ) . '</span></div>';
				echo '<div class="wawp-custom-shortcodes">';
				foreach ( $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE ] as $sc_item ) {
					if ( ! empty( $sc_item ) ) {
						echo '<div class="wawp-custom-shortcode">' . do_shortcode( $sc_item ) . '</div>';
					}
				}
				echo '</div>';
			}
			echo '</div>';

			$login_page_id = $this->get_wawp_setting( WAWP_Settings_Registry::LOGIN_PAGE_ID );
			if ( $login_page_id && ! is_page( $login_page_id ) && 'true' !== $atts['hide_login_link'] ) {
				$login_url = get_permalink( $login_page_id );
				/* translators: %s: Login URL */
				echo '<p class="wawp-form-footer-link" style="text-align: center; margin-top: 20px;">' . wp_kses_post( sprintf( __( "Already have an account? <a href='%s' class='wawp-back-btn'>Login</a>", 'automation-web-platform' ), esc_url( $login_url ) ) ) . '</p>';
			}

			return ob_get_clean();
		}

		/**
		 * AJAX signup form submit return.
		 *
		 * @param array $params Parameters.
		 * @return mixed
		 */
		public function ajax_signup_form_submit_return( $params ) {
			return WAWP_OTP_Service::get_instance()->handle_signup_submit_return(
				array_merge(
					$params,
					array(
						'nonce_field'  => $this->nonce_field,
						'nonce_action' => $this->nonce_action,
						'settings'     => $this->get_settings(),
					)
				),
				array( $this, 'finish_registration_return' )
			);
		}

		/**
		 * AJAX check availability return.
		 *
		 * @param array $params Parameters.
		 * @return array
		 */
		public function ajax_check_availability_return( $params ) {
			$type  = $params['type'] ?? '';
			$value = $params['value'] ?? '';

			if ( empty( $type ) && empty( $value ) ) {
				if ( isset( $params['email'] ) ) {
					$type  = 'email';
					$value = $params['email'];
				} elseif ( isset( $params['wawp_email'] ) ) {
					$type  = 'email';
					$value = $params['wawp_email'];
				} elseif ( isset( $params['phone'] ) ) {
					$type  = 'phone';
					$value = $params['phone'];
				} elseif ( isset( $params['wawp_phone'] ) ) {
					$type  = 'phone';
					$value = $params['wawp_phone'];
				}
			}

			if ( empty( $type ) || empty( $value ) ) {
				return array(
					'success' => false,
					'message' => esc_html__( 'Missing parameters.', 'automation-web-platform' ),
				);
			}

			if ( 'email' === $type ) {
				if ( ! is_email( $value ) ) {
					$this->log_action( 'failed', "Signup: Invalid email check: {$value}", array( 'email' => $value ), 'signup' );
					return array(
						'success' => false,
						'message' => esc_html__( 'Invalid email address.', 'automation-web-platform' ),
					);
				}
				if ( email_exists( $value ) ) {
					$this->log_action( 'failed', "Signup: Email already exists check: {$value}", array( 'email' => $value ), 'signup' );
					return array(
						'success' => false,
						'message' => esc_html__( 'Email already in use.', 'automation-web-platform' ),
					);
				}
			} elseif ( 'phone' === $type ) {
				if ( ! $this->is_valid_phone( $value ) ) {
					$this->log_action( 'failed', "Signup: Invalid phone check: {$value}", array( 'phone' => $value ), 'signup' );
					return array(
						'success' => false,
						'message' => esc_html__( 'Invalid phone number.', 'automation-web-platform' ),
					);
				}
				if ( WAWP()->user_manager->get_user_by_phone( $value ) ) {
					$this->log_action( 'failed', "Signup: Phone already exists check: {$value}", array( 'phone' => $value ), 'signup' );
					return array(
						'success' => false,
						'message' => esc_html__( 'Phone number already in use.', 'automation-web-platform' ),
					);
				}
			}

			return array( 'success' => true );
		}

		/**
		 * AJAX check auth status return.
		 *
		 * @param array $params Parameters.
		 * @return mixed
		 */
		public function ajax_check_auth_status_return( $params ) {
			if ( isset( $params['wawp_otp_transient'] ) ) {
				$params['otp_transient'] = $params['wawp_otp_transient'];
			}
			return WAWP_OTP_Service::get_instance()->check_status_return(
				array_merge(
					$params,
					array(
						'nonce_field'     => 'wawp_otp_nonce_field',
						'nonce_action'    => $this->nonce_action,
						'transient_field' => 'wawp_otp_transient',
						'settings'        => $this->get_settings(),
					)
				),
				array( $this, 'finish_registration_return' )
			);
		}

		/**
		 * AJAX verify OTP return.
		 *
		 * @param array $params Parameters.
		 * @return mixed
		 */
		public function ajax_verify_otp_return( $params ) {
			if ( ! empty( $params['idToken'] ) ) {
				return $this->ajax_verify_firebase_token_return( $params );
			}
			if ( isset( $params['wawp_otp_code'] ) ) {
				$params['otp'] = $params['wawp_otp_code'];
			}
			if ( isset( $params['wawp_otp_transient'] ) ) {
				$params['otp_transient'] = $params['wawp_otp_transient'];
			}
			return WAWP_OTP_Service::get_instance()->verify_otp_return(
				array_merge(
					$params,
					array(
						'nonce_field'     => 'wawp_otp_nonce_field',
						'nonce_action'    => $this->nonce_action,
						'transient_field' => 'wawp_otp_transient',
						'settings'        => $this->get_settings(),
					)
				),
				array( $this, 'finish_registration_return' )
			);
		}

		/**
		 * AJAX resend OTP return.
		 *
		 * @param array $params Parameters.
		 * @return mixed
		 */
		public function ajax_resend_otp_return( $params ) {
			if ( isset( $params['wawp_otp_transient'] ) ) {
				$params['otp_transient'] = $params['wawp_otp_transient'];
			}
			return WAWP_OTP_Service::get_instance()->resend_return(
				array_merge(
					$params,
					array(
						'settings'        => $this->get_settings(),
						'context'         => 'Signup',
						'transient_field' => 'wawp_otp_transient',
					)
				)
			);
		}

		/**
		 * AJAX verify Firebase token return.
		 *
		 * @param array $params Parameters.
		 * @return mixed
		 */
		public function ajax_verify_firebase_token_return( $params ) {
			return WAWP_OTP_Service::get_instance()->verify_firebase_return(
				array_merge(
					$params,
					array(
						'nonce_field'  => 'nonce',
						'nonce_action' => $this->nonce_action,
					)
				),
				array( $this, 'finish_registration_return' )
			);
		}

		/**
		 * Finish registration return.
		 *
		 * @param array  $stored_data   Stored data.
		 * @param string $transient_key Transient key.
		 * @return mixed
		 */
		public function finish_registration_return( $stored_data, $transient_key = '' ) {
			return $this->finish_registration_logic( $stored_data, $transient_key );
		}

		/**
		 * Finish registration logic.
		 *
		 * @param array  $stored_data   Stored data.
		 * @param string $transient_key Transient key.
		 * @return array
		 */
		private function finish_registration_logic( $stored_data, $transient_key = '' ) {
			$settings = $this->get_settings();

			$username   = $stored_data['username'] ?? '';
			$email      = $stored_data['email'] ?? '';
			$first_name = $stored_data['first_name'] ?? '';
			$last_name  = $stored_data['last_name'] ?? '';
			$password   = $stored_data['password'] ?? '';
			$phone      = $stored_data['phone'] ?? '';

			if ( empty( $password ) ) {
				$password = wp_generate_password();
			}

			if ( empty( $username ) ) {
				if ( ! empty( $email ) ) {
					$username = $email;
				} elseif ( ! empty( $phone ) ) {
					$username = $phone;
				}
			}

			$userdata = array(
				'user_login' => $username,
				'user_email' => $email,
				'user_pass'  => $password,
				'first_name' => $first_name,
				'last_name'  => $last_name,
			);

			$user_id = wp_insert_user( $userdata );

			if ( is_wp_error( $user_id ) ) {
				return array(
					'success' => false,
					'message' => $user_id->get_error_message(),
				);
			}

			WAWP()->user_manager->insert_user_info( $user_id, $first_name, $last_name, $email, $phone, $password );

			update_user_meta( $user_id, 'first_name', $first_name );
			update_user_meta( $user_id, 'last_name', $last_name );

			if ( ! empty( $phone ) ) {
				WAWP()->connector->sync_user_phone( $user_id, $phone );
			}

			if ( ! empty( $stored_data['custom_fields_data'] ) && is_array( $stored_data['custom_fields_data'] ) ) {
				foreach ( $stored_data['custom_fields_data'] as $meta_key => $value ) {
					update_user_meta( $user_id, $meta_key, $value );
				}
			}

			$actual_method = $stored_data['otp_method'] ?? $settings[ WAWP_Settings_Registry::SIGNUP_OTP_METHOD ] ?? 'whatsapp';
			WAWP()->user_manager->update_user_verification( $user_id, $actual_method, true );

			if ( $transient_key ) {
				if ( is_array( $stored_data ) ) {
					$stored_data['status']  = 'verified';
					$stored_data['user_id'] = $user_id;
					set_transient( $transient_key, $stored_data, 30 );
				} else {
					delete_transient( $transient_key );
				}
				delete_transient( $transient_key . '_attempts' );
			}

			do_action( 'wawp_after_user_signup', $user_id, $userdata );

			$combined_status_message = '';
			if ( empty( $stored_data['password'] ) && ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_PASSWORD_RESET ] ) ) {
				$user_obj = get_user_by( 'id', $user_id );
				if ( $user_obj ) {
					$reset_key = get_password_reset_key( $user_obj );
					if ( ! is_wp_error( $reset_key ) ) {
						$reset_url         = network_site_url( "wp-login.php?action=rp&key={$reset_key}&login=" . rawurlencode( $stored_data['username'] ), 'login' );
						$reset_link_status = array();
						if ( ! empty( $stored_data['email'] ) ) {
							$sent = wawp_messenger()->send(
								$stored_data['email'],
								array(
									'channel' => 'email',
									'subject' => esc_html__( 'Set Your Password', 'automation-web-platform' ),
									'message' => esc_html__( 'Please set your password using the following link:', 'automation-web-platform' ) . ' ' . $reset_url,
									'user_id' => $user_id,
									'context' => esc_html__( 'Signup Password Reset', 'automation-web-platform' ),
								)
							);
							if ( $sent ) {
								$reset_link_status['email'] = esc_html__( 'Password reset link sent via Email.', 'automation-web-platform' );
							}
						}
						if ( ! empty( $stored_data['phone'] ) ) {
							$sent = wawp_messenger()->send(
								$stored_data['phone'],
								array(
									'channel' => 'whatsapp',
									'message' => esc_html__( 'Please set your password using the following link:', 'automation-web-platform' ) . ' ' . $reset_url,
									'user_id' => $user_id,
									'context' => esc_html__( 'Signup Password Reset', 'automation-web-platform' ),
								)
							);
							if ( $sent ) {
								$reset_link_status['whatsapp'] = esc_html__( 'Password reset link sent via WhatsApp.', 'automation-web-platform' );
							}
						}
						$combined_status_message = implode( ' ', $reset_link_status );
					}
				}
			}

			if ( ! empty( $settings[ WAWP_Settings_Registry::SIGNUP_AUTO_LOGIN ] ) ) {
				wp_set_current_user( $user_id );
				wp_set_auth_cookie( $user_id );
				$user_info = get_user_by( 'id', $user_id );
				WAWP()->unified_log->log_auth(
					$user_id,
					'signup',
					$actual_method,
					'success',
					array(
						'email' => $stored_data['email'] ?? '',
						'phone' => $stored_data['phone'] ?? '',
					)
				);
				// Skip the next login log because we just logged a signup event.
				if ( class_exists( 'WAWP_Unified_Log' ) ) {
					WAWP_Unified_Log::$skip_next_login_log = true;
				}

				do_action( 'wawp_user_logged_in', $user_info->ID );
				call_user_func( 'do_action', 'wp_login', $user_info->user_login, $user_info );

			}

			$response = array(
				'message'      => esc_html__( 'Signup successful! Redirecting...', 'automation-web-platform' ),
				'redirect_url' => $settings[ WAWP_Settings_Registry::SIGNUP_REDIRECT_URL ] ?? home_url(),
				'verified'     => true,
			);
			if ( ! empty( $combined_status_message ) ) {
				$response['password_reset_status'] = $combined_status_message;
			}

			return array(
				'success' => true,
				'message' => esc_html__( 'Signup successful!', 'automation-web-platform' ),
				'data'    => $response,
			);
		}

		/**
		 * Check if valid phone.
		 *
		 * @param string $phone Phone.
		 * @return bool
		 */
		private function is_valid_phone( $phone ) {
			return preg_match( '/^\+?[0-9]{7,15}$/', $phone );
		}

		/**
		 * User logged in hook.
		 *
		 * @param string  $user_login User login.
		 * @param WP_User $user       User object.
		 */
		public function user_logged_in( $user_login, $user ) {
			do_action( 'wawp_user_logged_in', $user->ID );
		}

		/**
		 * Inject custom button styles.
		 */
		public function inject_custom_button_styles() {
			$settings    = $this->get_settings();
			$dynamic_css = '
                #wawp-signup-container .wawp-primary-btn {
                    background-color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_BTN_BG_COLOR ] ) . ' !important;
                    color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_BTN_TEXT_COLOR ] ) . ' !important;
                }
                #wawp-signup-container .wawp-primary-btn:hover {
                    background-color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_BTN_HOVER_BG_COLOR ] ) . ' !important;
                    color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_BTN_HOVER_TEXT_COLOR ] ) . ' !important;
                }
                #wawp-signup-container .wawp-resend-otp-btn {
                    background-color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_RESEND_BTN_COLOR ] ) . ' !important;
                    color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_RESEND_BTN_TEXT_COLOR ] ) . ' !important;
                    border-color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_RESEND_BTN_COLOR ] ) . ' !important;
                }
                #wawp-signup-container .wawp-toggle-password {
                    color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_COLOR ] ) . ' !important;
                }
                #wawp-signup-container .wawp-verify-btn {
                    background-color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_COLOR ] ) . ' !important;
                    color: ' . esc_attr( $settings[ WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_TEXT_COLOR ] ) . ' !important;
                }
            ';
			wp_add_inline_style( 'wawp-signup-style', $dynamic_css );
		}

		/**
		 * Clear expired OTPs.
		 */
		public function clear_expired_otps() {
			global $wpdb;
			$table = $wpdb->prefix . 'options';

			WAWP()->db->execute_query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name LIKE %s', $table, $wpdb->esc_like( '_transient_wawp_signup_otp_' ) . '%' ) );
			wp_cache_flush();
		}

		/**
		 * Get React settings data.
		 *
		 * @return array
		 */
		public function get_react_settings_data() {
			static $cached_result = null;
			if ( null !== $cached_result ) {
				return $cached_result;
			}

			$rows      = WAWP()->instance_manager->get_online_instances_cached();
			$instances = array();
			if ( ! empty( $rows ) ) {
				foreach ( $rows as $r ) {
					$instances[] = array(
						'instance_id' => $r->instance_id,
						'name'        => $r->name,
					);
				}
			}

			global $wpdb;
			$table     = $wpdb->prefix . 'wawp_email_templates';
			$cache_key = 'wawp_email_templates_list_v2';
			$templates = wp_cache_get( $cache_key, 'wawp_database' );

			if ( false === $templates ) {
				$templates = WAWP()->db->get_results_cached( $wpdb->prepare( 'SELECT id, name FROM %i', $table ), $cache_key, 'wawp_database', 300 );
			}

			$option_names = array(
				'selectedInstance'                => WAWP_Settings_Registry::SIGNUP_SELECTED_INSTANCE,
				'enable_otp'                      => WAWP_Settings_Registry::SIGNUP_ENABLE_OTP,
				'otp_method'                      => WAWP_Settings_Registry::SIGNUP_OTP_METHOD,
				'otp_message_whatsapp'            => WAWP_Settings_Registry::SIGNUP_OTP_MSG,
				'otp_message_email'               => WAWP_Settings_Registry::SIGNUP_OTP_MSG_EMAIL,
				'otp_subject_email'               => WAWP_Settings_Registry::SIGNUP_OTP_SUBJECT_EMAIL,
				'otp_email_template_id'           => WAWP_Settings_Registry::SIGNUP_OTP_EMAIL_TEMPLATE_ID,
				'enable_verification_fallback'    => WAWP_Settings_Registry::SIGNUP_FALLBACK,
				'field_order'                     => WAWP_Settings_Registry::SIGNUP_FIELD_ORDER,
				'signup_redirect_url'             => WAWP_Settings_Registry::SIGNUP_REDIRECT_URL,
				'signup_logo'                     => WAWP_Settings_Registry::SIGNUP_LOGO,
				'signup_title'                    => WAWP_Settings_Registry::SIGNUP_TITLE,
				'signup_description'              => WAWP_Settings_Registry::SIGNUP_DESCRIPTION,
				'color_theme'                     => WAWP_Settings_Registry::SIGNUP_COLOR_THEME,
				'enable_strong_password'          => WAWP_Settings_Registry::SIGNUP_STRONG_PASSWORD,
				'enable_password_reset'           => WAWP_Settings_Registry::SIGNUP_PASSWORD_RESET,
				'auto_login'                      => WAWP_Settings_Registry::SIGNUP_AUTO_LOGIN,
				'custom_css'                      => WAWP_Settings_Registry::SIGNUP_CUSTOM_CSS,
				'custom_fields'                   => WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS,
				'custom_shortcode'                => WAWP_Settings_Registry::SIGNUP_CUSTOM_SHORTCODE,
				'enable_premium_design'           => WAWP_Settings_Registry::SIGNUP_PREMIUM_DESIGN,
				'first_name_enabled'              => WAWP_Settings_Registry::SIGNUP_FIRST_NAME_ENABLED,
				'first_name_required'             => WAWP_Settings_Registry::SIGNUP_FIRST_NAME_REQUIRED,
				'last_name_enabled'               => WAWP_Settings_Registry::SIGNUP_LAST_NAME_ENABLED,
				'last_name_required'              => WAWP_Settings_Registry::SIGNUP_LAST_NAME_REQUIRED,
				'email_enabled'                   => WAWP_Settings_Registry::SIGNUP_EMAIL_ENABLED,
				'email_required'                  => WAWP_Settings_Registry::SIGNUP_EMAIL_REQUIRED,
				'phone_enabled'                   => WAWP_Settings_Registry::SIGNUP_PHONE_ENABLED,
				'phone_required'                  => WAWP_Settings_Registry::SIGNUP_PHONE_REQUIRED,
				'password_enabled'                => WAWP_Settings_Registry::SIGNUP_PASSWORD_ENABLED,
				'password_required'               => WAWP_Settings_Registry::SIGNUP_PASSWORD_REQUIRED,
				'button_bg_color'                 => WAWP_Settings_Registry::SIGNUP_BTN_BG_COLOR,
				'button_text_color'               => WAWP_Settings_Registry::SIGNUP_BTN_TEXT_COLOR,
				'button_hover_bg_color'           => WAWP_Settings_Registry::SIGNUP_BTN_HOVER_BG_COLOR,
				'button_hover_text_color'         => WAWP_Settings_Registry::SIGNUP_BTN_HOVER_TEXT_COLOR,
				'whatsapp_button_color'           => WAWP_Settings_Registry::SIGNUP_WA_BTN_COLOR,
				'whatsapp_button_text_color'      => WAWP_Settings_Registry::SIGNUP_WA_BTN_TEXT_COLOR,
				'firebase_button_color'           => WAWP_Settings_Registry::SIGNUP_FB_BTN_COLOR,
				'firebase_button_text_color'      => WAWP_Settings_Registry::SIGNUP_FB_BTN_TEXT_COLOR,
				'email_otp_button_color'          => WAWP_Settings_Registry::SIGNUP_EMAIL_BTN_COLOR,
				'email_otp_button_text_color'     => WAWP_Settings_Registry::SIGNUP_EMAIL_BTN_TEXT_COLOR,
				'verify_button_color'             => WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_COLOR,
				'verify_button_text_color'        => WAWP_Settings_Registry::SIGNUP_VERIFY_BTN_TEXT_COLOR,
				'resend_button_color'             => WAWP_Settings_Registry::SIGNUP_RESEND_BTN_COLOR,
				'resend_button_text_color'        => WAWP_Settings_Registry::SIGNUP_RESEND_BTN_TEXT_COLOR,
				'show_password_button_color'      => WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_COLOR,
				'show_password_button_text_color' => WAWP_Settings_Registry::SIGNUP_SHOW_PASS_BTN_TEXT_COLOR,
				'back_button_color'               => WAWP_Settings_Registry::SIGNUP_BACK_BTN_COLOR,
				'back_button_text_color'          => WAWP_Settings_Registry::SIGNUP_BACK_BTN_TEXT_COLOR,
				'whatsappAuthType'                => WAWP_Settings_Registry::SIGNUP_WA_AUTH_TYPE,
				'whatsappOtpLangType'             => WAWP_Settings_Registry::SIGNUP_WA_OTP_LANG_TYPE,
				'whatsappOtpCustomLang'           => WAWP_Settings_Registry::SIGNUP_WA_OTP_CUSTOM_LANG,
				'whatsappOtpFooterEnabled'        => WAWP_Settings_Registry::SIGNUP_WA_OTP_FOOT_EN,
				'whatsappOtpFooter'               => WAWP_Settings_Registry::SIGNUP_WA_OTP_FOOT,
				'whatsappListTitle'               => WAWP_Settings_Registry::SIGNUP_WA_LIST_TITLE,
				'whatsappListDesc'                => WAWP_Settings_Registry::SIGNUP_WA_LIST_DESC,
				'whatsappListFooter'              => WAWP_Settings_Registry::SIGNUP_WA_LIST_FOOTER,
				'whatsappListButton'              => WAWP_Settings_Registry::SIGNUP_WA_LIST_BUTTON,
				'whatsappListItem1'               => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM1,
				'whatsappListItem1Desc'           => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM1_DESC,
				'whatsappListItem2'               => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM2,
				'whatsappListItem2Desc'           => WAWP_Settings_Registry::SIGNUP_WA_LIST_ITEM2_DESC,
				'initialCooldown'                 => WAWP_Settings_Registry::AUTH_INITIAL_COOLDOWN,
				'maxCooldown'                     => WAWP_Settings_Registry::AUTH_MAX_COOLDOWN,
				'pollingInterval'                 => WAWP_Settings_Registry::AUTH_POLLING_INTERVAL,
			);

			$raw_settings   = $this->get_settings();
			$mapped_options = array();
			$defaults       = WAWP_Settings_Registry::get_defaults();

			foreach ( $option_names as $pretty => $db ) {
				$val = isset( $raw_settings[ $db ] ) ? $raw_settings[ $db ] : null;

				if ( ( null === $val || '' === $val ) && isset( $defaults[ $db ] ) ) {
					$val = $defaults[ $db ];
				}

				// Ensure complex fields are decoded.
				if ( in_array( $pretty, array( 'field_order', 'custom_fields', 'custom_shortcode' ), true ) ) {
					$val = $this->from_db( $val );
				}

				$mapped_options[ $pretty ] = $val;
				$mapped_options[ $db ]     = $val;
			}

			foreach ( $raw_settings as $k => $v ) {
				if ( ! isset( $mapped_options[ $k ] ) ) {
					$mapped_options[ $k ] = $v;
				}
			}

			$result = array(
				'options'        => $mapped_options,
				'optionNames'    => $option_names,
				'instances'      => $instances,
				'emailTemplates' => $templates,
				'i18n'           => array(
					'registration_form_title'          => esc_html__( 'Registration Form Builder', 'automation-web-platform' ),
					'registration_form_desc'           => esc_html__( 'Configure your custom registration form with OTP verification and advanced field management.', 'automation-web-platform' ),
					'general'                          => esc_html__( 'General', 'automation-web-platform' ),
					'form_fields'                      => esc_html__( 'Form Fields', 'automation-web-platform' ),
					'advanced'                         => esc_html__( 'Advanced', 'automation-web-platform' ),
					'style'                            => esc_html__( 'Style', 'automation-web-platform' ),
					'shortcodes'                       => esc_html__( 'Shortcodes', 'automation-web-platform' ),
					'save_settings_btn'                => esc_html__( 'Save Settings', 'automation-web-platform' ),
					'new_field_btn'                    => esc_html__( 'Add New Field', 'automation-web-platform' ),
					'allChangesSaved'                  => esc_html__( 'All Changes Saved', 'automation-web-platform' ),
					'unsavedChanges'                   => esc_html__( 'Unsaved Changes', 'automation-web-platform' ),
					'enable_signup_verification_label' => esc_html__( 'Enable Signup Verification', 'automation-web-platform' ),
					'enable_signup_verification_desc'  => __( 'Verify new users via OTP (One-Time Password) before account creation.', 'automation-web-platform' ),
					'method_label'                     => esc_html__( 'Verification Method', 'automation-web-platform' ),
					'chooseWaSender'                   => esc_html__( 'Choose WhatsApp Sender', 'automation-web-platform' ),
					'chooseWaSenderDesc'               => esc_html__( 'Select the WhatsApp number that will be used to send messages for this feature.', 'automation-web-platform' ),
					'preferredChannel'                 => esc_html__( 'Preferred Verification Channel', 'automation-web-platform' ),
					'msg_config_label'                 => esc_html__( 'Message Configuration', 'automation-web-platform' ),
					'force_lang_label'                 => esc_html__( 'Force Language', 'automation-web-platform' ),
					'enable_custom_footer_label'       => esc_html__( 'Enable Custom Footer', 'automation-web-platform' ),
					'list_ui_label'                    => esc_html__( 'List Interface Design', 'automation-web-platform' ),
					'preferred_channel_label'          => esc_html__( 'Preferred Verification Channel', 'automation-web-platform' ),
					'standard_otp_label'               => esc_html__( 'Standard OTP', 'automation-web-platform' ),
					'standard_otp_desc'                => esc_html__( '6-digit code via Message', 'automation-web-platform' ),
					'interactive_list_label'           => esc_html__( 'Interactive List', 'automation-web-platform' ),
					'interactive_list_desc'            => esc_html__( 'One-tap Verify/Reject', 'automation-web-platform' ),
					'message_config_label'             => esc_html__( 'Message Configuration', 'automation-web-platform' ),
					'force_language_label'             => esc_html__( 'Force Language', 'automation-web-platform' ),
					'list_interface_design_label'      => esc_html__( 'List Interface Design', 'automation-web-platform' ),
					'list_title_field_label'           => esc_html__( 'List Title', 'automation-web-platform' ),
					'main_description_field_label'     => esc_html__( 'Main Description', 'automation-web-platform' ),
					'item1_success_action_label'       => esc_html__( 'Item 1 (Success Action)', 'automation-web-platform' ),
					'action_title_field_label'         => esc_html__( 'Action Title', 'automation-web-platform' ),
					'action_description_field_label'   => esc_html__( 'Action Description', 'automation-web-platform' ),
					'item2_reject_action_label'        => esc_html__( 'Item 2 (Reject Action)', 'automation-web-platform' ),
					'selection_button_field_label'     => esc_html__( 'Selection Button', 'automation-web-platform' ),
					'bottom_footer_field_label'        => esc_html__( 'Bottom Footer', 'automation-web-platform' ),
					'select_language_label'            => esc_html__( 'Select Language', 'automation-web-platform' ),
					'footer_content_label'             => esc_html__( 'Footer Content', 'automation-web-platform' ),
					'selectInstance'                   => esc_html__( '— Select Instance —', 'automation-web-platform' ),
					'field_settings_title'             => esc_html__( 'Field Settings', 'automation-web-platform' ),
					'field_settings_desc'              => esc_html__( 'Customize how your registration fields are displayed and validated.', 'automation-web-platform' ),
					'advanced_settings_title'          => esc_html__( 'Advanced Protocols', 'automation-web-platform' ),
					'advanced_settings_desc'           => __( 'Control post-signup behavior, redirections, and security enhancements.', 'automation-web-platform' ),
					'design_aesthetics_title'          => __( 'Design & Aesthetics', 'automation-web-platform' ),
					'design_aesthetics_desc'           => __( 'Transform your registration page with custom themes, colors, and branding.', 'automation-web-platform' ),
					'shortcode_integration_title'      => esc_html__( 'Shortcode Integration', 'automation-web-platform' ),
					'shortcode_integration_desc'       => esc_html__( 'Copy and paste these shortcodes to display your custom registration form anywhere.', 'automation-web-platform' ),
					'settingsSaved'                    => esc_html__( 'Settings saved successfully!', 'automation-web-platform' ),
					'errorOccurred'                    => esc_html__( 'An error occurred while saving.', 'automation-web-platform' ),
					'email_label'                      => esc_html__( 'Email OTP', 'automation-web-platform' ),
					'firebase_label'                   => esc_html__( 'Firebase SMS', 'automation-web-platform' ),
					'whatsapp_label'                   => esc_html__( 'WhatsApp Web', 'automation-web-platform' ),
					'field_settings_label'             => esc_html__( 'Registration Fields', 'automation-web-platform' ),
					'enabled_fields_label'             => esc_html__( 'Enabled', 'automation-web-platform' ),
					'required_fields_label'            => esc_html__( 'Required', 'automation-web-platform' ),
					'custom_shortcodes_label'          => esc_html__( 'Additional Shortcodes', 'automation-web-platform' ),
					'custom_shortcodes_desc'           => esc_html__( 'Add custom shortcodes to be displayed on the signup page.', 'automation-web-platform' ),
					'placeholder_shortcode'            => esc_html__( '[your_shortcode]', 'automation-web-platform' ),
					'add_shortcode_btn'                => esc_html__( 'Add Shortcode', 'automation-web-platform' ),
					'redirect_url_label'               => esc_html__( 'Success Redirect URL', 'automation-web-platform' ),
					'redirect_url_desc'                => esc_html__( 'Where should users go after successful registration?', 'automation-web-platform' ),
					'strong_pass_label'                => esc_html__( 'Enforce Strong Passwords', 'automation-web-platform' ),
					'pass_reset_label'                 => esc_html__( 'Show Password Reset Link', 'automation-web-platform' ),
					'auto_login_label'                 => esc_html__( 'Auto-login After Signup', 'automation-web-platform' ),
					'style_settings_label'             => esc_html__( 'Visual Customization', 'automation-web-platform' ),
					'style_settings_desc'              => esc_html__( 'Customize the appearance of your registration form to match your brand.', 'automation-web-platform' ),
					'logo_label'                       => esc_html__( 'Form Logo', 'automation-web-platform' ),
					'title_label'                      => esc_html__( 'Form Title', 'automation-web-platform' ),
					'description_label'                => esc_html__( 'Form Description', 'automation-web-platform' ),
					'custom_css_label'                 => esc_html__( 'Custom CSS', 'automation-web-platform' ),
					'use_this_logo'                    => esc_html__( 'Use this logo', 'automation-web-platform' ),
					'signup_active'                    => esc_html__( 'Registration portal is active', 'automation-web-platform' ),
					'signup_inactive'                  => esc_html__( 'Registration portal is disabled', 'automation-web-platform' ),
					/* translators: %s: verification method name */
					'signup_method'                    => __( 'Users verify via %s', 'automation-web-platform' ),
					/* translators: 1: total fields, 2: enabled fields, 3: required fields */
					'signup_fields_active'             => __( 'Form has %1$d fields total (%2$d enabled, %3$d required)', 'automation-web-platform' ),
					/* translators: %s: field name */
					'signup_fields_error'              => __( 'Error: %s is required but not enabled!', 'automation-web-platform' ),
					'signup_strong_pw_a'               => esc_html__( 'Strong password enforcement is active', 'automation-web-platform' ),
					'signup_strong_pw_i'               => esc_html__( 'Strong password enforcement is inactive', 'automation-web-platform' ),
					'signup_pw_reset_a'                => esc_html__( 'Password reset link is visible', 'automation-web-platform' ),
					'signup_pw_reset_i'                => esc_html__( 'Password reset link is hidden', 'automation-web-platform' ),
					'signup_fallback_a'                => esc_html__( 'Verification fallback is enabled', 'automation-web-platform' ),
					'signup_fallback_i'                => esc_html__( 'Verification fallback is disabled', 'automation-web-platform' ),
					'signup_autologin_a'               => esc_html__( 'Users are logged in automatically after signup', 'automation-web-platform' ),
					'signup_autologin_i'               => esc_html__( 'Users must log in manually after signup', 'automation-web-platform' ),
					'firebase_tpl_label'               => esc_html__( 'Manage Firebase SMS Templates', 'automation-web-platform' ),
					'whatsapp_desc'                    => esc_html__( 'Ultra-fast delivery via Wawp engine', 'automation-web-platform' ),
					'email_desc'                       => esc_html__( 'Secure one-click login link', 'automation-web-platform' ),
					'firebase_desc'                    => esc_html__( 'Reliable Google infrastructure', 'automation-web-platform' ),
					'main_shortcode_title'             => esc_html__( 'Main Shortcode', 'automation-web-platform' ),
					'main_shortcode_desc'              => esc_html__( 'Display the complete Registration form on any page, post or widget area.', 'automation-web-platform' ),
					'php_integration_title'            => esc_html__( 'PHP Integration', 'automation-web-platform' ),
					'php_integration_desc'             => esc_html__( 'Call the form programmatically directly from your theme template files.', 'automation-web-platform' ),
					'whatsapp_engine_label'            => esc_html__( 'WhatsApp Auth Engine', 'automation-web-platform' ),
					'customer_email_label'             => esc_html__( 'Customer Email', 'automation-web-platform' ),
					'security_hardening_desc'          => esc_html__( 'Security Hardening', 'automation-web-platform' ),
					'user_convenience_desc'            => esc_html__( 'User Convenience', 'automation-web-platform' ),
					'direct_access_desc'               => esc_html__( 'Direct Access', 'automation-web-platform' ),
					'apply_changes'                    => esc_html__( 'Apply Changes', 'automation-web-platform' ),
					'autologin_label'                  => esc_html__( 'Auto Login After Signup', 'automation-web-platform' ),
					'back'                             => esc_html__( 'Back', 'automation-web-platform' ),
					'close'                            => esc_html__( 'Close', 'automation-web-platform' ),
					'edit_field_title'                 => esc_html__( 'Edit Field', 'automation-web-platform' ),
					'fallback_desc'                    => esc_html__( 'ALLOW EMAIL VERIFICATION IF WHATSAPP FAILS', 'automation-web-platform' ),
					'fallback_label'                   => esc_html__( 'Verification Fallback', 'automation-web-platform' ),
					'save_changes'                     => esc_html__( 'Save Changes', 'automation-web-platform' ),
					'select_logo'                      => esc_html__( 'Select Logo', 'automation-web-platform' ),
					'type_checkbox'                    => esc_html__( 'Checkbox Group', 'automation-web-platform' ),
					'type_email'                       => esc_html__( 'Email Input', 'automation-web-platform' ),
					'type_number'                      => esc_html__( 'Number Input', 'automation-web-platform' ),
					'type_radio'                       => esc_html__( 'Radio Selection', 'automation-web-platform' ),
					'type_text'                        => esc_html__( 'Text Input', 'automation-web-platform' ),
					'type_textarea'                    => esc_html__( 'Text Area', 'automation-web-platform' ),
					'addExtension'                     => esc_html__( 'Add Extension', 'automation-web-platform' ),
					'noExtensions'                     => esc_html__( 'No extensions defined', 'automation-web-platform' ),
					'useCustomShortcodes'              => esc_html__( 'Use custom shortcodes to inject content before or after the form.', 'automation-web-platform' ),
					'customStylesActive'               => esc_html__( 'Custom Styles Active', 'automation-web-platform' ),
					'standardDesignActive'             => esc_html__( 'Standard design is active', 'automation-web-platform' ),
					'customStylesApplied'              => esc_html__( 'Your custom styles are being applied with high priority.', 'automation-web-platform' ),
					'customStylesInjected'             => esc_html__( 'Your custom styles will be injected into the footer and applied with high priority to override default layouts.', 'automation-web-platform' ),
					'field_settings_desc_standard'     => esc_html__( 'Standard and custom fields for your registration form. Drag rows to reorder.', 'automation-web-platform' ),
					'first_name_field'                 => esc_html__( 'First Name', 'automation-web-platform' ),
					'last_name_field'                  => esc_html__( 'Last Name', 'automation-web-platform' ),
					'email_field'                      => esc_html__( 'Email Address', 'automation-web-platform' ),
					'phone_field'                      => esc_html__( 'Phone Number', 'automation-web-platform' ),
					'password_field'                   => esc_html__( 'Password', 'automation-web-platform' ),
					'standard_badge'                   => esc_html__( 'Standard', 'automation-web-platform' ),
					'custom_badge'                     => esc_html__( 'Custom', 'automation-web-platform' ),
					'new_custom_field_label'           => esc_html__( 'New Custom Field', 'automation-web-platform' ),
					'configure_field_desc'             => esc_html__( 'Configure your field settings and options', 'automation-web-platform' ),
					'field_label_title'                => esc_html__( 'Field Label', 'automation-web-platform' ),
					'field_label_placeholder'          => esc_html__( 'e.g., Your Name', 'automation-web-platform' ),
					'field_label_desc'                 => esc_html__( 'This will be the Name for the user frontend. Allowed every Thing.', 'automation-web-platform' ),
					'meta_key_title'                   => esc_html__( 'Meta Key (Unique ID)', 'automation-web-platform' ),
					'meta_key_placeholder'             => esc_html__( 'e.g., my_custom_field', 'automation-web-platform' ),
					'meta_key_desc'                    => esc_html__( 'This will be the unique meta key for the user profile. Use lowercase, no spaces, no special characters except underscore.', 'automation-web-platform' ),
					'field_component_type'             => esc_html__( 'Field Component Type', 'automation-web-platform' ),
					'select_component_placeholder'     => esc_html__( 'Select Component Type...', 'automation-web-platform' ),
					'choose_field_appearance'          => esc_html__( 'Choose how the field will appear to your users.', 'automation-web-platform' ),
					'selection_options'                => esc_html__( 'Selection Options', 'automation-web-platform' ),
					'interactive_mode'                 => esc_html__( 'Interactive', 'automation-web-platform' ),
					'bulk_mode'                        => esc_html__( 'Bulk', 'automation-web-platform' ),
					'option_label_placeholder'         => esc_html__( 'Option Label', 'automation-web-platform' ),
					'option_value_placeholder'         => esc_html__( 'Option Value', 'automation-web-platform' ),
					'add_new_option'                   => esc_html__( 'Add New Option', 'automation-web-platform' ),
					'bulk_options_placeholder'         => esc_html__( "Option 1|value1\nOption 2|value2", 'automation-web-platform' ),
					'provide_labels_values_desc'       => esc_html__( 'Provide labels and internal values for the selection options.', 'automation-web-platform' ),
					'enable_field_title'               => esc_html__( 'Enable Field', 'automation-web-platform' ),
					'enable_field_desc'                => esc_html__( 'Show this field on your signup form.', 'automation-web-platform' ),
					'required_field_title'             => esc_html__( 'Required Field', 'automation-web-platform' ),
					'required_field_desc'              => esc_html__( 'Users must fill this field to register.', 'automation-web-platform' ),
					'logo_recommendations'             => esc_html__( 'Better to use a transparent PNG or SVG logo. Recommended size: 400x120px.', 'automation-web-platform' ),
					'change_logo'                      => esc_html__( 'Change Logo', 'automation-web-platform' ),
					'color_presets_label'              => esc_html__( 'Color Presets', 'automation-web-platform' ),
					'preset_default'                   => esc_html__( 'Default', 'automation-web-platform' ),
					'preset_teal'                      => esc_html__( 'Teal', 'automation-web-platform' ),
					'preset_ocean'                     => esc_html__( 'Ocean', 'automation-web-platform' ),
					'preset_midnight'                  => esc_html__( 'Midnight', 'automation-web-platform' ),
					'preset_sunset'                    => esc_html__( 'Sunset', 'automation-web-platform' ),
					'preset_minimal'                   => esc_html__( 'Minimal', 'automation-web-platform' ),
					'detailed_colors_label'            => esc_html__( 'Detailed Colors', 'automation-web-platform' ),
					'color_whatsapp_btn'               => esc_html__( 'WhatsApp Button', 'automation-web-platform' ),
					'color_email_btn'                  => esc_html__( 'Email Button', 'automation-web-platform' ),
					'color_sms_btn'                    => esc_html__( 'SMS Button', 'automation-web-platform' ),
					'color_signup_btn'                 => esc_html__( 'Signup Button', 'automation-web-platform' ),
					'color_verify_btn'                 => esc_html__( 'Verify Button', 'automation-web-platform' ),
					'group_methods'                    => esc_html__( 'Methods', 'automation-web-platform' ),
					'group_main_action'                => esc_html__( 'Main Action', 'automation-web-platform' ),
					'group_verification'               => esc_html__( 'Verification', 'automation-web-platform' ),
					'color_bg_label'                   => esc_html__( 'Bg', 'automation-web-platform' ),
					'color_text_label'                 => esc_html__( 'Text', 'automation-web-platform' ),
					'subject_label'                    => esc_html__( 'Subject', 'automation-web-platform' ),
					'subject_placeholder'              => esc_html__( 'Subject...', 'automation-web-platform' ),
					'body_template_label'              => esc_html__( 'Body Template', 'automation-web-platform' ),
					'manage_templates_label'           => esc_html__( 'Manage', 'automation-web-platform' ),
					'manage_templates_title'           => esc_html__( 'Manage Templates', 'automation-web-platform' ),
					'primary_badge'                    => esc_html__( 'Primary', 'automation-web-platform' ),
					'developer_badge'                  => esc_html__( 'Developer', 'automation-web-platform' ),
					/* translators: %s: Redirect URL */
					'users_redirected_success'         => esc_html__( 'Users will be redirected to %s after registration.', 'automation-web-platform' ),
					'no_redirect_url_set'              => esc_html__( 'No redirect URL set. Users will remain on the success page.', 'automation-web-platform' ),
					'logo_set_desc'                    => esc_html__( 'Your brand logo is set and will appear at the top of the registration form.', 'automation-web-platform' ),
					'no_logo_desc'                     => esc_html__( 'No logo selected. The form will display your site title instead.', 'automation-web-platform' ),
					'branding_active_desc'             => esc_html__( 'Custom form header (title and description) is active and visible to users.', 'automation-web-platform' ),
					'branding_inactive_desc'           => esc_html__( 'Using default header text. Consider adding a custom title to welcome users.', 'automation-web-platform' ),
					'css_active_desc'                  => esc_html__( 'Custom CSS is active. Your style overrides are being applied to the form.', 'automation-web-platform' ),
					'css_inactive_desc'                => esc_html__( 'No custom CSS applied. The form is using the standard premium theme.', 'automation-web-platform' ),
					'colors_active_desc'               => esc_html__( 'Visual theme overrides are active. Custom button and text colors are being applied.', 'automation-web-platform' ),
					'premium_design_active_desc'       => esc_html__( 'Premium custom design is active and applied to your registration form.', 'automation-web-platform' ),
					'premium_design_inactive_desc'     => esc_html__( 'Standard design is active. Enable premium design to apply these custom styles.', 'automation-web-platform' ),
					/* translators: %d: number of shortcodes */
					'active_shortcodes_count_desc'     => esc_html__( '<b>%d</b> additional shortcodes are active.', 'automation-web-platform' ),
					'no_active_shortcodes_desc'        => esc_html__( 'No additional shortcodes are currently active.', 'automation-web-platform' ),
					'advanced_tab_desc'                => esc_html__( 'Fine-tune your registration flow and security settings.', 'automation-web-platform' ),
				),
				'presets'        => self::get_color_presets(),
				'availability'   => array(
					'whatsapp' => array(
						'available' => WAWP_Connector()->is_whatsapp_active() || WAWP_Connector()->is_meta_active(),
						'reason'    => ( WAWP_Connector()->is_whatsapp_active() || WAWP_Connector()->is_meta_active() ) ? '' : esc_html__( 'WhatsApp is not configured or active.', 'automation-web-platform' ),
					),
					'email'    => array(
						'available' => ( function () {
							$smtp = $this->get_wawp_setting( WAWP_Settings_Registry::SMTP_SETTINGS, array() );
							return ! empty( $smtp['enabled'] );
						} )(),
						'reason'    => ( function () {
							$smtp = $this->get_wawp_setting( WAWP_Settings_Registry::SMTP_SETTINGS, array() );
							return ! empty( $smtp['enabled'] ) ? '' : esc_html__( 'SMTP/Email is not configured or active.', 'automation-web-platform' );
						} )(),
					),
					'firebase' => array(
						'available' => WAWP_Connector()->is_firebase_active(),
						'reason'    => WAWP_Connector()->is_firebase_active() ? '' : esc_html__( 'Firebase is not configured or active.', 'automation-web-platform' ),
					),
				),
			);

			$cached_result = $result;
			return $result;
		}

		/**
		 * Add custom user profile fields.
		 *
		 * @param WP_User $user User object.
		 */
		public function add_custom_user_profile_fields( $user ) {
			$settings      = $this->get_settings();
			$custom_fields = $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ];
			if ( empty( $custom_fields ) ) {
				return;
			}
			?>
			<h2><?php esc_html_e( 'Additional Profile Information', 'automation-web-platform' ); ?></h2>
			<table class="form-table">
				<?php
				foreach ( $custom_fields as $field_key => $field ) {
					$user_meta_value = get_user_meta( $user->ID, $field_key, true );
					if ( ! empty( $field['enabled'] ) || ( ! empty( $user_meta_value ) && '' !== $user_meta_value ) ) {
						$field_id   = 'wawp_custom_field_' . esc_attr( $field_key );
						$field_name = esc_attr( $field_key );
						?>
						<tr>
							<th><label for="<?php echo esc_attr( $field_id ); ?>"><?php echo esc_html( $field['label'] ); ?></label></th>
							<td>
								<?php
								switch ( $field['type'] ) {
									case 'text':
									case 'email':
									case 'number':
										?>
										<input type="<?php echo esc_attr( $field['type'] ); ?>" name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $field_id ); ?>" value="<?php echo esc_attr( $user_meta_value ); ?>" class="regular-text" />
										<?php
										break;
									case 'textarea':
										?>
										<textarea name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $field_id ); ?>" rows="5" cols="30" class="large-text"><?php echo esc_textarea( $user_meta_value ); ?></textarea>
										<?php
										break;
									case 'checkbox':
										$current_values = is_array( $user_meta_value ) ? $user_meta_value : array();
										if ( ! empty( $field['options'] ) && is_array( $field['options'] ) ) {
											foreach ( $field['options'] as $option ) {
												?>
												<label><input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>[]" value="<?php echo esc_attr( $option['value'] ); ?>" 
												<?php
												checked( in_array( $option['value'], $current_values, true ), true );
												?>
												/> <?php echo esc_html( $option['label'] ?? '' ); ?></label><br>
												<?php
											}
										}
										break;
									case 'radio':
										$current_value = $user_meta_value;
										if ( ! empty( $field['options'] ) && is_array( $field['options'] ) ) {
											foreach ( $field['options'] as $option ) {
												?>
												<label><input type="radio" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $option['value'] ); ?>" 
												<?php
												checked( ( $option['value'] === $current_value ), true );
												?>
												/> <?php echo esc_html( $option['label'] ?? '' ); ?></label><br>
												<?php
											}
										}
										break;
								}
								?>
							</td>
						</tr>
						<?php
					}
				}
				?>
			</table>
			<?php
		}

		/**
		 * Save custom user profile fields.
		 *
		 * @param int $user_id User ID.
		 */
		public function save_custom_user_profile_fields( $user_id ) {
			if ( ! current_user_can( 'edit_user', $user_id ) ) {
				return;
			}

			if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'update-user_' . $user_id ) ) {
				return;
			}

			$settings      = $this->get_settings();
			$custom_fields = $settings[ WAWP_Settings_Registry::SIGNUP_CUSTOM_FIELDS ];

			foreach ( $custom_fields as $field_key => $field_config ) {
				if ( isset( $_POST[ $field_key ] ) ) {
					$sanitized_value = is_array( $_POST[ $field_key ] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST[ $field_key ] ) ) : sanitize_text_field( wp_unslash( $_POST[ $field_key ] ) );
					update_user_meta( $user_id, $field_key, $sanitized_value );
				} elseif ( 'checkbox' === $field_config['type'] ) {
					delete_user_meta( $user_id, $field_key );
				}
			}
		}
		/**
		 * Update settings.
		 *
		 * @param array $data Settings data.
		 */
		public function update_settings( $data ) {
			foreach ( $data as $key => $value ) {
				$final_key = $key;
				if ( 'selected_instance' === $key ) {
					$final_key = WAWP_Settings_Registry::SIGNUP_SELECTED_INSTANCE;
				}
				$this->update_wawp_setting( $final_key, $value );
			}
		}
	}
}
