<?php
/**
 * Setup Wizard template.
 *
 * Rendered inside the main admin layout container (see settings.php template).
 *
 * @package WLC\Algolia
 */

defined( 'ABSPATH' ) || die( 'Direct file access is forbidden' );

( static function ( array $wlc_algolia_scope_vars ) {
	extract( $wlc_algolia_scope_vars, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract

	$wlc_algolia_step  = \wlc_algolia\Admin_Wizard::get_step();
	$wlc_algolia_state = \wlc_algolia\Admin_Wizard::get_state();

	$wlc_algolia_completed           = isset( $wlc_algolia_state['completed_steps'] ) && is_array( $wlc_algolia_state['completed_steps'] ) ? $wlc_algolia_state['completed_steps'] : array();
	$wlc_algolia_preset              = isset( $wlc_algolia_state['preset'] ) ? sanitize_key( (string) $wlc_algolia_state['preset'] ) : '';
	$wlc_algolia_primary_post_type   = isset( $wlc_algolia_state['primary_post_type'] ) ? sanitize_key( (string) $wlc_algolia_state['primary_post_type'] ) : 'post';
	$wlc_algolia_selected_post_types = isset( $wlc_algolia_state['selected_post_types'] ) && is_array( $wlc_algolia_state['selected_post_types'] ) ? array_map( 'sanitize_key', $wlc_algolia_state['selected_post_types'] ) : array();

	// Read existing options for better defaults.
	$wlc_algolia_general     = get_option( 'wlc_algolia_settings_general', array() );
	$wlc_algolia_general_tab = is_array( $wlc_algolia_general ) && isset( $wlc_algolia_general['general'] ) && is_array( $wlc_algolia_general['general'] ) ? $wlc_algolia_general['general'] : array();

	$wlc_algolia_post_type_index = get_option( 'wlc_algolia_settings_post_type_index', array() );
	$wlc_algolia_post_type_index = is_array( $wlc_algolia_post_type_index ) ? $wlc_algolia_post_type_index : array();

	$wlc_algolia_autocomplete = get_option( 'wlc_algolia_settings_autocomplete', array() );
	$wlc_algolia_autocomplete = is_array( $wlc_algolia_autocomplete ) ? $wlc_algolia_autocomplete : array();

	$wlc_algolia_is_completed = (bool) get_user_meta( get_current_user_id(), 'wlc_algolia_wizard_completed', true );

	// Status message (after redirect).
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$wlc_algolia_status = isset( $_GET['wlc_status'] ) ? sanitize_key( (string) $_GET['wlc_status'] ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$wlc_algolia_message = isset( $_GET['wlc_message'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['wlc_message'] ) ) : '';

	$wlc_algolia_steps = array(
		1 => __( 'Connect', 'wlc-search-integration-with-algolia' ),
		2 => __( 'Post types', 'wlc-search-integration-with-algolia' ),
		3 => __( 'Schema & facets', 'wlc-search-integration-with-algolia' ),
		4 => __( 'Autocomplete', 'wlc-search-integration-with-algolia' ),
		5 => __( 'Index & finish', 'wlc-search-integration-with-algolia' ),
	);

	$wlc_algolia_post_types = \wlc_algolia\Helpers\wlc_retrieve_searchable_post_type_objects();

	// Derive a sensible default preset if not set.
	if ( '' === $wlc_algolia_preset ) {
		$wlc_algolia_preset = post_type_exists( 'product' ) ? 'woo' : 'content';
	}

	// Allow changing previewed primary post type without saving (Step 3 UX).
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$wlc_algolia_primary_preview = isset( $_GET['primary_post_type'] ) ? sanitize_key( (string) $_GET['primary_post_type'] ) : '';
	if ( '' !== $wlc_algolia_primary_preview && post_type_exists( $wlc_algolia_primary_preview ) ) {
		$wlc_algolia_primary_post_type = $wlc_algolia_primary_preview;
	}

	$wlc_algolia_total_steps  = count( $wlc_algolia_steps );
	$wlc_algolia_done_steps   = count( array_filter( $wlc_algolia_completed ) );
	$wlc_algolia_progress_pct = (int) round( ( $wlc_algolia_done_steps / max( 1, $wlc_algolia_total_steps ) ) * 100 );

	$wlc_algolia_base_index_prefix = (string) ( $wlc_algolia_general_tab['index_name'] ?? '' );

	// Recommended post types by preset (only if exists).
	$wlc_algolia_recommended_post_types = array();
	if ( 'woo' === $wlc_algolia_preset ) {
		$wlc_algolia_recommended_post_types = array( 'product', 'page' );
	} elseif ( 'mixed' === $wlc_algolia_preset ) {
		$wlc_algolia_recommended_post_types = array( 'post', 'page', 'product' );
	} else {
		$wlc_algolia_recommended_post_types = array( 'post', 'page' );
	}
	$wlc_algolia_recommended_post_types = array_values( array_filter( $wlc_algolia_recommended_post_types, 'post_type_exists' ) );
	?>

	<?php if ( '' !== $wlc_algolia_status && '' !== $wlc_algolia_message ) : ?>
	<div class="wlc-wizard-notice <?php echo 'success' === $wlc_algolia_status ? 'is-success' : 'is-error'; ?>">
		<?php echo esc_html( $wlc_algolia_message ); ?>
	</div>
<?php endif; ?>

<div class="wlc-wizard-shell" data-component="wlc-wizard" data-step="<?php echo esc_attr( (string) $wlc_algolia_step ); ?>" data-preset="<?php echo esc_attr( $wlc_algolia_preset ); ?>">
	<div class="wlc-wizard-hero">
		<div class="wlc-wizard-hero-left">
			<div class="wlc-wizard-kicker"><?php esc_html_e( 'Fast onboarding', 'wlc-search-integration-with-algolia' ); ?></div>
			<div class="wlc-wizard-title"><?php esc_html_e( 'Setup Wizard', 'wlc-search-integration-with-algolia' ); ?></div>
			<div class="wlc-wizard-subtitle"><?php esc_html_e( 'We’ll guide you through a clean, working configuration. You can always fine-tune later in Advanced tabs.', 'wlc-search-integration-with-algolia' ); ?></div>
		</div>
		<div class="wlc-wizard-hero-right">
			<div class="wlc-wizard-progress">
				<div class="wlc-wizard-progress-meta">
					<?php /* translators: 1: current step number, 2: total steps number */ ?>
					<span><?php echo esc_html( sprintf( __( '%1$d/%2$d steps', 'wlc-search-integration-with-algolia' ), $wlc_algolia_done_steps, $wlc_algolia_total_steps ) ); ?></span>
					<span class="wlc-wizard-progress-pct"><?php echo esc_html( (string) $wlc_algolia_progress_pct ); ?>%</span>
				</div>
				<div class="wlc-wizard-progress-bar">
					<div class="wlc-wizard-progress-bar-fill" style="width: <?php echo esc_attr( (string) $wlc_algolia_progress_pct ); ?>%"></div>
				</div>
			</div>
			<div class="wlc-wizard-chip">
				<?php echo esc_html( strtoupper( $wlc_algolia_preset ) ); ?>
			</div>
		</div>
	</div>

	<div class="wlc-wizard-layout">
		<aside class="wlc-wizard-nav" aria-label="<?php esc_attr_e( 'Setup steps', 'wlc-search-integration-with-algolia' ); ?>">
			<?php foreach ( $wlc_algolia_steps as $wlc_algolia_i => $wlc_algolia_label ) : ?>
				<?php
				$wlc_algolia_is_active = $wlc_algolia_i === $wlc_algolia_step;
				$wlc_algolia_is_done   = ! empty( $wlc_algolia_completed[ $wlc_algolia_i ] );
				$wlc_algolia_url       = admin_url( 'admin.php?page=wlc-algolia&tab=wizard&step=' . absint( $wlc_algolia_i ) );
				?>
				<a class="wlc-wizard-nav-item <?php echo $wlc_algolia_is_active ? 'is-active' : ''; ?> <?php echo $wlc_algolia_is_done ? 'is-done' : ''; ?>"
					href="<?php echo esc_url( $wlc_algolia_url ); ?>">
					<span class="wlc-wizard-nav-index"><?php echo esc_html( (string) $wlc_algolia_i ); ?></span>
					<span class="wlc-wizard-nav-label"><?php echo esc_html( $wlc_algolia_label ); ?></span>
				</a>
			<?php endforeach; ?>
		</aside>

		<section class="wlc-wizard-panel">
		<?php if ( $wlc_algolia_is_completed ) : ?>
				<div class="wlc-wizard-banner">
					<div>
						<strong><?php esc_html_e( 'Wizard completed.', 'wlc-search-integration-with-algolia' ); ?></strong>
						<div class="wlc-wizard-banner-sub"><?php esc_html_e( 'You can still change everything in the Advanced tabs on the left.', 'wlc-search-integration-with-algolia' ); ?></div>
					</div>
					<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=wlc-algolia&tab=general' ) ); ?>"><?php esc_html_e( 'Open Advanced settings', 'wlc-search-integration-with-algolia' ); ?></a>
				</div>
		<?php endif; ?>

		<?php if ( 1 === $wlc_algolia_step ) : ?>
			<div class="wlc-wizard-section">
				<div class="wlc-wizard-section-title"><?php esc_html_e( 'Choose a preset', 'wlc-search-integration-with-algolia' ); ?></div>
				<div class="wlc-wizard-section-sub"><?php esc_html_e( 'This only changes recommendations in the next steps. You can tweak everything later.', 'wlc-search-integration-with-algolia' ); ?></div>

				<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wlc-wizard-form">
					<input type="hidden" name="action" value="wlc_algolia_wizard_save" />
					<input type="hidden" name="step" value="1" />
					<?php wp_nonce_field( 'wlc_algolia_wizard_save', 'wlc_algolia_wizard_nonce' ); ?>

					<div class="wlc-wizard-presets">
						<label class="wlc-wizard-preset <?php echo 'woo' === $wlc_algolia_preset ? 'is-selected' : ''; ?>">
							<input type="radio" name="preset" value="woo" <?php checked( 'woo', $wlc_algolia_preset ); ?> />
							<div class="wlc-wizard-preset-title"><?php esc_html_e( 'WooCommerce store', 'wlc-search-integration-with-algolia' ); ?></div>
							<div class="wlc-wizard-preset-sub"><?php esc_html_e( 'Products first: price, categories, tags', 'wlc-search-integration-with-algolia' ); ?></div>
						</label>
						<label class="wlc-wizard-preset <?php echo 'content' === $wlc_algolia_preset ? 'is-selected' : ''; ?>">
							<input type="radio" name="preset" value="content" <?php checked( 'content', $wlc_algolia_preset ); ?> />
							<div class="wlc-wizard-preset-title"><?php esc_html_e( 'Content / Blog', 'wlc-search-integration-with-algolia' ); ?></div>
							<div class="wlc-wizard-preset-sub"><?php esc_html_e( 'Posts & pages: fast content search', 'wlc-search-integration-with-algolia' ); ?></div>
						</label>
						<label class="wlc-wizard-preset <?php echo 'mixed' === $wlc_algolia_preset ? 'is-selected' : ''; ?>">
							<input type="radio" name="preset" value="mixed" <?php checked( 'mixed', $wlc_algolia_preset ); ?> />
							<div class="wlc-wizard-preset-title"><?php esc_html_e( 'Mixed site', 'wlc-search-integration-with-algolia' ); ?></div>
							<div class="wlc-wizard-preset-sub"><?php esc_html_e( 'Products + posts (balanced)', 'wlc-search-integration-with-algolia' ); ?></div>
						</label>
					</div>
			</div>

			<div class="wlc-wizard-section">
				<div class="wlc-wizard-section-title"><?php esc_html_e( 'Connect Algolia', 'wlc-search-integration-with-algolia' ); ?></div>
				<div class="wlc-wizard-section-sub"><?php esc_html_e( 'Paste keys from the Algolia dashboard. Test connection instantly (no reload).', 'wlc-search-integration-with-algolia' ); ?></div>

					<label class="wlc-wizard-field">
						<span class="wlc-wizard-field-label"><?php esc_html_e( 'Enable Algolia', 'wlc-search-integration-with-algolia' ); ?></span>
						<select name="general[enable_algolia]">
							<option value="1" <?php selected( '1', $wlc_algolia_general_tab['enable_algolia'] ?? '0' ); ?>><?php esc_html_e( 'Enabled', 'wlc-search-integration-with-algolia' ); ?></option>
							<option value="0" <?php selected( '0', $wlc_algolia_general_tab['enable_algolia'] ?? '0' ); ?>><?php esc_html_e( 'Disabled', 'wlc-search-integration-with-algolia' ); ?></option>
						</select>
					</label>

					<label class="wlc-wizard-field">
						<span class="wlc-wizard-field-label"><?php esc_html_e( 'Application ID', 'wlc-search-integration-with-algolia' ); ?></span>
						<input type="text" name="general[application_id]" value="<?php echo esc_attr( $wlc_algolia_general_tab['application_id'] ?? '' ); ?>" autocomplete="off" data-wlc-field="app_id" />
					</label>

					<label class="wlc-wizard-field">
						<span class="wlc-wizard-field-label"><?php esc_html_e( 'Search API Key', 'wlc-search-integration-with-algolia' ); ?></span>
						<input type="text" name="general[search_api_key]" value="<?php echo esc_attr( $wlc_algolia_general_tab['search_api_key'] ?? '' ); ?>" autocomplete="off" />
					</label>

					<label class="wlc-wizard-field">
						<span class="wlc-wizard-field-label"><?php esc_html_e( 'Write API Key', 'wlc-search-integration-with-algolia' ); ?></span>
						<input type="password" name="general[write_api_key]" value="<?php echo esc_attr( $wlc_algolia_general_tab['write_api_key'] ?? '' ); ?>" autocomplete="off" data-wlc-field="write_key" />
					</label>
			</div>

			<div class="wlc-wizard-section">
				<label class="wlc-wizard-field">
					<span class="wlc-wizard-field-label"><?php esc_html_e( 'Default index name (prefix)', 'wlc-search-integration-with-algolia' ); ?></span>
					<input type="text" name="general[index_name]" value="<?php echo esc_attr( $wlc_algolia_general_tab['index_name'] ?? '' ); ?>" placeholder="woo1m" />
					<span class="wlc-wizard-field-help"><?php esc_html_e( 'We will create indices like {prefix}_{post_type}.', 'wlc-search-integration-with-algolia' ); ?></span>
				</label>

				<div class="wlc-wizard-conn">
					<input type="hidden" id="wlc-wizard-ajax-nonce-test" value="<?php echo esc_attr( wp_create_nonce( 'wlc_algolia_wizard_test_connection' ) ); ?>" />
					<button type="button" class="button" id="wlc-wizard-test-connection"><?php esc_html_e( 'Test connection', 'wlc-search-integration-with-algolia' ); ?></button>
					<div class="wlc-wizard-conn-status" id="wlc-wizard-conn-status" aria-live="polite"></div>
				</div>

				<div class="wlc-wizard-actions">
					<button type="submit" class="button button-primary wlc-algolia-save-button"><?php esc_html_e( 'Save & continue', 'wlc-search-integration-with-algolia' ); ?></button>
				</div>
			</form>
		<?php endif; ?>

		<?php if ( 2 === $wlc_algolia_step ) : ?>
			<div class="wlc-wizard-section">
				<div class="wlc-wizard-section-title"><?php esc_html_e( 'What should be searchable?', 'wlc-search-integration-with-algolia' ); ?></div>
				<div class="wlc-wizard-section-sub"><?php esc_html_e( 'Pick the post types you want in Algolia. We’ll use this to build indices and autocomplete sources.', 'wlc-search-integration-with-algolia' ); ?></div>
				<div class="wlc-wizard-toolbar">
					<button type="button" class="button wlc-wizard-select" data-wlc-select="recommended"><?php esc_html_e( 'Select recommended', 'wlc-search-integration-with-algolia' ); ?></button>
					<button type="button" class="button wlc-wizard-select" data-wlc-select="all"><?php esc_html_e( 'Select all', 'wlc-search-integration-with-algolia' ); ?></button>
					<button type="button" class="button wlc-wizard-select" data-wlc-select="none"><?php esc_html_e( 'Select none', 'wlc-search-integration-with-algolia' ); ?></button>
				</div>
			</div>

			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wlc-wizard-form">
				<input type="hidden" name="action" value="wlc_algolia_wizard_save" />
				<input type="hidden" name="step" value="2" />
				<?php wp_nonce_field( 'wlc_algolia_wizard_save', 'wlc_algolia_wizard_nonce' ); ?>

				<div class="wlc-wizard-rows wlc-wizard-rows--pt">
					<?php foreach ( $wlc_algolia_post_types as $wlc_algolia_pt ) : ?>
						<?php
						$wlc_algolia_pt_slug        = sanitize_key( (string) $wlc_algolia_pt->name );
						$wlc_algolia_saved_checked  = in_array( $wlc_algolia_pt_slug, $wlc_algolia_selected_post_types, true )
							? true
							: ( '1' === (string) ( $wlc_algolia_post_type_index[ $wlc_algolia_pt_slug ] ?? '0' ) );
						$wlc_algolia_is_recommended = in_array( $wlc_algolia_pt_slug, $wlc_algolia_recommended_post_types, true );

						// If nothing saved yet, lean on recommendations.
						$wlc_algolia_has_any_saved = ! empty( $wlc_algolia_selected_post_types ) || array_search( '1', $wlc_algolia_post_type_index, true ) !== false;
						$wlc_algolia_checked       = $wlc_algolia_has_any_saved ? $wlc_algolia_saved_checked : $wlc_algolia_is_recommended;

						$wlc_algolia_count_obj = wp_count_posts( $wlc_algolia_pt_slug );
						$wlc_algolia_count     = 0;
						if ( is_object( $wlc_algolia_count_obj ) ) {
							$wlc_algolia_count  = (int) ( $wlc_algolia_count_obj->publish ?? 0 );
							$wlc_algolia_count += (int) ( $wlc_algolia_count_obj->private ?? 0 );
						}
						?>
						<div class="wlc-wizard-row">
							<div class="wlc-wizard-row-meta">
								<div class="wlc-wizard-row-title"><?php echo esc_html( $wlc_algolia_pt->label ); ?></div>
								<div class="wlc-wizard-row-sub">
									<?php echo esc_html( $wlc_algolia_pt_slug ); ?>
									<?php if ( $wlc_algolia_count > 0 ) : ?>
										<span class="wlc-wizard-badge"><?php echo esc_html( number_format_i18n( $wlc_algolia_count ) ); ?></span>
									<?php endif; ?>
									<?php if ( $wlc_algolia_is_recommended ) : ?>
										<span class="wlc-wizard-badge is-accent"><?php esc_html_e( 'Recommended', 'wlc-search-integration-with-algolia' ); ?></span>
									<?php endif; ?>
								</div>
							</div>

							<div class="wlc-wizard-row-actions">
								<?php /* translators: %s: post type label */ ?>
								<label class="wlc-algolia-toggle" aria-label="<?php echo esc_attr( sprintf( __( 'Enable %s', 'wlc-search-integration-with-algolia' ), $wlc_algolia_pt->label ) ); ?>">
									<input
										type="checkbox"
										name="post_types[]"
										value="<?php echo esc_attr( $wlc_algolia_pt_slug ); ?>"
										<?php checked( true, $wlc_algolia_checked ); ?>
										data-recommended="<?php echo $wlc_algolia_is_recommended ? '1' : '0'; ?>"
									/>
									<span class="wlc-algolia-toggle-slider"></span>
								</label>
							</div>
						</div>
					<?php endforeach; ?>
				</div>

				<div class="wlc-wizard-actions">
					<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=wlc-algolia&tab=wizard&step=1' ) ); ?>"><?php esc_html_e( 'Back', 'wlc-search-integration-with-algolia' ); ?></a>
					<button type="submit" class="button button-primary wlc-algolia-save-button"><?php esc_html_e( 'Save & continue', 'wlc-search-integration-with-algolia' ); ?></button>
				</div>
			</form>
		<?php endif; ?>

		<?php if ( 3 === $wlc_algolia_step ) : ?>
			<div class="wlc-wizard-section">
				<div class="wlc-wizard-section-title"><?php esc_html_e( 'Schema & facets', 'wlc-search-integration-with-algolia' ); ?></div>
				<div class="wlc-wizard-section-sub"><?php esc_html_e( 'Choose what fields to index and what fields should be facetable. Switch post type to see different field lists.', 'wlc-search-integration-with-algolia' ); ?></div>
			</div>

			<?php
			$wlc_algolia_state_primary    = $wlc_algolia_primary_post_type;
			$wlc_algolia_available_fields = \wlc_algolia\Admin_Wizard::get_available_fields_for_post_type( $wlc_algolia_state_primary );

			// Recommended sets by preset.
			$wlc_algolia_recommended_fields = array( 'ID', 'post_title', 'post_content', 'post_excerpt', 'post_date', 'post_permalink', 'images', 'taxonomies' );
			$wlc_algolia_recommended_facets = array( 'taxonomies' );
			if ( 'woo' === $wlc_algolia_preset || 'mixed' === $wlc_algolia_preset ) {
				if ( 'product' === $wlc_algolia_state_primary ) {
					$wlc_algolia_recommended_fields = array_merge( $wlc_algolia_recommended_fields, array( '_price', '_regular_price', '_sale_price', '_sku' ) );
					$wlc_algolia_recommended_facets = array( 'taxonomies', '_price' );
				}
			}

			// Clamp to available fields.
			$wlc_algolia_recommended_fields = array_values( array_intersect( $wlc_algolia_recommended_fields, $wlc_algolia_available_fields ) );
			$wlc_algolia_recommended_facets = array_values( array_intersect( $wlc_algolia_recommended_facets, $wlc_algolia_available_fields ) );
			?>

			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wlc-wizard-form">
				<input type="hidden" name="action" value="wlc_algolia_wizard_save" />
				<input type="hidden" name="step" value="3" />
				<?php wp_nonce_field( 'wlc_algolia_wizard_save', 'wlc_algolia_wizard_nonce' ); ?>

				<label class="wlc-wizard-field">
					<span class="wlc-wizard-field-label"><?php esc_html_e( 'Primary post type', 'wlc-search-integration-with-algolia' ); ?></span>
					<select name="primary_post_type" id="wlc-wizard-primary-post-type">
						<?php foreach ( $wlc_algolia_post_types as $wlc_algolia_pt ) : ?>
							<option value="<?php echo esc_attr( $wlc_algolia_pt->name ); ?>" <?php selected( $wlc_algolia_state_primary, $wlc_algolia_pt->name ); ?>>
								<?php echo esc_html( $wlc_algolia_pt->label ); ?>
							</option>
						<?php endforeach; ?>
					</select>
					<span class="wlc-wizard-field-help"><?php esc_html_e( 'Wizard writes CPT mapping for this post type. You can refine other post types later in CPT tab.', 'wlc-search-integration-with-algolia' ); ?></span>
				</label>

				<div class="wlc-wizard-toolbar">
					<label class="wlc-wizard-field" style="max-width: 420px;">
						<span class="wlc-wizard-field-label"><?php esc_html_e( 'Search fields', 'wlc-search-integration-with-algolia' ); ?></span>
						<input type="text" id="wlc-wizard-field-filter" placeholder="<?php esc_attr_e( 'Type to filter…', 'wlc-search-integration-with-algolia' ); ?>" />
					</label>
				</div>

				<div class="wlc-wizard-fieldlist">
					<div class="wlc-wizard-fieldlist-head">
						<div><?php esc_html_e( 'Field', 'wlc-search-integration-with-algolia' ); ?></div>
						<div><?php esc_html_e( 'Index', 'wlc-search-integration-with-algolia' ); ?></div>
						<div><?php esc_html_e( 'Facet', 'wlc-search-integration-with-algolia' ); ?></div>
					</div>
					<?php foreach ( $wlc_algolia_available_fields as $wlc_algolia_field ) : ?>
						<?php
						$wlc_algolia_is_index_rec = in_array( $wlc_algolia_field, $wlc_algolia_recommended_fields, true );
						$wlc_algolia_is_facet_rec = in_array( $wlc_algolia_field, $wlc_algolia_recommended_facets, true );
						?>
						<div class="wlc-wizard-fieldrow" data-field="<?php echo esc_attr( strtolower( (string) $wlc_algolia_field ) ); ?>">
							<div class="wlc-wizard-fieldrow-meta">
								<div class="wlc-wizard-fieldrow-title">
									<?php echo esc_html( $wlc_algolia_field ); ?>
									<?php if ( $wlc_algolia_is_index_rec ) : ?>
										<span class="wlc-wizard-badge is-accent"><?php esc_html_e( 'Recommended', 'wlc-search-integration-with-algolia' ); ?></span>
									<?php endif; ?>
								</div>
								<?php if ( 'taxonomies' === $wlc_algolia_field ) : ?>
									<div class="wlc-wizard-fieldrow-sub"><?php esc_html_e( 'Faceting expands to taxonomies.{taxonomy}.slug/name automatically.', 'wlc-search-integration-with-algolia' ); ?></div>
								<?php endif; ?>
							</div>

							<div class="wlc-wizard-fieldrow-toggle">
								<?php /* translators: %s: field name/slug */ ?>
								<label class="wlc-algolia-toggle" aria-label="<?php echo esc_attr( sprintf( __( 'Index field %s', 'wlc-search-integration-with-algolia' ), $wlc_algolia_field ) ); ?>">
									<input
										type="checkbox"
										name="index_fields[]"
										value="<?php echo esc_attr( $wlc_algolia_field ); ?>"
										<?php checked( true, $wlc_algolia_is_index_rec ); ?>
										data-wlc-field="<?php echo esc_attr( $wlc_algolia_field ); ?>"
										data-wlc-role="index"
									/>
									<span class="wlc-algolia-toggle-slider"></span>
								</label>
							</div>

							<div class="wlc-wizard-fieldrow-toggle">
								<?php /* translators: %s: field name/slug */ ?>
								<label class="wlc-algolia-toggle" aria-label="<?php echo esc_attr( sprintf( __( 'Facet field %s', 'wlc-search-integration-with-algolia' ), $wlc_algolia_field ) ); ?>">
									<input
										type="checkbox"
										name="facet_fields[]"
										value="<?php echo esc_attr( $wlc_algolia_field ); ?>"
										<?php checked( true, $wlc_algolia_is_facet_rec ); ?>
										data-wlc-field="<?php echo esc_attr( $wlc_algolia_field ); ?>"
										data-wlc-role="facet"
									/>
									<span class="wlc-algolia-toggle-slider"></span>
								</label>
							</div>
						</div>
					<?php endforeach; ?>
				</div>

				<div class="wlc-wizard-actions">
					<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=wlc-algolia&tab=wizard&step=2' ) ); ?>"><?php esc_html_e( 'Back', 'wlc-search-integration-with-algolia' ); ?></a>
					<button type="submit" class="button button-primary wlc-algolia-save-button"><?php esc_html_e( 'Save & continue', 'wlc-search-integration-with-algolia' ); ?></button>
				</div>
			</form>
		<?php endif; ?>

		<?php if ( 4 === $wlc_algolia_step ) : ?>
			<p class="wlc-wizard-lead"><?php esc_html_e( 'Enable autocomplete and choose which post types appear in suggestions.', 'wlc-search-integration-with-algolia' ); ?></p>

			<?php
			$wlc_algolia_enabled = (string) ( $wlc_algolia_autocomplete['enable-autocomplete'] ?? '0' );
			?>

			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wlc-wizard-form">
				<input type="hidden" name="action" value="wlc_algolia_wizard_save" />
				<input type="hidden" name="step" value="4" />
				<?php wp_nonce_field( 'wlc_algolia_wizard_save', 'wlc_algolia_wizard_nonce' ); ?>

				<label class="wlc-wizard-field">
					<span class="wlc-wizard-field-label"><?php esc_html_e( 'Enable Autocomplete', 'wlc-search-integration-with-algolia' ); ?></span>
					<select name="enable_autocomplete">
						<option value="1" <?php selected( '1', $wlc_algolia_enabled ); ?>><?php esc_html_e( 'Enabled', 'wlc-search-integration-with-algolia' ); ?></option>
						<option value="0" <?php selected( '0', $wlc_algolia_enabled ); ?>><?php esc_html_e( 'Disabled', 'wlc-search-integration-with-algolia' ); ?></option>
					</select>
				</label>

				<label class="wlc-wizard-field">
					<span class="wlc-wizard-field-label"><?php esc_html_e( 'Max suggestions per post type', 'wlc-search-integration-with-algolia' ); ?></span>
					<input type="number" min="0" name="max_suggestions" value="10" />
				</label>

				<div class="wlc-wizard-rows wlc-wizard-rows--ac">
					<?php foreach ( $wlc_algolia_post_types as $wlc_algolia_pt ) : ?>
						<?php
						$wlc_algolia_pt_slug = sanitize_key( (string) $wlc_algolia_pt->name );
						$wlc_algolia_checked = isset( $wlc_algolia_autocomplete[ $wlc_algolia_pt_slug ]['enable'] ) && '1' === (string) $wlc_algolia_autocomplete[ $wlc_algolia_pt_slug ]['enable'];
						if ( empty( $wlc_algolia_autocomplete ) && in_array( $wlc_algolia_pt_slug, $wlc_algolia_selected_post_types, true ) ) {
							$wlc_algolia_checked = true;
						}
						?>
						<div class="wlc-wizard-row">
							<div class="wlc-wizard-row-meta">
								<div class="wlc-wizard-row-title"><?php echo esc_html( $wlc_algolia_pt->label ); ?></div>
								<div class="wlc-wizard-row-sub"><?php echo esc_html( $wlc_algolia_pt_slug ); ?></div>
							</div>
							<div class="wlc-wizard-row-actions">
								<?php /* translators: %s: post type label */ ?>
								<label class="wlc-algolia-toggle" aria-label="<?php echo esc_attr( sprintf( __( 'Enable autocomplete for %s', 'wlc-search-integration-with-algolia' ), $wlc_algolia_pt->label ) ); ?>">
									<input type="checkbox" name="autocomplete_post_types[]" value="<?php echo esc_attr( $wlc_algolia_pt_slug ); ?>" <?php checked( true, $wlc_algolia_checked ); ?> />
									<span class="wlc-algolia-toggle-slider"></span>
								</label>
							</div>
						</div>
					<?php endforeach; ?>
				</div>

				<div class="wlc-wizard-actions">
					<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=wlc-algolia&tab=wizard&step=3' ) ); ?>"><?php esc_html_e( 'Back', 'wlc-search-integration-with-algolia' ); ?></a>
					<button type="submit" class="button button-primary wlc-algolia-save-button"><?php esc_html_e( 'Save & continue', 'wlc-search-integration-with-algolia' ); ?></button>
				</div>
			</form>
		<?php endif; ?>

		<?php if ( 5 === $wlc_algolia_step ) : ?>
			<div class="wlc-wizard-section">
				<div class="wlc-wizard-section-title"><?php esc_html_e( 'Index now (recommended)', 'wlc-search-integration-with-algolia' ); ?></div>
				<div class="wlc-wizard-section-sub"><?php esc_html_e( 'This runs in batches and does not block the UI. You can close the page and come back.', 'wlc-search-integration-with-algolia' ); ?></div>
			</div>

			<div class="index-error-message"><p class="post-type-index-error-message"></p></div>
			<div class="index-success-message"><p class="post-type-index-success-message"></p></div>

			<?php
			// Determine enabled post types from saved options (fallback to wizard state).
			$wlc_algolia_enabled_pts = array();
			if ( is_array( $wlc_algolia_post_type_index ) ) {
				foreach ( $wlc_algolia_post_type_index as $wlc_algolia_k => $wlc_algolia_v ) {
					$wlc_algolia_key = sanitize_key( (string) $wlc_algolia_k );
					if ( '' === $wlc_algolia_key || str_contains( $wlc_algolia_key, '_autosuggestion' ) || str_contains( $wlc_algolia_key, '_custom_index' ) ) {
						continue;
					}
					if ( '1' === (string) $wlc_algolia_v ) {
						$wlc_algolia_enabled_pts[] = $wlc_algolia_key;
					}
				}
			}
			if ( empty( $wlc_algolia_enabled_pts ) ) {
				$wlc_algolia_enabled_pts = $wlc_algolia_selected_post_types;
			}
			$wlc_algolia_enabled_pts = array_values( array_unique( array_filter( $wlc_algolia_enabled_pts, 'post_type_exists' ) ) );
			?>

			<div class="wlc-wizard-index">
				<div class="wlc-wizard-toolbar">
					<button type="button" class="button button-primary wlc-algolia-save-button" id="wlc-wizard-index-all" <?php echo empty( $wlc_algolia_enabled_pts ) ? 'disabled' : ''; ?>>
						<?php esc_html_e( 'Index all selected', 'wlc-search-integration-with-algolia' ); ?>
					</button>
					<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=wlc-algolia&tab=post_type_index' ) ); ?>">
						<?php esc_html_e( 'Open full indexing tab', 'wlc-search-integration-with-algolia' ); ?>
					</a>
				</div>

				<?php if ( empty( $wlc_algolia_enabled_pts ) ) : ?>
					<div class="wlc-wizard-card">
						<?php esc_html_e( 'No post types selected. Go back to Step 2 and enable at least one post type.', 'wlc-search-integration-with-algolia' ); ?>
					</div>
				<?php else : ?>
					<div class="wlc-wizard-index-list">
						<?php foreach ( $wlc_algolia_enabled_pts as $wlc_algolia_pt_slug ) : ?>
							<?php
							$wlc_algolia_pt_obj     = get_post_type_object( $wlc_algolia_pt_slug );
							$wlc_algolia_label      = $wlc_algolia_pt_obj ? $wlc_algolia_pt_obj->label : $wlc_algolia_pt_slug;
							$wlc_algolia_index_name = ( '' !== $wlc_algolia_base_index_prefix ) ? ( $wlc_algolia_base_index_prefix . '_' . $wlc_algolia_pt_slug ) : '';
							?>
							<div class="wlc-wizard-index-row">
								<div class="wlc-wizard-index-meta">
									<div class="wlc-wizard-index-title"><?php echo esc_html( $wlc_algolia_label ); ?></div>
									<div class="wlc-wizard-index-sub"><?php echo esc_html( $wlc_algolia_index_name ); ?></div>
								</div>

								<!-- Checkbox required by existing indexing JS: it checks $('.'+post_type). -->
								<input type="checkbox" class="<?php echo esc_attr( $wlc_algolia_pt_slug ); ?>" checked style="display:none" />

								<div class="wlc-post-type-progress">
									<div class="wlc-progress-bar-container">
										<div class="wlc-progress-bar progress-bar-<?php echo esc_attr( $wlc_algolia_pt_slug ); ?>" style="width:0%"></div>
									</div>
									<div class="wlc-progress-text progress-text-<?php echo esc_attr( $wlc_algolia_pt_slug ); ?>">0%</div>
								</div>

								<button
									type="button"
									class="button index-post-type"
									data-post-type="<?php echo esc_attr( $wlc_algolia_pt_slug ); ?>"
									data-index-name="<?php echo esc_attr( $wlc_algolia_index_name ); ?>"
								>
									<?php esc_html_e( 'Start', 'wlc-search-integration-with-algolia' ); ?>
								</button>
							</div>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</div>

			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wlc-wizard-form">
				<input type="hidden" name="action" value="wlc_algolia_wizard_save" />
				<input type="hidden" name="step" value="5" />
				<?php wp_nonce_field( 'wlc_algolia_wizard_save', 'wlc_algolia_wizard_nonce' ); ?>
				<div class="wlc-wizard-actions">
					<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=wlc-algolia&tab=wizard&step=4' ) ); ?>"><?php esc_html_e( 'Back', 'wlc-search-integration-with-algolia' ); ?></a>
					<button type="submit" class="button button-primary wlc-algolia-save-button"><?php esc_html_e( 'Finish wizard', 'wlc-search-integration-with-algolia' ); ?></button>
				</div>
			</form>
		<?php endif; ?>
		</section>
	</div>
</div>
	<?php
} )( get_defined_vars() );
