<?php
/**
 * Shared markup/helpers for module advanced settings in the PowerSuite modal.
 *
 * @package WP_PowerSuite
 */

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

if ( ! function_exists( 'wpps_render_modal_settings_tip' ) ) {
	/**
	 * Modal help control: hover/focus panel (matches Cloudflare Turnstile pattern; avoids flaky native title tooltips in WP admin).
	 *
	 * @param string $accessible_label Short summary for the trigger aria-label.
	 * @param string $tooltip_text     Help copy (plain text).
	 */
	function wpps_render_modal_settings_tip( string $accessible_label, string $tooltip_text ): void {
		echo '<span class="wpps-modal-tooltip">';
		echo '<button type="button" class="wpps-modal-tooltip__trigger" aria-label="' . esc_attr( $accessible_label ) . '">';
		echo '<span class="wpps-modal-tooltip__glyph" aria-hidden="true">?</span>';
		echo '</button>';
		echo '<span class="wpps-modal-tooltip__panel" role="tooltip">' . esc_html( $tooltip_text ) . '</span>';
		echo '</span>';
	}
}

if ( ! function_exists( 'wpps_enqueue_accordion_tooltip_script' ) ) {
	/**
	 * Viewport-aware accordion help tooltips (Code Snippets, Magic Login, Redirect Manager, etc.).
	 */
	function wpps_enqueue_accordion_tooltip_script(): void {
		static $done = false;
		if ( $done ) {
			return;
		}
		$plugin_url = defined( 'WP_POWERSUITE_PLUGIN_URL' ) ? WP_POWERSUITE_PLUGIN_URL : '';
		$version    = defined( 'WP_POWERSUITE_VERSION' ) ? WP_POWERSUITE_VERSION : '1.0.0';
		if ( $plugin_url === '' ) {
			return;
		}
		// Version on file mtime so browsers never serve a stale tooltip script.
		$script_file = function_exists( 'wp_powersuite_admin_asset_path' )
			? wp_powersuite_admin_asset_path( 'wpps-accordion-tooltips.js' )
			: __DIR__ . DIRECTORY_SEPARATOR . 'Assets' . DIRECTORY_SEPARATOR . 'wpps-accordion-tooltips.js';
		$script_url  = function_exists( 'wp_powersuite_admin_asset_url' )
			? wp_powersuite_admin_asset_url( 'wpps-accordion-tooltips.js' )
			: $plugin_url . 'src/Admin/Assets/wpps-accordion-tooltips.js';
		wp_enqueue_script(
			'wpps-accordion-tooltips',
			$script_url,
			[],
			is_readable( $script_file ) ? (string) filemtime( $script_file ) : $version,
			true
		);
		$done = true;
	}
}

if ( ! function_exists( 'wpps_render_premium_badge' ) ) {
	function wpps_render_premium_badge(): void {
		echo ' <span class="wpps-pro-badge">' . esc_html__( 'Premium', 'wp-powersuite' ) . '</span>';
	}
}

if ( ! function_exists( 'wpps_render_premium_upsell_notice' ) ) {
	/**
	 * Inline Premium notice: license page if Pro is installed, else purchase URL.
	 */
	function wpps_render_premium_upsell_notice(): void {
		$url = function_exists( 'wp_powersuite_premium_upsell_url' )
			? wp_powersuite_premium_upsell_url()
			: ( defined( 'WP_POWERSUITE_PRODUCT_URL' ) ? WP_POWERSUITE_PRODUCT_URL : 'https://wppowersuite.com/' );
		$can = function_exists( 'wp_powersuite_can_activate_license' ) && wp_powersuite_can_activate_license();
		?>
	<div class="notice notice-info inline" style="margin:0 0 12px; padding:8px 12px;">
		<p style="margin:0;">
			<?php
			if ( $can ) {
				echo wp_kses_post(
					sprintf(
						/* translators: %s: URL to license page */
						__( 'This is a Premium feature. <a href="%s">Activate your license</a> to unlock it.', 'wp-powersuite' ),
						esc_url( $url )
					)
				);
			} else {
				echo wp_kses(
					sprintf(
						/* translators: %s: URL to purchase WP PowerSuite Pro */
						__( 'This is included with WP PowerSuite Pro. <a href="%s" target="_blank" rel="noopener noreferrer">Get WP PowerSuite Pro</a> to unlock it.', 'wp-powersuite' ),
						esc_url( $url )
					),
					[
						'a' => [
							'href'   => true,
							'target' => true,
							'rel'    => true,
						],
					]
				);
			}
			?>
		</p>
	</div>
		<?php
	}
}

if ( ! function_exists( 'wpps_render_captcha_overlap_notices' ) ) {
	/**
	 * Warn when multiple enabled CAPTCHA modules protect the same surface.
	 *
	 * @param string $module_id Current CAPTCHA module (google-recaptcha, cloudflare-turnstile, simple-captcha).
	 */
	function wpps_render_captcha_overlap_notices( string $module_id ): void {
		if ( ! class_exists( \WP_PowerSuite\Captcha\Shared\CaptchaOverlapDetector::class ) ) {
			return;
		}

		$overlaps = \WP_PowerSuite\Captcha\Shared\CaptchaOverlapDetector::getOverlapsForModule( $module_id );
		if ( $overlaps === [] ) {
			return;
		}
		?>
		<div class="wpps-settings-alert wpps-settings-alert--warning wpps-captcha-overlap-notice" role="alert">
			<p>
				<strong><?php esc_html_e( 'Overlapping CAPTCHA protection', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'More than one CAPTCHA module is protecting the same area. Visitors may see duplicate challenges or fail verification. Turn off each surface on all but one module.', 'wp-powersuite' ); ?>
			</p>
			<ul class="wpps-captcha-overlap-notice__list">
				<?php foreach ( $overlaps as $overlap ) : ?>
					<li>
						<strong><?php echo esc_html( $overlap['label'] ); ?>:</strong>
						<?php echo esc_html( implode( ', ', array_values( $overlap['modules'] ) ) ); ?>
					</li>
				<?php endforeach; ?>
			</ul>
			<p class="description">
				<?php esc_html_e( 'All three CAPTCHA modules can stay enabled—assign each surface (login, checkout, forms, etc.) to a single provider. Basic Forms picks a provider per form in the form editor.', 'wp-powersuite' ); ?>
			</p>
		</div>
		<?php
	}
}

if ( ! function_exists( 'wpps_render_content_protection_notice' ) ) {
	/**
	 * Shared admin warning for frontend content-restriction modules.
	 */
	function wpps_render_content_protection_notice(): void {
		?>
		<div class="wpps-module-notice--warning" role="alert">
			<p>
				<strong><?php esc_html_e( 'Not a security lock', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'These restrictions only deter casual misuse in the browser. They do not stop determined visitors, server-side copying, or screenshots.', 'wp-powersuite' ); ?>
			</p>
			<p>
				<strong><?php esc_html_e( 'Accessibility and UX', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'Blocking selection, context menus, or keyboard shortcuts can frustrate keyboard users, screen-reader users, and anyone who needs copy/paste or in-page search. Enable only where the trade-off is acceptable.', 'wp-powersuite' ); ?>
			</p>
			<p>
				<strong><?php esc_html_e( 'Page caching', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'Full-page cache plugins may serve HTML built for one visitor to everyone. Role-based rules may not apply reliably until the cache is cleared or varies by login cookie.', 'wp-powersuite' ); ?>
			</p>
		</div>
		<?php
	}
}
