<?php
/**
 * Internal Links Management Page Template
 *
 * @package ContentPerformanceAnalyzer
 * @since 1.0.0
 */

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

$status                     = get_option( 'cpanalyzer_link_analysis_status', array() );
$cpanalyzer_suggestions     = get_option( 'cpanalyzer_internal_link_suggestions', array() );
$cpanalyzer_is_analyzing    = isset( $status['status'] ) && 'analyzing' === $status['status'];
$cpanalyzer_has_suggestions = ! empty( $cpanalyzer_suggestions );
$cpanalyzer_ai_configured   = false;
$cpanalyzer_ai_error_message = '';

if ( cpanalyzer_ai_is_configured() ) {
	try {
		if ( class_exists( 'CPAP_AI_Processor' ) ) {
			$cpanalyzer_ai_processor  = new CPAP_AI_Processor();
			$cpanalyzer_ai_configured = method_exists( $cpanalyzer_ai_processor, 'is_configured' ) ? $cpanalyzer_ai_processor->is_configured() : true;
		} else {
			$cpanalyzer_ai_configured = true;
		}
	} catch ( Exception $e ) {
		$cpanalyzer_ai_error_message = 'Error checking AI configuration: ' . $e->getMessage();
		$cpanalyzer_ai_configured    = false;
	}
}

$cpanalyzer_content_count = 0;
if ( $cpanalyzer_ai_configured && class_exists( 'CPAP_Internal_Links' ) ) {
	try {
		$cpanalyzer_internal_links    = new CPAP_Internal_Links();
		$cpanalyzer_published_content = $cpanalyzer_internal_links->get_published_content();
		$cpanalyzer_content_count     = count( $cpanalyzer_published_content );
	} catch ( Exception $e ) {
		$cpanalyzer_content_count = 0;
	}
}

$cpanalyzer_estimated_minutes   = $cpanalyzer_content_count;
$cpanalyzer_estimated_time_text = '';
if ( $cpanalyzer_estimated_minutes > 0 ) {
	if ( $cpanalyzer_estimated_minutes < 60 ) {
		$cpanalyzer_estimated_time_text = sprintf( __( '%d minutes', 'content-performance-analyzer' ), $cpanalyzer_estimated_minutes );
	} else {
		$cpanalyzer_hours             = floor( $cpanalyzer_estimated_minutes / 60 );
		$cpanalyzer_remaining_minutes = $cpanalyzer_estimated_minutes % 60;
		if ( $cpanalyzer_remaining_minutes > 0 ) {
			$cpanalyzer_estimated_time_text = sprintf( __( '%1$d hour(s) and %2$d minutes', 'content-performance-analyzer' ), $cpanalyzer_hours, $cpanalyzer_remaining_minutes );
		} else {
			$cpanalyzer_estimated_time_text = sprintf( __( '%1$d hour(s)', 'content-performance-analyzer' ), $cpanalyzer_hours );
		}
	}
}

$cpanalyzer_total_suggestions = 0;
$cpanalyzer_posts_with_suggestions = 0;
foreach ( $cpanalyzer_suggestions as $cpanalyzer_post_suggestions ) {
	if ( isset( $cpanalyzer_post_suggestions['suggestions'] ) && is_array( $cpanalyzer_post_suggestions['suggestions'] ) && ! empty( $cpanalyzer_post_suggestions['suggestions'] ) ) {
		$cpanalyzer_total_suggestions += count( $cpanalyzer_post_suggestions['suggestions'] );
		++$cpanalyzer_posts_with_suggestions;
	}
}

$cpanalyzer_status_value = $status['status'] ?? 'idle';
$cpanalyzer_status_label = __( 'Ready', 'content-performance-analyzer' );
if ( 'analyzing' === $cpanalyzer_status_value ) {
	$cpanalyzer_status_label = __( 'Running', 'content-performance-analyzer' );
} elseif ( 'completed' === $cpanalyzer_status_value ) {
	$cpanalyzer_status_label = __( 'Completed', 'content-performance-analyzer' );
} elseif ( 'error' === $cpanalyzer_status_value ) {
	$cpanalyzer_status_label = __( 'Needs attention', 'content-performance-analyzer' );
}

