<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'simple-captcha';
}

$settings     = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}

$captcha_type         = in_array( $settings['captcha_type'] ?? 'math', [ 'math', 'text', 'image' ], true ) ? ( $settings['captcha_type'] ?? 'math' ) : 'math';
$math_difficulty      = in_array( $settings['math_difficulty'] ?? 'easy', [ 'easy', 'medium', 'hard' ], true ) ? ( $settings['math_difficulty'] ?? 'easy' ) : 'easy';
$text_questions       = is_array( $settings['text_questions'] ?? null ) ? $settings['text_questions'] : [];
$image_style          = in_array( $settings['image_style'] ?? 'distorted', [ 'distorted', 'simple' ], true ) ? ( $settings['image_style'] ?? 'distorted' ) : 'distorted';
$image_length         = max( 4, min( 8, (int) ( $settings['image_length'] ?? 5 ) ) );
$image_case_sensitive = ! empty( $settings['image_case_sensitive'] );
$skip_logged_in       = ! isset( $settings['skip_logged_in'] ) || ! empty( $settings['skip_logged_in'] );
$error_message        = trim( (string) ( $settings['error_message'] ?? '' ) );

$protect_login          = ! empty( $settings['protect_login'] );
$protect_registration   = ! empty( $settings['protect_registration'] );
$protect_password_reset = ! empty( $settings['protect_password_reset'] );
$protect_comments       = ! empty( $settings['protect_comments'] );

$woo_login          = ! empty( $settings['woo_login'] );
$woo_registration   = ! empty( $settings['woo_registration'] );
$woo_lost_password  = ! empty( $settings['woo_lost_password'] );
$woo_checkout       = ! empty( $settings['woo_checkout'] );
$woo_reviews        = ! empty( $settings['woo_reviews'] );

$login_canonical_only    = ! empty( $settings['login_canonical_only'] );
$register_canonical_only = ! empty( $settings['register_canonical_only'] );
$woo_checkout_guest_only = ! empty( $settings['woo_checkout_guest_only'] );

$anti_bot_honeypot  = ! isset( $settings['anti_bot_honeypot'] ) || ! empty( $settings['anti_bot_honeypot'] );
$anti_bot_timing    = ! isset( $settings['anti_bot_timing'] ) || ! empty( $settings['anti_bot_timing'] );
$min_submit_seconds = max( 1, min( 30, (int) ( $settings['min_submit_seconds'] ?? 2 ) ) );
$ajax_comment_reset = ! isset( $settings['ajax_comment_reset'] ) || ! empty( $settings['ajax_comment_reset'] );

$skip_rules        = isset( $settings['skip_rules'] ) && is_array( $settings['skip_rules'] ) ? $settings['skip_rules'] : [];
$skip_enabled      = ! empty( $skip_rules['enabled'] );
$skip_roles_saved  = isset( $skip_rules['roles'] ) && is_array( $skip_rules['roles'] ) ? $skip_rules['roles'] : [];
$skip_ips_saved    = isset( $skip_rules['ips'] ) && is_array( $skip_rules['ips'] ) ? $skip_rules['ips'] : [];
$skip_ips_textarea = implode( "\n", array_map( 'strval', $skip_ips_saved ) );
$skip_uas_saved    = isset( $skip_rules['user_agents'] ) && is_array( $skip_rules['user_agents'] ) ? $skip_rules['user_agents'] : [];
$skip_uas_textarea = implode( "\n", array_map( 'strval', $skip_uas_saved ) );
$all_roles         = function_exists( 'wp_roles' ) ? wp_roles()->roles : [];

$woo_active           = class_exists( 'WooCommerce', false );
$elementor_pro_active = defined( 'ELEMENTOR_PRO_VERSION' );

