<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<div class="wrap zacls-dashboard">
	<h1><?php esc_html_e( 'Security Wizard', 'za-creative-login-shield' ); ?></h1>
	<p class="description"><?php esc_html_e( 'Apply recommended security settings with one click, or configure each setting individually.', 'za-creative-login-shield' ); ?></p>

	<div class="zacls-wizard-progress" id="zacls-wizard-progress">
		<div class="wizard-step-indicator active" data-step="1">1</div>
		<div class="wizard-step-indicator" data-step="2">2</div>
		<div class="wizard-step-indicator" data-step="3">3</div>
		<div class="wizard-step-indicator" data-step="4">4</div>
		<div class="wizard-step-indicator" data-step="5">5</div>
	</div>

	<div class="zacls-settings-section" id="zacls-wizard-steps">
		<div class="zacls-wizard-step" data-step="1">
			<h2><?php esc_html_e( 'Step 1: One-Click Apply', 'za-creative-login-shield' ); ?></h2>
			<p><?php esc_html_e( 'Apply all recommended security settings at once with the button below. This will enable 2FA for administrators, configure login rate limiting, enforce strong passwords, and more.', 'za-creative-login-shield' ); ?></p>
			<p>
				<button type="button" class="zacls-btn zacls-btn-primary" id="zacls-wizard-apply-all">
					<span class="dashicons dashicons-yes-alt"></span>
					<?php esc_html_e( 'Apply Recommended Settings', 'za-creative-login-shield' ); ?>
				</button>
			</p>
			<div id="zacls-wizard-result" style="display:none;margin-top:16px;padding:12px;border-radius:4px;"></div>
		</div>

		<div class="zacls-wizard-step" data-step="2" style="display:none">
			<h2><?php esc_html_e( 'Step 2: Two-Factor Authentication', 'za-creative-login-shield' ); ?></h2>
			<p><?php esc_html_e( 'Enable 2FA to add an extra layer of security to user accounts.', 'za-creative-login-shield' ); ?></p>
			<label>
				<input type="checkbox" id="wizard-2fa" <?php checked( 'yes' === get_option( 'zacls_2fa_enabled', 'no' ) ); ?>>
				<?php esc_html_e( 'Enable Two-Factor Authentication', 'za-creative-login-shield' ); ?>
			</label>
		</div>

		<div class="zacls-wizard-step" data-step="3" style="display:none">
			<h2><?php esc_html_e( 'Step 3: Login Limits', 'za-creative-login-shield' ); ?></h2>
			<p><?php esc_html_e( 'Configure rate limiting to prevent brute-force attacks.', 'za-creative-login-shield' ); ?></p>
			<label>
				<input type="checkbox" id="wizard-login-limits" <?php checked( get_option( 'zacls_login_limits', array() )['max_attempts'] ?? 0 > 0 ); ?>>
				<?php esc_html_e( 'Enable Login Rate Limiting', 'za-creative-login-shield' ); ?>
			</label>
		</div>

		<div class="zacls-wizard-step" data-step="4" style="display:none">
			<h2><?php esc_html_e( 'Step 4: Password Policy', 'za-creative-login-shield' ); ?></h2>
			<p><?php esc_html_e( 'Enforce strong password requirements for all users.', 'za-creative-login-shield' ); ?></p>
			<label>
				<input type="checkbox" id="wizard-password-policy" <?php checked( ( get_option( 'zacls_password_policy', array() )['min_length'] ?? 0 ) >= 8 ); ?>>
				<?php esc_html_e( 'Enforce Strong Password Policy', 'za-creative-login-shield' ); ?>
			</label>
		</div>

		<div class="zacls-wizard-step" data-step="5" style="display:none">
			<h2><?php esc_html_e( 'Step 5: Custom Login URL', 'za-creative-login-shield' ); ?></h2>
			<p><?php esc_html_e( 'Change the default login page URL to reduce automated attacks.', 'za-creative-login-shield' ); ?></p>
			<label>
				<input type="checkbox" id="wizard-custom-url" <?php checked( '' !== get_option( 'zacls_custom_login_slug', '' ) ); ?>>
				<?php esc_html_e( 'Use Custom Login URL', 'za-creative-login-shield' ); ?>
			</label>
		</div>
	</div>

	<div class="zacls-wizard-nav">
		<button type="button" class="zacls-btn" id="wizard-prev" style="display:none"><?php esc_html_e( 'Previous', 'za-creative-login-shield' ); ?></button>
		<button type="button" class="zacls-btn zacls-btn-primary" id="wizard-next"><?php esc_html_e( 'Next', 'za-creative-login-shield' ); ?></button>
		<button type="button" class="zacls-btn" id="wizard-skip" style="display:none"><?php esc_html_e( 'Skip', 'za-creative-login-shield' ); ?></button>
		<button type="button" class="zacls-btn zacls-btn-primary" id="wizard-finish" style="display:none"><?php esc_html_e( 'Finish & Save', 'za-creative-login-shield' ); ?></button>
	</div>