$cpanalyzer_completed_label = '';
if ( ! empty( $status['completed_at'] ) ) {
	$cpanalyzer_completed_label = date_i18n(
		get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
		strtotime( $status['completed_at'] )
	);
}

$cpanalyzer_progress_total   = max( 1, (int) ( $status['total'] ?? 0 ) );
$cpanalyzer_progress_current = (int) ( $status['processed'] ?? 0 );
$cpanalyzer_progress_percent = min( 100, max( 0, (int) round( ( $cpanalyzer_progress_current / $cpanalyzer_progress_total ) * 100 ) ) );
?>

<div class="wrap cpanalyzer-il-page">
	<div class="cpanalyzer-il-hero">
		<div class="cpanalyzer-il-hero-copy">
			<h1><?php esc_html_e( 'Internal Linking', 'content-performance-analyzer' ); ?></h1>
			<p class="cpanalyzer-il-subtitle">
				<?php esc_html_e( 'Review sitewide internal-link opportunities from one place. The analyzer scans your published content, proposes high-value connections, and keeps the final decision in editorial review.', 'content-performance-analyzer' ); ?>
			</p>
		</div>

		<div class="cpanalyzer-il-guardrails">
			<h2><?php esc_html_e( 'Safety Guardrails', 'content-performance-analyzer' ); ?></h2>
			<ul>
				<li><?php esc_html_e( 'Only published content is analyzed and suggested as a target.', 'content-performance-analyzer' ); ?></li>
				<li><?php esc_html_e( 'Anchor text and context must exist in the paragraph-level source content before a suggestion is shown.', 'content-performance-analyzer' ); ?></li>
				<li><?php esc_html_e( 'Generic anchors, duplicate suggestions, and weak target matches are rejected before review.', 'content-performance-analyzer' ); ?></li>
				<li><?php esc_html_e( 'Editors still review or remove each suggestion before it is applied.', 'content-performance-analyzer' ); ?></li>
			</ul>
		</div>
	</div>

	<?php if ( ! empty( $cpanalyzer_ai_error_message ) ) : ?>
		<div class="notice notice-error">
			<p>
				<strong><?php esc_html_e( 'Error:', 'content-performance-analyzer' ); ?></strong>
				<?php echo esc_html( $cpanalyzer_ai_error_message ); ?>
			</p>
		</div>
	<?php elseif ( ! $cpanalyzer_ai_configured ) : ?>
		<div class="notice notice-error">
			<p>
				<?php esc_html_e( 'OpenAI API key is required for internal link analysis.', 'content-performance-analyzer' ); ?>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=cpanalyzer-settings' ) ); ?>">
					<?php esc_html_e( 'Configure it in Settings', 'content-performance-analyzer' ); ?>
				</a>
			</p>
		</div>
	<?php endif; ?>

	<div class="cpanalyzer-il-overview-grid">
		<div class="cpanalyzer-il-stat-card">
			<span class="cpanalyzer-il-stat-label"><?php esc_html_e( 'AI readiness', 'content-performance-analyzer' ); ?></span>
			<strong class="cpanalyzer-il-stat-value <?php echo $cpanalyzer_ai_configured ? 'is-good' : 'is-warning'; ?>">
				<?php echo esc_html( $cpanalyzer_ai_configured ? __( 'Configured', 'content-performance-analyzer' ) : __( 'Needs setup', 'content-performance-analyzer' ) ); ?>
			</strong>
			<span class="cpanalyzer-il-stat-meta">
				<?php echo esc_html( $cpanalyzer_ai_configured ? __( 'Ready to generate suggestions', 'content-performance-analyzer' ) : __( 'Set an AI key before running analysis', 'content-performance-analyzer' ) ); ?>
			</span>
		</div>

		<div class="cpanalyzer-il-stat-card">
			<span class="cpanalyzer-il-stat-label"><?php esc_html_e( 'Published items', 'content-performance-analyzer' ); ?></span>
			<strong class="cpanalyzer-il-stat-value"><?php echo esc_html( number_format_i18n( $cpanalyzer_content_count ) ); ?></strong>
			<span class="cpanalyzer-il-stat-meta"><?php esc_html_e( 'Eligible posts, pages, and public post types', 'content-performance-analyzer' ); ?></span>
		</div>

		<div class="cpanalyzer-il-stat-card">
			<span class="cpanalyzer-il-stat-label"><?php esc_html_e( 'Estimated runtime', 'content-performance-analyzer' ); ?></span>
			<strong class="cpanalyzer-il-stat-value"><?php echo esc_html( $cpanalyzer_estimated_time_text ? $cpanalyzer_estimated_time_text : __( 'Not available', 'content-performance-analyzer' ) ); ?></strong>
			<span class="cpanalyzer-il-stat-meta"><?php esc_html_e( 'Runs in the background; you can leave this page', 'content-performance-analyzer' ); ?></span>
		</div>

		<div class="cpanalyzer-il-stat-card">
			<span class="cpanalyzer-il-stat-label"><?php esc_html_e( 'Current results', 'content-performance-analyzer' ); ?></span>
			<strong id="cpanalyzer-total-suggestions" class="cpanalyzer-il-stat-value"><?php echo esc_html( number_format_i18n( $cpanalyzer_total_suggestions ) ); ?></strong>
			<span class="cpanalyzer-il-stat-meta">
				<?php
				printf(
					esc_html__( '%1$s source pages with suggestions • %2$s', 'content-performance-analyzer' ),
					esc_html( number_format_i18n( $cpanalyzer_posts_with_suggestions ) ),
					esc_html( $cpanalyzer_status_label )
				);
				?>
			</span>
		</div>
	</div>

	<?php if ( $cpanalyzer_ai_configured ) : ?>
		<section class="cpanalyzer-il-panel cpanalyzer-il-workflow">
			<div class="cpanalyzer-il-panel-header">
				<div>
					<h2><?php esc_html_e( 'Run sitewide analysis', 'content-performance-analyzer' ); ?></h2>
					<p>
						<?php esc_html_e( 'Start with a quick test run to validate quality on a few posts, then run the full sitewide analysis when you are comfortable with the output.', 'content-performance-analyzer' ); ?>
					</p>
				</div>
				<div class="cpanalyzer-il-status-pill status-<?php echo esc_attr( sanitize_html_class( $cpanalyzer_status_value ) ); ?>">
					<?php echo esc_html( $cpanalyzer_status_label ); ?>
				</div>
			</div>

			<div class="cpanalyzer-il-action-row">
				<button id="cpanalyzer-analyze-links" class="button button-primary button-hero" <?php disabled( $cpanalyzer_is_analyzing ); ?>>
					<span class="dashicons dashicons-analytics"></span>
					<?php esc_html_e( 'Analyze Internal Links', 'content-performance-analyzer' ); ?>
				</button>
				<button id="cpanalyzer-manual-trigger" class="button button-secondary" <?php disabled( $cpanalyzer_is_analyzing ); ?>>
					<span class="dashicons dashicons-hammer"></span>
					<?php esc_html_e( 'Test Run (3 Posts)', 'content-performance-analyzer' ); ?>
				</button>
			</div>

			<p class="cpanalyzer-il-helper">
				<?php esc_html_e( 'Test Run checks quality quickly. Full Analysis queues the entire published library and saves grouped suggestions for review.', 'content-performance-analyzer' ); ?>
			</p>

			<div id="cpanalyzer-analysis-status" class="cpanalyzer-il-inline-status"></div>

			<details class="cpanalyzer-il-troubleshooting">
				<summary><?php esc_html_e( 'Troubleshooting tools', 'content-performance-analyzer' ); ?></summary>
				<div class="cpanalyzer-il-troubleshooting-body">
					<p><?php esc_html_e( 'Use these only if the workflow looks stuck or WordPress cron is not progressing as expected.', 'content-performance-analyzer' ); ?></p>
					<div class="cpanalyzer-il-troubleshooting-actions">
						<button id="cpanalyzer-check-cron" class="button button-small">
							<span class="dashicons dashicons-clock"></span>
							<?php esc_html_e( 'Check Cron Status', 'content-performance-analyzer' ); ?>
						</button>
						<button id="cpanalyzer-reset-analysis" class="button button-small">
							<span class="dashicons dashicons-dismiss"></span>
							<?php esc_html_e( 'Reset Analysis', 'content-performance-analyzer' ); ?>
						</button>
					</div>
					<div id="cpanalyzer-debug-output" class="cpanalyzer-il-debug-output" hidden></div>
				</div>
			</details>
		</section>
	<?php endif; ?>

	<?php if ( $cpanalyzer_ai_configured ) : ?>
		<section id="cpanalyzer-analysis-progress" class="cpanalyzer-il-panel cpanalyzer-il-progress-panel"<?php echo $cpanalyzer_is_analyzing ? '' : ' hidden'; ?>>
			<div class="cpanalyzer-il-progress-head">
				<div>
					<h2><?php esc_html_e( 'Analysis in progress', 'content-performance-analyzer' ); ?></h2>
					<p><?php esc_html_e( 'The analyzer is processing published content in the background. The page will refresh automatically when the run completes.', 'content-performance-analyzer' ); ?></p>
				</div>
				<strong id="cpanalyzer-progress-percent"><?php echo esc_html( $cpanalyzer_progress_percent ); ?>%</strong>
			</div>
			<div class="cpanalyzer-il-progress-bar">
				<span id="cpanalyzer-progress-bar-fill" style="width: <?php echo esc_attr( $cpanalyzer_progress_percent ); ?>%;"></span>
			</div>
			<div class="cpanalyzer-il-progress-meta">
				<span id="cpanalyzer-progress-text">
					<?php
					printf(
						esc_html__( 'Progress: %1$s of %2$s items processed', 'content-performance-analyzer' ),
						esc_html( number_format_i18n( $cpanalyzer_progress_current ) ),
						esc_html( number_format_i18n( (int) ( $status['total'] ?? 0 ) ) )
					);
					?>
				</span>
				<span id="cpanalyzer-time-remaining">
					<?php
					$cpanalyzer_remaining = max( 0, (int) ( $status['total'] ?? 0 ) - $cpanalyzer_progress_current );
					printf(
						esc_html__( 'Estimated time remaining: %s minutes', 'content-performance-analyzer' ),
						esc_html( number_format_i18n( $cpanalyzer_remaining ) )
					);
					?>
				</span>
			</div>
			<div id="cpanalyzer-overtime-message" class="cpanalyzer-il-overtime" hidden>
				<strong><?php esc_html_e( 'Taking longer than expected?', 'content-performance-analyzer' ); ?></strong>
				<span><?php esc_html_e( 'Large libraries can take extra time. The process is still safe to leave running in the background.', 'content-performance-analyzer' ); ?></span>
			</div>
		</section>
	<?php endif; ?>

	<?php if ( $cpanalyzer_has_suggestions && ! $cpanalyzer_is_analyzing ) : ?>
		<section class="cpanalyzer-il-panel cpanalyzer-il-results">
			<div class="cpanalyzer-il-panel-header">
				<div>
					<h2><?php esc_html_e( 'Review opportunities', 'content-performance-analyzer' ); ?></h2>
					<p><?php esc_html_e( 'Suggestions are grouped by source page so editors can review context before applying or removing each recommendation.', 'content-performance-analyzer' ); ?></p>
				</div>
				<div class="cpanalyzer-il-results-meta">
					<strong id="cpanalyzer-posts-with-suggestions"><?php echo esc_html( number_format_i18n( $cpanalyzer_posts_with_suggestions ) ); ?></strong>
					<span><?php esc_html_e( 'source pages with suggestions', 'content-performance-analyzer' ); ?></span>
					<?php if ( $cpanalyzer_completed_label ) : ?>
						<small>
							<?php
							printf(
								esc_html__( 'Last run: %s', 'content-performance-analyzer' ),
								esc_html( $cpanalyzer_completed_label )
							);
							?>
						</small>
					<?php endif; ?>
				</div>
			</div>

			<div class="cpanalyzer-il-review-note">
				<strong><?php esc_html_e( 'Review before applying:', 'content-performance-analyzer' ); ?></strong>
				<?php esc_html_e( 'Only keep suggestions whose context sentence matches the real paragraph flow of the source page.', 'content-performance-analyzer' ); ?>
			</div>

			<div class="cpanalyzer-il-results-toolbar">
				<label for="cpanalyzer-suggestions-search" class="screen-reader-text"><?php esc_html_e( 'Search suggestions', 'content-performance-analyzer' ); ?></label>
				<input type="search" id="cpanalyzer-suggestions-search" class="regular-text" placeholder="<?php esc_attr_e( 'Filter by source, target, anchor, or context...', 'content-performance-analyzer' ); ?>">
			</div>

			<?php wp_nonce_field( CPAP_Internal_Links::NONCE_ACTION, CPAP_Internal_Links::NONCE_NAME ); ?>

			<div class="cpanalyzer-suggestions-list">
				<?php foreach ( $cpanalyzer_suggestions as $post_id => $cpanalyzer_post_data ) : ?>
					<?php
					if ( ! isset( $cpanalyzer_post_data['source_post'], $cpanalyzer_post_data['suggestions'] ) || ! is_array( $cpanalyzer_post_data['suggestions'] ) || empty( $cpanalyzer_post_data['suggestions'] ) ) {
						continue;
					}

					$cpanalyzer_source_post      = $cpanalyzer_post_data['source_post'];
					$cpanalyzer_post_suggestions = $cpanalyzer_post_data['suggestions'];
					$cpanalyzer_source_title     = $cpanalyzer_source_post['title'] ?? __( 'Untitled', 'content-performance-analyzer' );
					$cpanalyzer_source_type      = ucfirst( $cpanalyzer_source_post['type'] ?? 'post' );
					?>
					<article class="cpanalyzer-post-suggestions" data-source-title="<?php echo esc_attr( $cpanalyzer_source_title ); ?>">
						<header class="cpanalyzer-post-header">
							<div>
								<h3>
									<?php if ( ! empty( $cpanalyzer_source_post['url'] ) ) : ?>
										<a href="<?php echo esc_url( $cpanalyzer_source_post['url'] ); ?>" target="_blank" rel="noopener noreferrer">
											<?php echo esc_html( $cpanalyzer_source_title ); ?>
										</a>
									<?php else : ?>
										<?php echo esc_html( $cpanalyzer_source_title ); ?>
									<?php endif; ?>
								</h3>
								<div class="cpanalyzer-post-meta">
									<span class="cpanalyzer-il-badge"><?php echo esc_html( $cpanalyzer_source_type ); ?></span>
									<span>
										<?php
										printf(
											esc_html__( '%s opportunities', 'content-performance-analyzer' ),
											esc_html( number_format_i18n( count( $cpanalyzer_post_suggestions ) ) )
										);
										?>
									</span>
								</div>
							</div>
							<?php if ( ! empty( $cpanalyzer_source_post['url'] ) ) : ?>
								<a class="button button-secondary" href="<?php echo esc_url( $cpanalyzer_source_post['url'] ); ?>" target="_blank" rel="noopener noreferrer">
									<?php esc_html_e( 'Open source page', 'content-performance-analyzer' ); ?>
								</a>
							<?php endif; ?>
						</header>

						<div class="cpanalyzer-suggestions-table">
							<table class="wp-list-table widefat striped">
								<thead>
									<tr>
										<th><?php esc_html_e( 'Target page', 'content-performance-analyzer' ); ?></th>
										<th><?php esc_html_e( 'Anchor', 'content-performance-analyzer' ); ?></th>
										<th><?php esc_html_e( 'Context', 'content-performance-analyzer' ); ?></th>
										<th><?php esc_html_e( 'Why it fits', 'content-performance-analyzer' ); ?></th>
										<th><?php esc_html_e( 'Actions', 'content-performance-analyzer' ); ?></th>
									</tr>
								</thead>
								<tbody>
									<?php foreach ( $cpanalyzer_post_suggestions as $cpanalyzer_suggestion ) : ?>
										<?php
										if ( ! is_array( $cpanalyzer_suggestion ) ) {
											continue;
										}

										$cpanalyzer_anchor_text = $cpanalyzer_suggestion['anchor_text'] ?? '';
										$cpanalyzer_context     = $cpanalyzer_suggestion['context'] ?? '';
										$cpanalyzer_search_text = strtolower(
											implode(
												' ',
												array(
													$cpanalyzer_source_title,
													$cpanalyzer_suggestion['target_title'] ?? '',
													$cpanalyzer_anchor_text,
													$cpanalyzer_context,
													$cpanalyzer_suggestion['reason'] ?? '',
												)
											)
										);
										?>
										<tr class="cpanalyzer-suggestion-row"
											data-source-id="<?php echo esc_attr( $post_id ); ?>"
											data-target-id="<?php echo esc_attr( $cpanalyzer_suggestion['target_id'] ?? '' ); ?>"
											data-anchor-text="<?php echo esc_attr( $cpanalyzer_anchor_text ); ?>"
											data-context="<?php echo esc_attr( $cpanalyzer_context ); ?>"
											data-search-text="<?php echo esc_attr( $cpanalyzer_search_text ); ?>">
											<td>
												<strong><?php echo esc_html( $cpanalyzer_suggestion['target_title'] ?? __( 'Unknown target', 'content-performance-analyzer' ) ); ?></strong>
												<div class="cpanalyzer-target-meta">
													<?php if ( ! empty( $cpanalyzer_suggestion['target_type'] ) ) : ?>
														<span class="cpanalyzer-il-badge subtle"><?php echo esc_html( ucfirst( $cpanalyzer_suggestion['target_type'] ) ); ?></span>
													<?php endif; ?>
													<?php if ( ! empty( $cpanalyzer_suggestion['target_url'] ) ) : ?>
														<a href="<?php echo esc_url( $cpanalyzer_suggestion['target_url'] ); ?>" target="_blank" rel="noopener noreferrer">
															<?php esc_html_e( 'Open target', 'content-performance-analyzer' ); ?>
														</a>
													<?php endif; ?>
												</div>
											</td>
											<td>
												<span class="cpanalyzer-anchor-chip"><?php echo esc_html( $cpanalyzer_anchor_text ); ?></span>
											</td>
											<td>
												<div class="cpanalyzer-context-text">
													<?php
													if ( ! empty( $cpanalyzer_context ) && ! empty( $cpanalyzer_anchor_text ) ) {
														$cpanalyzer_highlighted_context = str_ireplace(
															$cpanalyzer_anchor_text,
															'<mark>' . esc_html( $cpanalyzer_anchor_text ) . '</mark>',
															esc_html( $cpanalyzer_context )
														);
														echo wp_kses( $cpanalyzer_highlighted_context, array( 'mark' => array() ) );
													} else {
														echo esc_html( $cpanalyzer_context );
													}
													?>
												</div>
											</td>
											<td>
												<div class="cpanalyzer-reason-text"><?php echo esc_html( $cpanalyzer_suggestion['reason'] ?? '' ); ?></div>
											</td>
											<td>
												<div class="cpanalyzer-action-stack">
													<button type="button" class="button button-primary button-small cpanalyzer-apply-one">
														<?php esc_html_e( 'Apply', 'content-performance-analyzer' ); ?>
													</button>
													<button type="button" class="button button-secondary button-small cpanalyzer-remove-one">
														<?php esc_html_e( 'Remove', 'content-performance-analyzer' ); ?>
													</button>
												</div>
											</td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					</article>
				<?php endforeach; ?>
			</div>
		</section>
	<?php elseif ( ! $cpanalyzer_is_analyzing && $cpanalyzer_ai_configured ) : ?>
		<section class="cpanalyzer-il-panel cpanalyzer-il-empty-state">
			<h2><?php esc_html_e( 'No analysis results yet', 'content-performance-analyzer' ); ?></h2>
			<p><?php esc_html_e( 'Start with a test run if you want to validate output quickly, or run the full analysis to generate grouped internal-link opportunities for the site.', 'content-performance-analyzer' ); ?></p>
		</section>
	<?php endif; ?>

	<div id="cpanalyzer-links-status-message" class="cpanalyzer-il-message-slot"></div>