$wpps_sc_integrations = [
	[ 'key' => 'basic_forms', 'label' => __( 'Basic Forms (WP PowerSuite)', 'wppowersuite-lite' ), 'available' => true ],
	[ 'key' => 'cf7', 'label' => __( 'Contact Form 7', 'wppowersuite-lite' ), 'available' => class_exists( 'WPCF7' ) ],
	[ 'key' => 'wpforms', 'label' => __( 'WPForms', 'wppowersuite-lite' ), 'available' => function_exists( 'wpforms' ) ],
	[ 'key' => 'formidable', 'label' => __( 'Formidable Forms', 'wppowersuite-lite' ), 'available' => class_exists( 'FrmFormsController' ) ],
	[ 'key' => 'fluent_forms', 'label' => __( 'Fluent Forms', 'wppowersuite-lite' ), 'available' => defined( 'FLUENTFORM' ) ],
	[ 'key' => 'elementor_forms', 'label' => __( 'Elementor Pro Forms', 'wppowersuite-lite' ), 'available' => $elementor_pro_active ],
	[ 'key' => 'integration_gravity', 'label' => __( 'Gravity Forms', 'wppowersuite-lite' ), 'available' => class_exists( 'GFForms' ) ],
	[ 'key' => 'integration_jetpack', 'label' => __( 'Jetpack forms', 'wppowersuite-lite' ), 'available' => defined( 'JETPACK__VERSION' ) ],
	[ 'key' => 'integration_bbpress', 'label' => __( 'bbPress', 'wppowersuite-lite' ), 'available' => function_exists( 'bbpress' ) ],
	[ 'key' => 'integration_buddypress', 'label' => __( 'BuddyPress', 'wppowersuite-lite' ), 'available' => class_exists( 'BuddyPress' ) ],
	[ 'key' => 'integration_ultimate_member', 'label' => __( 'Ultimate Member', 'wppowersuite-lite' ), 'available' => class_exists( 'UM' ) ],
	[ 'key' => 'integration_memberpress', 'label' => __( 'MemberPress', 'wppowersuite-lite' ), 'available' => class_exists( 'MeprAppCtrl' ) ],
	[ 'key' => 'integration_edd', 'label' => __( 'Easy Digital Downloads', 'wppowersuite-lite' ), 'available' => function_exists( 'EDD' ) ],
	[ 'key' => 'integration_pmp', 'label' => __( 'Paid Memberships Pro', 'wppowersuite-lite' ), 'available' => defined( 'PMPRO_VERSION' ) ],
	[ 'key' => 'integration_mc4wp', 'label' => __( 'MC4WP (Mailchimp)', 'wppowersuite-lite' ), 'available' => defined( 'MC4WP_VERSION' ) ],
];

$builtin_text_questions = [];
if ( class_exists( \WP_PowerSuite\Core\ModuleManager::class ) ) {
	$sc_mod = \WP_PowerSuite\Core\ModuleManager::getInstance()->getModule( 'simple-captcha' );
	if ( $sc_mod instanceof \SimpleCaptcha\Module ) {
		$builtin_text_questions = $sc_mod->getBuiltinTextQuestionDefinitions();
	}
}

// Rows shown in the accordion: saved custom rows, or (when none) built-in defaults for editing.
$text_rows_for_ui = [];
$using_builtin_text_rows = false;
foreach ( is_array( $text_questions ) ? $text_questions : [] as $q ) {
	if ( ! is_array( $q ) ) {
		continue;
	}
	$q_question = trim( (string) ( $q['question'] ?? '' ) );
	$q_answer   = trim( (string) ( $q['answer'] ?? '' ) );
	if ( $q_question !== '' && $q_answer !== '' ) {
		$text_rows_for_ui[] = [ 'question' => $q_question, 'answer' => $q_answer ];
	}
}
if ( $text_rows_for_ui === [] && $builtin_text_questions !== [] ) {
	$using_builtin_text_rows = true;
	foreach ( $builtin_text_questions as $b ) {
		$qtxt = trim( (string) ( $b['question'] ?? '' ) );
		$ans  = is_array( $b['answers'] ?? null ) ? $b['answers'] : [];
		$ans  = array_map( 'strval', $ans );
		if ( $qtxt === '' || $ans === [] ) {
			continue;
		}
		$text_rows_for_ui[] = [
			'question' => $qtxt,
			'answer'   => implode( ' | ', $ans ),
		];
	}
}

