<?php
/**
 * Optimizations Tab Template
 *
 * China resource analysis + Google Font localization settings.
 *
 * @package SpeedInChina
 */

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

$speeinch_current_tab          = 'optimizations';
$speeinch_google_fonts_enabled = get_option( SPEEINCH_Google_Fonts::OPTION_ENABLED, false );
?>
<div class="wrap speeinch-wrap">

	<h1><?php esc_html_e( 'Speed in China', 'speed-in-china' ); ?></h1>

	<?php require_once SPEEINCH_PLUGIN_PATH . 'admin/templates/nav-tabs.php'; ?>

	<?php
	/*
	 * =============================================
	 * Persisted Speed Test Optimization Suggestions
	 * =============================================
	 */
	$speeinch_history     = get_option( 'speeinch_speed_test_history', array() );
	$speeinch_last        = ! empty( $speeinch_history ) ? $speeinch_history[0] : null;
	$speeinch_suggestions = ( $speeinch_last && ! empty( $speeinch_last['suggestions'] ) ) ? $speeinch_last['suggestions'] : array();
	$speeinch_last_time   = ( $speeinch_last && isset( $speeinch_last['timestamp'] ) ) ? $speeinch_last['timestamp'] : '';
	$speeinch_last_domain = ( $speeinch_last && isset( $speeinch_last['domain'] ) ) ? $speeinch_last['domain'] : '';
	?>

	<?php if ( ! empty( $speeinch_suggestions ) ) : ?>
	<div class="speeinch-card speeinch-card--spaced">
		<h2>
			<?php esc_html_e( '⚡ Speed Test Optimization Results', 'speed-in-china' ); ?>
			<?php if ( $speeinch_last_time ) : ?>
				<small class="speeinch-result-meta speeinch-result-meta--small">
					<?php echo esc_html( $speeinch_last_time ); ?>
					<?php if ( $speeinch_last_domain ) : ?>
						— <?php echo esc_html( $speeinch_last_domain ); ?>
					<?php endif; ?>
				</small>
			<?php endif; ?>
		</h2>
		<p class="speeinch-card__lede"><?php esc_html_e( 'Based on your latest speed test from multiple China locations, here are your performance metrics and recommendations:', 'speed-in-china' ); ?></p>

		<?php
		foreach ( $speeinch_suggestions as $speeinch_sug ) :
			$speeinch_metric          = isset( $speeinch_sug['metric'] ) ? $speeinch_sug['metric'] : '';
			$speeinch_status          = isset( $speeinch_sug['status'] ) ? $speeinch_sug['status'] : 'good';
			$speeinch_value           = isset( $speeinch_sug['value_ms'] ) ? $speeinch_sug['value_ms'] : '';
			$speeinch_threshold       = isset( $speeinch_sug['threshold'] ) ? $speeinch_sug['threshold'] : '';
			$speeinch_suggestion_text = isset( $speeinch_sug['suggestion'] ) ? $speeinch_sug['suggestion'] : '';
			$speeinch_is_warning      = ( 'warning' === $speeinch_status );
			$speeinch_icon            = $speeinch_is_warning ? '⚠️' : '✅';
			?>
		<div class="speeinch-issue<?php echo $speeinch_is_warning ? ' speeinch-issue--warning' : ' speeinch-issue--ok'; ?>">
			<div class="speeinch-issue-detail">
				<span class="speeinch-issue-service"><?php echo esc_html( $speeinch_icon ); ?> <?php echo esc_html( $speeinch_metric ); ?></span>
				<?php if ( '' !== $speeinch_value ) : ?>
					<span class="speeinch-issue-type">
						<?php echo esc_html( $speeinch_value . 'ms' ); ?>
						<?php if ( '' !== $speeinch_threshold ) : ?>
							/ <?php echo esc_html( $speeinch_threshold . 'ms threshold' ); ?>
						<?php endif; ?>
					</span>
				<?php endif; ?>
				<?php if ( $speeinch_is_warning && $speeinch_suggestion_text ) : ?>
					<div class="speeinch-issue-rec"><?php echo esc_html( $speeinch_suggestion_text ); ?></div>
				<?php elseif ( ! $speeinch_is_warning ) : ?>
					<div class="speeinch-issue-rec speeinch-issue-rec--ok"><?php esc_html_e( 'Within acceptable range — no action needed.', 'speed-in-china' ); ?></div>
				<?php endif; ?>
			</div>
		</div>
		<?php endforeach; ?>
	</div>
	<?php endif; ?>

	<!-- China Resource Analysis -->
	<div class="speeinch-card speeinch-card--spaced">
		<h2><?php esc_html_e( '🔍 China Resource Analysis', 'speed-in-china' ); ?></h2>
		<p><?php esc_html_e( 'Scan your site for resources blocked by the Great Firewall (GFW). Detects Google services, social media embeds, CDNs, and other blocked domains.', 'speed-in-china' ); ?></p>

		<button id="speeinch-run-analysis" class="button button-primary speeinch-button--with-spacing">
			<?php esc_html_e( '🔎 Run Analysis', 'speed-in-china' ); ?>
		</button>
		<span id="speeinch-analysis-spinner" class="spinner speeinch-spinner--inline"></span>

		<div id="speeinch-analysis-results" class="speeinch-hidden-init">
			<div id="speeinch-analysis-timestamp" class="speeinch-analysis-ts-row"></div>
			<div id="speeinch-analysis-summary" class="speeinch-analysis-summary"></div>
			<div id="speeinch-analysis-issues"></div>
		</div>
	</div>

	<!-- Google Font Localization -->
	<div class="speeinch-card speeinch-card--spaced">
		<h2><?php esc_html_e( '🔤 Google Font Localization', 'speed-in-china' ); ?></h2>
		<p><?php esc_html_e( 'Google Fonts (fonts.googleapis.com) are blocked in China. Enable this to download and serve them locally.', 'speed-in-china' ); ?></p>

		<label class="speeinch-toggle">
			<input type="checkbox" id="speeinch-google-fonts-toggle" <?php checked( $speeinch_google_fonts_enabled ); ?>>
			<span><?php esc_html_e( 'Localize Google Fonts', 'speed-in-china' ); ?></span>
		</label>

		<div id="speeinch-font-status" class="speeinch-status-line"></div>

		<button id="speeinch-clear-font-cache" class="button speeinch-button--top-spaced">
			<?php esc_html_e( 'Clear Font Cache', 'speed-in-china' ); ?>
		</button>
	</div>

</div>

