<?php
/**
 * Shortcode Manager.
 *
 * @package automation-web-platform
 */

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

/**
 * WAWP_Shortcode_Manager Class
 * Handles registration and rendering of plugin shortcodes.
 */
class WAWP_Shortcode_Manager {
	use WAWP_Settings_Handler;

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_shortcode( 'wawp-fast-login', array( $this, 'render_fast_login_shortcode' ) );
	}

	/**
	 * Render the [wawp-fast-login] shortcode.
	 */
	public function render_fast_login_shortcode() {
		static $fl_counter = 0;
		++$fl_counter;
		$fl_uid = $fl_counter;
		ob_start();
		?>
		<div id="wawp-fast-login-wrapper-<?php echo esc_attr( $fl_uid ); ?>" class="wawp-fast-login-container wawp-fast-login-wrapper">
			<?php
			global $wawp_recaptcha_rendered;
			$recaptcha_login_enabled  = $this->get_wawp_setting( WAWP_Settings_Registry::GOOGLE_RECAPTCHA_ENABLE_WAWP_LOGIN );
			$recaptcha_signup_enabled = $this->get_wawp_setting( WAWP_Settings_Registry::GOOGLE_RECAPTCHA_ENABLE_WAWP_SIGNUP );

			if ( $recaptcha_login_enabled && empty( $wawp_recaptcha_rendered['login'] ) ) {
				echo '<div class="wawp-recaptcha-container" id="wawp-recaptcha-login"></div>';
				$wawp_recaptcha_rendered['login'] = true;
			}

			if ( $recaptcha_signup_enabled && empty( $wawp_recaptcha_rendered['signup'] ) ) {
				echo '<div class="wawp-recaptcha-container" id="wawp-recaptcha-signup"></div>';
				$wawp_recaptcha_rendered['signup'] = true;
			}
			?>
			<div class="wawp-fast-login-view wawp-fast-login-view-login">
				<?php echo do_shortcode( '[wawp_otp_login hide_signup_link="true"]' ); ?>
				<p class="wawp-switch-form" style="margin-top: 20px; text-align: center; font-size: 15px; color: #434960;">
					<?php echo esc_html__( 'You don\'t have an account?', 'automation-web-platform' ); ?>
					<a href="#" class="wawp-fast-login-signup-toggle" style="color: #2563eb !important; font-weight: 600; text-decoration: underline; margin-left: 5px;">
						<?php echo esc_html__( 'Signup', 'automation-web-platform' ); ?>
					</a>
				</p>
			</div>
			<div class="wawp-fast-login-view wawp-fast-login-view-signup" style="display: none;">
				<?php echo do_shortcode( '[wawp_signup_form hide_login_link="true"]' ); ?>
				<p class="wawp-switch-form" style="margin-top: 20px; text-align: center; font-size: 15px; color: #434960;">
					<?php echo esc_html__( 'Already have an account?', 'automation-web-platform' ); ?>
					<a href="#" class="wawp-fast-login-login-toggle" style="color: #2563eb !important; font-weight: 600; text-decoration: underline; margin-left: 5px;">
						<?php echo esc_html__( 'Login', 'automation-web-platform' ); ?>
					</a>
				</p>
			</div>
		</div>
		<style>
			.wawp-fast-login-container { width: 100%; max-width: 450px; margin: 0 auto; padding: 10px; }
			.wawp-fast-login-view { width: 100%; }
		</style>
		<?php
		return ob_get_clean();
	}
	/**
	 * Create Fast Login page on activation.
	 */
	public static function on_activate() {
		$page_title = esc_html__( 'Fast Login', 'automation-web-platform' );
		$query      = new \WP_Query(
			array(
				'post_type'              => 'page',
				'title'                  => $page_title,
				'post_status'            => 'all',
				'posts_per_page'         => 1,
				'no_found_rows'          => true,
				'ignore_sticky_posts'    => true,
				'update_post_term_cache' => false,
				'update_post_meta_cache' => false,
			)
		);

		$page_check   = ! empty( $query->posts ) ? $query->posts[0] : null;
		$page_content = '[wawp-fast-login]';

		if ( ! $page_check ) {
			wp_insert_post(
				array(
					'post_title'     => $page_title,
					'post_type'      => 'page',
					'post_content'   => $page_content,
					'post_status'    => 'publish',
					'comment_status' => 'closed',
				)
			);
		}
	}
}