$uid = preg_replace( '/[^a-z0-9_-]/i', '', $module_id );

$premium_unlocked = class_exists( \SimpleCaptcha\FeatureGate::class ) && \SimpleCaptcha\FeatureGate::isPremiumLicensed();
$overlay_locked   = ! $premium_unlocked;

$wpps_sc_render_overlay_notice = static function () use ( $overlay_locked ): void {
	if ( $overlay_locked && function_exists( 'wpps_render_premium_upsell_notice' ) ) {
		wpps_render_premium_upsell_notice();
	}
};
?>

<?php wpps_render_captcha_overlap_notices( $module_id ); ?>

<div
	class="wp-powersuite-module-settings wpps-settings-panel wpps-settings-panel--accordion-stack"
	data-module-id="<?php echo esc_attr( $module_id ); ?>"
	data-i18n-q="<?php echo esc_attr( __( 'Question', 'wppowersuite-lite' ) ); ?>"
	data-i18n-a="<?php echo esc_attr( __( 'Answer(s), e.g. blue or 7|seven', 'wppowersuite-lite' ) ); ?>"
	data-i18n-a-lbl="<?php echo esc_attr( __( 'Acceptable answer(s)', 'wppowersuite-lite' ) ); ?>"
	data-i18n-del="<?php echo esc_attr( __( 'Delete question', 'wppowersuite-lite' ) ); ?>"
	data-i18n-empty="<?php echo esc_attr( __( '(empty question)', 'wppowersuite-lite' ) ); ?>"
	data-text-questions-fallback="<?php echo $using_builtin_text_rows ? '1' : '0'; ?>"