</div>

<script>
jQuery(function($) {
	var currentStep = 1;
	var totalSteps = 5;

	function updateWizard() {
		$('.zacls-wizard-step').hide();
		$('.zacls-wizard-step[data-step="' + currentStep + '"]').show();
		$('.wizard-step-indicator').removeClass('active');
		$('.wizard-step-indicator[data-step="' + currentStep + '"]').addClass('active');

		$('#wizard-prev').toggle(currentStep > 1);
		$('#wizard-next').toggle(currentStep < totalSteps);
		$('#wizard-finish').toggle(currentStep === totalSteps);
		$('#wizard-skip').toggle(currentStep > 1 && currentStep < totalSteps);
	}

	$('#wizard-next').on('click', function() {
		if (currentStep < totalSteps) {
			currentStep++;
			updateWizard();
		}
	});

	$('#wizard-prev').on('click', function() {
		if (currentStep > 1) {
			currentStep--;
			updateWizard();
		}
	});

	$('#wizard-finish').on('click', function() {
		var data = {
			action: 'zacls_save_settings',
			nonce: zaclsData.nonce,
			settings: {
				zacls_2fa_enabled: $('#wizard-2fa').is(':checked') ? 'yes' : 'no',
				zacls_block_wp_login: $('#wizard-custom-url').is(':checked') ? 'yes' : 'no',
				zacls_password_policy: {
					min_length: $('#wizard-password-policy').is(':checked') ? 8 : 0,
					require_upper: 'yes',
					require_lower: 'yes',
					require_number: 'yes',
					require_special: 'yes'
				},
				zacls_login_limits: {
					max_attempts: $('#wizard-login-limits').is(':checked') ? 5 : 0,
					time_window: 15,
					lockout_time: 15
				}
			}
		};

		$.post(zaclsData.ajaxUrl, data, function(response) {
			if (response.success) {
				$('#zacls-wizard-result').removeClass('notice-error').addClass('notice-success')
					.html('<p>' + response.data.message + '</p>').show();
				setTimeout(function() { window.location.href = zaclsData.redirectUrl + '&tab=dashboard'; }, 1500);
			} else {
				$('#zacls-wizard-result').removeClass('notice-success').addClass('notice-error')
					.html('<p>' + (response.data.message || zaclsData.i18n.error) + '</p>').show();
			}
		});
	});

	$('#zacls-wizard-apply-all').on('click', function() {
		var btn = $(this).prop('disabled', true).html('<span class="spinner is-active" style="float:none;margin:0 6px 0 0"></span> <?php esc_html_e( 'Applying...', 'za-creative-login-shield' ); ?>');

		$.post(zaclsData.ajaxUrl, {
			action: 'zacls_apply_recommended',
			nonce: zaclsData.nonce
		}, function(response) {
			btn.prop('disabled', false).html('<span class="dashicons dashicons-yes-alt" style="vertical-align:middle;margin-right:6px"></span> <?php esc_html_e( 'Apply Recommended Settings', 'za-creative-login-shield' ); ?>');
			var result = $('#zacls-wizard-result');
			if (response.success) {
				result.removeClass('notice-error').addClass('notice-success')
					.html('<p>' + response.data.message + '</p>').show();
				setTimeout(function() { window.location.href = zaclsData.redirectUrl + '&tab=dashboard'; }, 2000);
			} else {
				result.removeClass('notice-success').addClass('notice-error')
					.html('<p>' + (response.data.message || zaclsData.i18n.error) + '</p>').show();
			}
		});
	});

	updateWizard();
});
</script>