</div>

<?php if ( $cpanalyzer_ai_configured ) : ?>
	<?php
	wp_add_inline_script(
		'cpanalyzer-internal-links-analysis',
		'var CPANALYZER_INTERNAL_LINKS_ANALYSIS = ' . wp_json_encode(
			array(
				'ajaxurl'           => admin_url( 'admin-ajax.php' ),
				'nonce'             => wp_create_nonce( 'cpanalyzer_internal_links' ),
				'isAnalyzing'       => $cpanalyzer_is_analyzing,
				'estimatedDuration' => (int) ( $cpanalyzer_estimated_minutes * 60 ),
				'processedCount'    => isset( $status['processed'] ) ? (int) $status['processed'] : 0,
				'hasEstimate'       => ( $cpanalyzer_content_count > 0 && ! empty( $cpanalyzer_estimated_time_text ) ),
				'strings'           => array(
					'confirmStart'      => __( 'Start the full sitewide internal-link analysis?', 'content-performance-analyzer' ),
					'confirmTest'       => __( 'Run a quick 3-post test analysis now?', 'content-performance-analyzer' ),
					'confirmReset'      => __( 'Reset the current analysis state and clear saved suggestions?', 'content-performance-analyzer' ),
					'foundItems'        => sprintf( __( 'Found %d items to analyze.', 'content-performance-analyzer' ), $cpanalyzer_content_count ),
					'estimatedTime'     => sprintf( __( 'Estimated time: %s', 'content-performance-analyzer' ), $cpanalyzer_estimated_time_text ),
					'runsInBackground'  => __( 'The analysis runs in the background. You can leave this page and return later.', 'content-performance-analyzer' ),
					'mayTakeTime'       => __( 'This may take several minutes depending on the amount of content.', 'content-performance-analyzer' ),
					'startingAnalysis'  => __( 'Starting analysis...', 'content-performance-analyzer' ),
					'startingTest'      => __( 'Running test analysis...', 'content-performance-analyzer' ),
					'preparingContent'  => __( 'Preparing content for analysis...', 'content-performance-analyzer' ),
					'analyzeButton'     => __( 'Analyze Internal Links', 'content-performance-analyzer' ),
					'testButton'        => __( 'Test Run (3 Posts)', 'content-performance-analyzer' ),
					'errorStarting'     => __( 'Error starting analysis. Please try again.', 'content-performance-analyzer' ),
					'progress'          => __( 'Progress:', 'content-performance-analyzer' ),
					'of'                => __( 'of', 'content-performance-analyzer' ),
					'itemsProcessed'    => __( 'items processed', 'content-performance-analyzer' ),
					'timeRemaining'     => __( 'Estimated time remaining:', 'content-performance-analyzer' ),
					'minutes'           => __( 'minutes', 'content-performance-analyzer' ),
					'almostFinished'    => __( 'Almost finished...', 'content-performance-analyzer' ),
					'checkingCron'      => __( 'Checking cron status...', 'content-performance-analyzer' ),
					'resetting'         => __( 'Resetting analysis...', 'content-performance-analyzer' ),
					'resetSuccess'      => __( 'Analysis reset successfully. Reloading…', 'content-performance-analyzer' ),
					'cronStatus'        => __( 'Cron status', 'content-performance-analyzer' ),
				),
			)
		) . ';',
		'before'
	);
	?>
<?php endif; ?>
