<?php
/**
 * Admin System Information Page
 *
 * @package MaplePress
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Load system info class
require_once MAPLEPRESS_PLUGIN_DIR . 'includes/class-maplepress-system-info.php';

// Get system information
$system_info = MaplePress_System_Info::get_all_info();
?>

<div class="wrap">
	<h1><?php esc_html_e( 'System Information', 'maplepress' ); ?></h1>

	<div class="notice notice-info" style="padding: 15px; margin: 20px 0;">
		<h2 style="margin-top: 0;">
			📊 <?php esc_html_e( 'System Diagnostics', 'maplepress' ); ?>
		</h2>
		<p>
			<?php esc_html_e( 'This page shows detailed information about your server configuration, PHP-FPM workers, and system resources. Use this data to diagnose performance bottlenecks.', 'maplepress' ); ?>
		</p>
		<p>
			<strong><?php esc_html_e( 'Key Metrics to Check:', 'maplepress' ); ?></strong>
		</p>
		<ul style="margin-left: 20px;">
			<li><?php esc_html_e( 'PHP-FPM Max Children: Should be ≥50 for 4GB servers to handle concurrent requests', 'maplepress' ); ?></li>
			<li><?php esc_html_e( 'OpCache Status: Should be enabled for better PHP performance', 'maplepress' ); ?></li>
			<li><?php esc_html_e( 'Memory Limits: PHP memory_limit should be 256M+', 'maplepress' ); ?></li>
			<li><?php esc_html_e( 'Load Average: Should be below CPU count for healthy performance', 'maplepress' ); ?></li>
			<li><?php esc_html_e( 'Database Connections: max_connections should be 200+ for high traffic', 'maplepress' ); ?></li>
		</ul>
	</div>

	<?php
	// Check for critical issues
	$warnings = array();

	// Check PHP-FPM max_children
	if ( isset( $system_info['php_fpm']['max_children'] ) && is_numeric( $system_info['php_fpm']['max_children'] ) ) {
		if ( $system_info['php_fpm']['max_children'] < 25 ) {
			$warnings[] = sprintf(
				__( '⚠️ PHP-FPM max_children is %d. Recommended: 50+ for 4GB servers. This is the #1 WordPress performance bottleneck.', 'maplepress' ),
				$system_info['php_fpm']['max_children']
			);
		}
	}

	// Check OpCache
	if ( ! $system_info['php']['opcache_enabled'] ) {
		$warnings[] = __( '⚠️ OpCache is not enabled. Enable it for 30-50% faster PHP execution.', 'maplepress' );
	}

	// Check memory limit
	$memory_limit = ini_get( 'memory_limit' );
	if ( preg_match( '/^(\d+)/', $memory_limit, $matches ) ) {
		if ( (int) $matches[1] < 128 ) {
			$warnings[] = sprintf(
				__( '⚠️ PHP memory_limit is %s. Recommended: 256M or higher.', 'maplepress' ),
				$memory_limit
			);
		}
	}

	// Display warnings
	if ( ! empty( $warnings ) ) :
		?>
		<div class="notice notice-warning" style="padding: 15px; margin: 20px 0; border-left-color: #ffc107;">
			<h3 style="margin-top: 0; color: #856404;">
				⚠️ <?php esc_html_e( 'Performance Warnings', 'maplepress' ); ?>
			</h3>
			<ul style="margin-left: 20px; color: #856404;">
				<?php foreach ( $warnings as $warning ) : ?>
					<li><?php echo esc_html( $warning ); ?></li>
				<?php endforeach; ?>
			</ul>
			<p style="margin-bottom: 0;">
				<a href="<?php echo esc_url( MAPLEPRESS_PLUGIN_URL . 'docs/PERFORMANCE_OPTIMIZATION.md' ); ?>"
				   class="button button-primary"
				   target="_blank"
				   style="background: #856404; border-color: #856404;">
					📖 <?php esc_html_e( 'Read Performance Optimization Guide', 'maplepress' ); ?>
				</a>
			</p>
		</div>
	<?php else : ?>
		<div class="notice notice-success" style="padding: 15px; margin: 20px 0;">
			<p style="margin: 0;">
				<strong>✓ <?php esc_html_e( 'System configuration looks good!', 'maplepress' ); ?></strong>
			</p>
		</div>
	<?php endif; ?>

	<!-- System Information Tables -->
	<?php echo MaplePress_System_Info::format_as_html( $system_info ); ?>

	<!-- Export Button -->
	<div style="margin-top: 30px;">
		<button id="copy-system-info" class="button button-primary">
			📋 <?php esc_html_e( 'Copy All Info to Clipboard', 'maplepress' ); ?>
		</button>
		<button id="download-system-info" class="button">
			💾 <?php esc_html_e( 'Download as JSON', 'maplepress' ); ?>
		</button>
	</div>

	<script>
	jQuery(document).ready(function($) {
		// Copy to clipboard
		$('#copy-system-info').on('click', function() {
			var systemInfo = <?php echo wp_json_encode( $system_info ); ?>;
			var text = JSON.stringify(systemInfo, null, 2);

			navigator.clipboard.writeText(text).then(function() {
				alert('<?php esc_html_e( 'System information copied to clipboard!', 'maplepress' ); ?>');
			}).catch(function() {
				// Fallback for older browsers
				var textarea = document.createElement('textarea');
				textarea.value = text;
				document.body.appendChild(textarea);
				textarea.select();
				document.execCommand('copy');
				document.body.removeChild(textarea);
				alert('<?php esc_html_e( 'System information copied to clipboard!', 'maplepress' ); ?>');
			});
		});

		// Download as JSON
		$('#download-system-info').on('click', function() {
			var systemInfo = <?php echo wp_json_encode( $system_info ); ?>;
			var dataStr = JSON.stringify(systemInfo, null, 2);
			var dataBlob = new Blob([dataStr], {type: 'application/json'});
			var url = URL.createObjectURL(dataBlob);

			var link = document.createElement('a');
			link.href = url;
			link.download = 'maplepress-system-info-' + Date.now() + '.json';
			document.body.appendChild(link);
			link.click();
			document.body.removeChild(link);
			URL.revokeObjectURL(url);
		});
	});
	</script>
</div>