>
	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<input type="hidden" id="<?php echo esc_attr( $module_id ); ?>_text_questions" class="wp-powersuite-advanced-setting" data-setting-key="text_questions" value="<?php echo esc_attr( wp_json_encode( $text_questions ) ); ?>" />

		<div class="wpps-ts-accordions">
			<div class="wpps-ts-accordion is-open">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_type" aria-expanded="true" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_type">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'CAPTCHA type & messages', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_type" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_type">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_captcha_type"><?php esc_html_e( 'CAPTCHA type', 'wppowersuite-lite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: CAPTCHA type', 'wppowersuite-lite' ),
								__( 'Math, text Q&A, or image CAPTCHA — all run on your server without third-party services.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<select
								id="<?php echo esc_attr( $module_id ); ?>_captcha_type"
								name="<?php echo esc_attr( $module_id ); ?>[captcha_type]"
								class="wp-powersuite-advanced-setting"
								data-setting-key="captcha_type"
							>
								<option value="math" <?php selected( $captcha_type, 'math' ); ?>><?php esc_html_e( 'Math', 'wppowersuite-lite' ); ?></option>
								<option value="text" <?php selected( $captcha_type, 'text' ); ?>><?php esc_html_e( 'Text', 'wppowersuite-lite' ); ?></option>
								<option value="image" <?php selected( $captcha_type, 'image' ); ?>><?php esc_html_e( 'Image', 'wppowersuite-lite' ); ?></option>
							</select>
							<p class="description"><?php esc_html_e( 'Math: simple arithmetic. Text: custom Q&A. Image: type characters from image.', 'wppowersuite-lite' ); ?></p>
						</div>
					</div>

					<div class="wpps-ts-field wpps-sc-math-options"<?php echo $captcha_type !== 'math' ? ' hidden' : ''; ?>>
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_math_difficulty"><?php esc_html_e( 'Math difficulty', 'wppowersuite-lite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: math difficulty', 'wppowersuite-lite' ),
								__( 'Easy: 1–10 addition. Medium: 5–20 with +/-. Hard: 10–99 with +/–/*.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<select
								id="<?php echo esc_attr( $module_id ); ?>_math_difficulty"
								name="<?php echo esc_attr( $module_id ); ?>[math_difficulty]"
								class="wp-powersuite-advanced-setting"
								data-setting-key="math_difficulty"
							>
								<option value="easy" <?php selected( $math_difficulty, 'easy' ); ?>><?php esc_html_e( 'Easy', 'wppowersuite-lite' ); ?></option>
								<option value="medium" <?php selected( $math_difficulty, 'medium' ); ?>><?php esc_html_e( 'Medium', 'wppowersuite-lite' ); ?></option>
								<option value="hard" <?php selected( $math_difficulty, 'hard' ); ?>><?php esc_html_e( 'Hard', 'wppowersuite-lite' ); ?></option>
							</select>
						</div>
					</div>

					<div class="wpps-ts-field wpps-sc-text-options"<?php echo $captcha_type !== 'text' ? ' hidden' : ''; ?>>
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Text questions', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: text questions', 'wppowersuite-lite' ),
								__( 'One random question is shown per challenge. If you save no questions, the built-in set is used on the site. For several acceptable answers, separate with | (e.g. 7|seven).', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div id="<?php echo esc_attr( $module_id ); ?>_text_questions_container" class="wpps-sc-text-accordion" role="region" aria-label="<?php esc_attr_e( 'Text CAPTCHA questions', 'wppowersuite-lite' ); ?>">
								<?php foreach ( $text_rows_for_ui as $idx => $q ) : ?>
									<?php
									$q_question  = (string) ( $q['question'] ?? '' );
									$q_answer    = (string) ( $q['answer'] ?? '' );
									$sum_preview = $q_question !== '' ? ( function_exists( 'mb_substr' ) ? mb_substr( $q_question, 0, 72 ) : substr( $q_question, 0, 72 ) ) : (string) __( '(empty question)', 'wppowersuite-lite' );
									if ( function_exists( 'mb_strlen' ) ? ( mb_strlen( $q_question ) > 72 ) : ( strlen( $q_question ) > 72 ) ) {
										$sum_preview .= '…';
									}
									?>
									<details class="wpps-sc-text-accordion__item" data-index="<?php echo (int) $idx; ?>" <?php echo 0 === (int) $idx ? 'open' : ''; ?>>
										<summary class="wpps-sc-text-accordion__summary"><?php echo esc_html( $sum_preview ); ?></summary>
										<div class="wpps-sc-text-accordion__body">
											<div class="wpps-sc-text-accordion__field">
												<label class="screen-reader-text" for="<?php echo esc_attr( $module_id ); ?>-tq-q-<?php echo (int) $idx; ?>"><?php esc_html_e( 'Question', 'wppowersuite-lite' ); ?></label>
												<input
													type="text"
													id="<?php echo esc_attr( $module_id ); ?>-tq-q-<?php echo (int) $idx; ?>"
													class="wpps-sc-question large-text"
													placeholder="<?php esc_attr_e( 'Question', 'wppowersuite-lite' ); ?>"
													value="<?php echo esc_attr( $q_question ); ?>"
												/>
											</div>
											<div class="wpps-sc-text-accordion__field" style="margin-top:8px;">
												<label class="screen-reader-text" for="<?php echo esc_attr( $module_id ); ?>-tq-a-<?php echo (int) $idx; ?>"><?php esc_html_e( 'Acceptable answer(s)', 'wppowersuite-lite' ); ?></label>
												<input
													type="text"
													id="<?php echo esc_attr( $module_id ); ?>-tq-a-<?php echo (int) $idx; ?>"
													class="wpps-sc-answer large-text"
													placeholder="<?php esc_attr_e( 'Answer(s), e.g. blue or 7|seven', 'wppowersuite-lite' ); ?>"
													value="<?php echo esc_attr( $q_answer ); ?>"
												/>
											</div>
											<p class="wpps-sc-text-accordion__actions" style="margin:10px 0 0;">
												<button type="button" class="button-link wpps-sc-remove-row" style="color:#b32d2e; border:0; background:transparent; padding:0; cursor:pointer; text-decoration:underline;"><?php esc_html_e( 'Delete question', 'wppowersuite-lite' ); ?></button>
											</p>
										</div>
									</details>
								<?php endforeach; ?>
							</div>
							<?php if ( $text_rows_for_ui === [] && $captcha_type === 'text' ) : ?>
								<p class="description wpps-sc-text-empty-hint" style="margin:8px 0;"><?php esc_html_e( 'No questions in the list yet — the plugin will use built-in questions until you add and save your own. Click “Add question” to start.', 'wppowersuite-lite' ); ?></p>
							<?php endif; ?>
							<p style="margin-top: 10px;">
								<button type="button" class="button button-secondary wpps-sc-add-row"><?php esc_html_e( 'Add question', 'wppowersuite-lite' ); ?></button>
							</p>
						</div>
					</div>

					<div class="wpps-ts-field wpps-sc-image-options"<?php echo $captcha_type !== 'image' ? ' hidden' : ''; ?>>
						<div class="wpps-ts-controls-row wpps-ts-controls-row--adv-2col">
							<div class="wpps-ts-controls-row__cell">
								<div class="wpps-ts-field__label-row">
									<label for="<?php echo esc_attr( $module_id ); ?>_image_style"><?php esc_html_e( 'Image style', 'wppowersuite-lite' ); ?></label>
								</div>
								<select
									id="<?php echo esc_attr( $module_id ); ?>_image_style"
									name="<?php echo esc_attr( $module_id ); ?>[image_style]"
									class="wp-powersuite-advanced-setting"
									data-setting-key="image_style"
								>
									<option value="distorted" <?php selected( $image_style, 'distorted' ); ?>><?php esc_html_e( 'Distorted', 'wppowersuite-lite' ); ?></option>
									<option value="simple" <?php selected( $image_style, 'simple' ); ?>><?php esc_html_e( 'Simple', 'wppowersuite-lite' ); ?></option>
								</select>
								<p class="description"><?php esc_html_e( 'Distorted adds lines and noise. Simple is cleaner.', 'wppowersuite-lite' ); ?></p>
							</div>
							<div class="wpps-ts-controls-row__cell">
								<div class="wpps-ts-field__label-row">
									<label for="<?php echo esc_attr( $module_id ); ?>_image_length"><?php esc_html_e( 'Image length', 'wppowersuite-lite' ); ?></label>
								</div>
								<input
									type="number"
									id="<?php echo esc_attr( $module_id ); ?>_image_length"
									name="<?php echo esc_attr( $module_id ); ?>[image_length]"
									value="<?php echo esc_attr( $image_length ); ?>"
									min="4"
									max="8"
									class="small-text wp-powersuite-advanced-setting"
									data-setting-key="image_length"
								/>
								<p class="description"><?php esc_html_e( 'Number of characters in the image (4–8).', 'wppowersuite-lite' ); ?></p>
							</div>
						</div>
					</div>

					<div class="wpps-ts-field wpps-sc-image-options"<?php echo $captcha_type !== 'image' ? ' hidden' : ''; ?>>
						<div class="wpps-ts-field__control">
							<label class="wpps-ts-inline-check">
								<input
									type="checkbox"
									name="<?php echo esc_attr( $module_id ); ?>[image_case_sensitive]"
									value="1"
									<?php checked( $image_case_sensitive ); ?>
									class="wp-powersuite-advanced-setting"
									data-setting-key="image_case_sensitive"
								/>
								<?php esc_html_e( 'Require exact case for image CAPTCHA', 'wppowersuite-lite' ); ?>
							</label>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Visitors', 'wppowersuite-lite' ); ?></span>
						</div>
						<div class="wpps-ts-field__control">
							<label class="wpps-ts-inline-check">
								<input
									type="checkbox"
									name="<?php echo esc_attr( $module_id ); ?>[skip_logged_in]"
									value="1"
									<?php checked( $skip_logged_in ); ?>
									class="wp-powersuite-advanced-setting"
									data-setting-key="skip_logged_in"
								/>
								<?php esc_html_e( "Don't show CAPTCHA to logged-in users", 'wppowersuite-lite' ); ?>
							</label>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_error_message"><?php esc_html_e( 'Error message', 'wppowersuite-lite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: error message', 'wppowersuite-lite' ),
								__( 'Custom message when verification fails. Leave blank for the default.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<input
								type="text"
								id="<?php echo esc_attr( $module_id ); ?>_error_message"
								name="<?php echo esc_attr( $module_id ); ?>[error_message]"
								value="<?php echo esc_attr( $error_message ); ?>"
								class="wp-powersuite-advanced-setting"
								data-setting-key="error_message"
								placeholder="<?php esc_attr_e( 'Please complete the verification.', 'wppowersuite-lite' ); ?>"
							/>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_wp" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_wp">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'WordPress core', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_wp" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_wp">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Protected forms', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: WordPress forms', 'wppowersuite-lite' ),
								__( 'Adds Simple CAPTCHA to core WordPress flows. Enable only what you use — each adds a verification step for visitors.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_login]" value="1" <?php checked( $protect_login ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_login" />
									<?php esc_html_e( 'Login', 'wppowersuite-lite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_registration]" value="1" <?php checked( $protect_registration ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_registration" />
									<?php esc_html_e( 'Registration', 'wppowersuite-lite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_password_reset]" value="1" <?php checked( $protect_password_reset ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_password_reset" />
									<?php esc_html_e( 'Password reset', 'wppowersuite-lite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_comments]" value="1" <?php checked( $protect_comments ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_comments" />
									<?php esc_html_e( 'Comments', 'wppowersuite-lite' ); ?>
								</label>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_woo" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_woo">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'WooCommerce', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_woo" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_woo">
					<?php if ( ! $woo_active ) : ?>
						<p class="description"><?php esc_html_e( 'WooCommerce is not active. These options stay disabled until you install and activate WooCommerce.', 'wppowersuite-lite' ); ?></p>
					<?php endif; ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Protected forms', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: WooCommerce', 'wppowersuite-lite' ),
								__( 'Adds Simple CAPTCHA to storefront forms. Product reviews only applies when Comments protection above is off.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_login]" value="1" <?php checked( $woo_login ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_login" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Login', 'wppowersuite-lite' ); ?>
								</label>
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_registration]" value="1" <?php checked( $woo_registration ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_registration" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Registration', 'wppowersuite-lite' ); ?>
								</label>
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_lost_password]" value="1" <?php checked( $woo_lost_password ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_lost_password" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Lost password', 'wppowersuite-lite' ); ?>
								</label>
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_checkout]" value="1" <?php checked( $woo_checkout ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_checkout" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Checkout', 'wppowersuite-lite' ); ?>
								</label>
								<label class="wpps-check-grid__label-with-tip<?php echo $woo_active ? '' : ' wpps-ts-integration--unavailable'; ?>">
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_reviews]" value="1" <?php checked( $woo_reviews ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_reviews" <?php disabled( ! $woo_active ); ?> />
									<span><?php esc_html_e( 'Product reviews', 'wppowersuite-lite' ); ?></span>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: product reviews', 'wppowersuite-lite' ),
										__( 'Use when Comments protection is disabled but you still want Simple CAPTCHA on WooCommerce product reviews only.', 'wppowersuite-lite' )
									);
									?>
								</label>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_forms" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_forms">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Forms & integrations', 'wppowersuite-lite' ); ?><?php if ( $overlay_locked && function_exists( 'wpps_render_premium_badge' ) ) { wpps_render_premium_badge(); } ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_forms" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_forms">
					<?php $wpps_sc_render_overlay_notice(); ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Integrations', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: form plugins', 'wppowersuite-lite' ),
								__( 'Supported integrations for Simple CAPTCHA in this plugin. Install and activate a plugin to enable its checkbox. Basic Forms is built into WP PowerSuite.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<?php foreach ( $wpps_sc_integrations as $wpps_sc_int ) : ?>
									<?php
									$ik         = $wpps_sc_int['key'];
									$ia         = $wpps_sc_int['available'];
									$iv         = ! empty( $settings[ $ik ] );
									$is_overlay = \SimpleCaptcha\FeatureGate::isOverlaySetting( $ik );
									?>
								<label<?php echo $ia ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[<?php echo esc_attr( $ik ); ?>]" value="1" <?php checked( $iv ); ?> class="wp-powersuite-advanced-setting" data-setting-key="<?php echo esc_attr( $ik ); ?>" <?php disabled( ! $ia || ( $is_overlay && $overlay_locked ) ); ?> />
									<?php echo esc_html( $wpps_sc_int['label'] ); ?>
									<?php if ( $is_overlay && $overlay_locked && function_exists( 'wpps_render_premium_badge' ) ) { wpps_render_premium_badge(); } ?>
								</label>
								<?php endforeach; ?>
							</div>
							<?php if ( $elementor_pro_active ) : ?>
								<p class="description" style="margin-top:12px;">
									<?php esc_html_e( 'Elementor Pro: adds a “Simple CAPTCHA” field to the Form widget. Edit your template, open the Form → add a field → Field type: Simple CAPTCHA (place it where you want, usually before Submit).', 'wppowersuite-lite' ); ?>
								</p>
							<?php endif; ?>
							<?php if ( defined( 'MC4WP_VERSION' ) ) : ?>
								<p class="description" style="margin-top:12px;">
									<?php esc_html_e( 'MC4WP: place this shortcode in the form editor above the submit button (no auto-injection):', 'wppowersuite-lite' ); ?>
									<code>[wpps-mc4wp-simple-captcha]</code>
								</p>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_adv" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_adv">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Advanced options', 'wppowersuite-lite' ); ?><?php if ( $overlay_locked && function_exists( 'wpps_render_premium_badge' ) ) { wpps_render_premium_badge(); } ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_adv" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_adv">
					<?php $wpps_sc_render_overlay_notice(); ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row"><?php esc_html_e( 'Anti-bot', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[anti_bot_honeypot]" value="1" <?php checked( $anti_bot_honeypot ); ?> class="wp-powersuite-advanced-setting" data-setting-key="anti_bot_honeypot" <?php disabled( $overlay_locked ); ?> />
									<?php esc_html_e( 'Honeypot field (hidden trap for bots)', 'wppowersuite-lite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[anti_bot_timing]" value="1" <?php checked( $anti_bot_timing ); ?> class="wp-powersuite-advanced-setting" data-setting-key="anti_bot_timing" <?php disabled( $overlay_locked ); ?> />
									<?php esc_html_e( 'Reject submissions that are too fast', 'wppowersuite-lite' ); ?>
								</label>
							</div>
							<p class="description" style="margin:12px 0 0;">
								<label for="<?php echo esc_attr( $module_id ); ?>_min_submit_seconds"><?php esc_html_e( 'Minimum seconds before submit', 'wppowersuite-lite' ); ?></label>
								<input type="number" id="<?php echo esc_attr( $module_id ); ?>_min_submit_seconds" name="<?php echo esc_attr( $module_id ); ?>[min_submit_seconds]" value="<?php echo esc_attr( $min_submit_seconds ); ?>" min="1" max="30" class="small-text wp-powersuite-advanced-setting" data-setting-key="min_submit_seconds" <?php disabled( $overlay_locked ); ?> />
							</p>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row"><?php esc_html_e( 'Behavior', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[ajax_comment_reset]" value="1" <?php checked( $ajax_comment_reset ); ?> class="wp-powersuite-advanced-setting" data-setting-key="ajax_comment_reset" <?php disabled( $overlay_locked ); ?> />
									<?php esc_html_e( 'Clear CAPTCHA when replying to comments (AJAX)', 'wppowersuite-lite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[login_canonical_only]" value="1" <?php checked( $login_canonical_only ); ?> class="wp-powersuite-advanced-setting" data-setting-key="login_canonical_only" <?php disabled( $overlay_locked ); ?> />
									<?php esc_html_e( 'Protect wp-login.php login only', 'wppowersuite-lite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[register_canonical_only]" value="1" <?php checked( $register_canonical_only ); ?> class="wp-powersuite-advanced-setting" data-setting-key="register_canonical_only" <?php disabled( $overlay_locked ); ?> />
									<?php esc_html_e( 'Protect wp-login.php registration only', 'wppowersuite-lite' ); ?>
								</label>
								<?php if ( $woo_active ) : ?>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_checkout_guest_only]" value="1" <?php checked( $woo_checkout_guest_only ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_checkout_guest_only" <?php disabled( $overlay_locked ); ?> />
									<?php esc_html_e( 'WooCommerce checkout: guests only', 'wppowersuite-lite' ); ?>
								</label>
								<?php endif; ?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_skip" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_skip">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Skip rules', 'wppowersuite-lite' ); ?><?php if ( $overlay_locked && function_exists( 'wpps_render_premium_badge' ) ) { wpps_render_premium_badge(); } ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_skip" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_skip">
					<?php $wpps_sc_render_overlay_notice(); ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Trusted visitors', 'wppowersuite-lite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: skip rules', 'wppowersuite-lite' ),
								__( 'Matching roles or IPs bypass the challenge (no CAPTCHA and no server verification for matching visitors). IP rules require correct proxy/Cloudflare configuration. Leave lists empty when skip is enabled to effectively disable skipping.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<label class="wpps-ts-inline-check">
								<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[skip_rules][enabled]" value="1" <?php checked( $skip_enabled ); ?> class="wp-powersuite-advanced-setting" data-setting-key="skip_rules.enabled" <?php disabled( $overlay_locked ); ?> />
								<?php esc_html_e( 'Enable skip rules', 'wppowersuite-lite' ); ?>
							</label>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row"><?php esc_html_e( 'Allowed roles', 'wppowersuite-lite' ); ?></div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid wpps-check-grid--roles">
								<?php if ( ! empty( $all_roles ) ) : ?>
									<?php foreach ( $all_roles as $slug => $role_data ) : ?>
										<label>
											<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[skip_rules][roles][]" value="<?php echo esc_attr( $slug ); ?>" <?php checked( in_array( $slug, $skip_roles_saved, true ) ); ?> class="wp-powersuite-advanced-setting" data-setting-key="skip_rules.roles" <?php disabled( $overlay_locked ); ?> />
											<?php echo esc_html( is_array( $role_data ) && isset( $role_data['name'] ) ? $role_data['name'] : $slug ); ?>
										</label>
									<?php endforeach; ?>
								<?php endif; ?>
							</div>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_skip_ips"><?php esc_html_e( 'Allowed IPs / CIDR', 'wppowersuite-lite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: IP allowlist', 'wppowersuite-lite' ),
								__( 'One IP or CIDR per line. Supports IPv4 and IPv6. Useful for office networks; misconfiguration can weaken protection.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<textarea
								id="<?php echo esc_attr( $module_id ); ?>_skip_ips"
								name="<?php echo esc_attr( $module_id ); ?>[skip_rules][ips]"
								class="large-text code wp-powersuite-advanced-setting"
								data-setting-key="skip_rules.ips"
								rows="4"
								placeholder="203.0.113.5&#10;10.0.0.0/24"
								spellcheck="false"
								<?php disabled( $overlay_locked ); ?>
							><?php echo esc_textarea( $skip_ips_textarea ); ?></textarea>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_skip_user_agents"><?php esc_html_e( 'User-Agent substrings', 'wppowersuite-lite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: User-Agent skip list', 'wppowersuite-lite' ),
								__( 'One substring per line matched against the visitor User-Agent (case-sensitive). Useful for trusted bots or internal tools.', 'wppowersuite-lite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<textarea
								id="<?php echo esc_attr( $module_id ); ?>_skip_user_agents"
								name="<?php echo esc_attr( $module_id ); ?>[skip_rules][user_agents]"
								class="large-text code wp-powersuite-advanced-setting"
								data-setting-key="skip_rules.user_agents"
								rows="3"
								placeholder="SomeBot/1.0"
								spellcheck="false"
								<?php disabled( $overlay_locked ); ?>
							><?php echo esc_textarea( $skip_uas_textarea ); ?></textarea>
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>
</div>
