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

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

$allowed = [ 'username_email', 'username_only', 'email_only' ];
$login_id_type = isset( $settings['login_id_type'] ) && in_array( $settings['login_id_type'], $allowed, true )
	? $settings['login_id_type']
	: 'username_email';
// Backward compat: treat legacy "both" as username_email
if ( isset( $settings['login_id_type'] ) && $settings['login_id_type'] === 'both' ) {
	$login_id_type = 'username_email';
}

$generic_errors_warning = '';
if ( OptionStoreModuleSettingsRepository::isModuleEnabled( 'custom-login-error-messages'  ) && $login_id_type !== 'username_email' ) {
	$generic_errors_warning = __( 'Generic Login Error Message is also enabled. Login ID enforcement still applies, but error text is handled by that module. For predictable behavior, use either module in a restricted mode—not both.', 'wp-powersuite' );
}

?>
<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<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' ); ?>

					<p class="description" style="margin-bottom: 16px;">
						<?php esc_html_e( 'Control how users log in. Username & Email mode keeps native WordPress behavior. Username Only and Email Only also update login labels and replace credential errors with one instructional message.', 'wp-powersuite' ); ?>
					</p>

					<?php if ( $generic_errors_warning !== '' ) : ?>
					<div class="notice notice-warning inline" style="margin: 0 0 16px; padding: 10px 12px;">
						<p style="margin: 0;"><?php echo esc_html( $generic_errors_warning ); ?></p>
					</div>
					<?php endif; ?>

					<table class="form-table" role="presentation">
						<tbody>
							<tr>
								<th scope="row">
									<label><?php esc_html_e( 'Login ID Type', 'wp-powersuite' ); ?></label>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: login ID', 'wp-powersuite' ),
										__( 'Choose whether users can log in with username, email, or both. Error messages remain generic for security.', 'wp-powersuite' )
									);
									?>
								</th>
								<td>
									<fieldset>
										<label>
											<input
												type="radio"
												name="<?php echo esc_attr( $module_id ); ?>[login_id_type]"
												class="wp-powersuite-advanced-setting"
												data-setting-key="login_id_type"
												value="username_email"
												<?php checked( $login_id_type, 'username_email' ); ?>
											/>
											<strong><?php esc_html_e( 'Username & Email', 'wp-powersuite' ); ?></strong>
										</label>
										<p class="description" style="margin-left: 24px;">
											<?php esc_html_e( 'Default WordPress behavior. Does not change login errors. Enable Generic Login Error Message for anti-enumeration on this mode.', 'wp-powersuite' ); ?>
										</p>

										<label style="display: block; margin-top: 12px;">
											<input
												type="radio"
												name="<?php echo esc_attr( $module_id ); ?>[login_id_type]"
												class="wp-powersuite-advanced-setting"
												data-setting-key="login_id_type"
												value="username_only"
												<?php checked( $login_id_type, 'username_only' ); ?>
											/>
											<strong><?php esc_html_e( 'Username Only', 'wp-powersuite' ); ?></strong>
										</label>
										<p class="description" style="margin-left: 24px;">
											<?php esc_html_e( 'Users must log in with username. Helps prevent email enumeration.', 'wp-powersuite' ); ?>
										</p>

										<label style="display: block; margin-top: 12px;">
											<input
												type="radio"
												name="<?php echo esc_attr( $module_id ); ?>[login_id_type]"
												class="wp-powersuite-advanced-setting"
												data-setting-key="login_id_type"
												value="email_only"
												<?php checked( $login_id_type, 'email_only' ); ?>
											/>
											<strong><?php esc_html_e( 'Email Only', 'wp-powersuite' ); ?></strong>
										</label>
										<p class="description" style="margin-left: 24px;">
											<?php esc_html_e( 'Users must log in with their email address.', 'wp-powersuite' ); ?>
										</p>
									</fieldset>
								</td>
							</tr>
						</tbody>
					</table>

					<div class="wp-powersuite-info-box" style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 12px; margin: 20px 0;">
						<p style="margin: 0 0 8px 0;">
							<strong><?php esc_html_e( 'Scope', 'wp-powersuite' ); ?></strong>
						</p>
						<p style="margin: 0 0 8px 0;">
							<?php esc_html_e( 'Restricted modes update wp-login and WooCommerce My Account labels, and replace credential-failure messages on wp-login, WooCommerce, REST authentication, XML-RPC, and PowerSuite 2FA AJAX login.', 'wp-powersuite' ); ?>
						</p>
						<p style="margin: 0;">
							<?php esc_html_e( 'Registration and password reset are not changed. Third-party LMS or membership login forms can opt in via the wp_powersuite_is_credential_login_request filter.', 'wp-powersuite' ); ?>
						</p>
					</div>
</form>
</div>
