<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'Performance Dashboard', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=settings' ) ); ?>" class="zacls-btn"><?php esc_html_e( 'Auto Cleanup Settings', 'za-creative-login-shield' ); ?></a>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Monitor server performance, database health, and plugin resource usage.', 'za-creative-login-shield' ); ?></p>

	<?php
	$perf = ZACLS_Performance::get_stats();
	$mem_class = $perf['memory_pct'] > 80 ? 'status-failed' : ( $perf['memory_pct'] > 50 ? 'status-manual' : 'status-success' );
	$cron_class = $perf['overdue_crons'] > 0 ? 'status-failed' : 'status-success';
	?>

	<div class="zacls-stats-grid">
		<div class="zacls-stat-card">
			<div class="stat-icon database"><span class="dashicons dashicons-database"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $perf['db_size'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Database Size', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon attempts"><span class="dashicons dashicons-dashboard"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $perf['memory_usage'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Memory Usage', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon cron"><span class="dashicons dashicons-clock"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $perf['crons'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Scheduled Tasks', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon blocked"><span class="dashicons dashicons-layout"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $perf['db_tables'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Total Tables', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
	</div>

	<div class="zacls-grid-2">
		<div class="zacls-section-card">
			<div class="zacls-section-card-header">
				<span class="dashicons dashicons-dashboard"></span>
				<h3><?php esc_html_e( 'Server Resources', 'za-creative-login-shield' ); ?></h3>
			</div>
			<div class="zacls-section-card-body">
				<table class="widefat striped">
					<tbody>
						<tr><td><strong><?php esc_html_e( 'Memory Limit', 'za-creative-login-shield' ); ?></strong></td><td><?php echo esc_html( $perf['memory_limit'] ?: 'N/A' ); ?></td></tr>
						<tr><td><strong><?php esc_html_e( 'Peak Memory Usage', 'za-creative-login-shield' ); ?></strong></td><td><?php echo esc_html( $perf['memory_usage'] ); ?> <span class="zacls-status-badge <?php echo esc_attr( $mem_class ); ?>" style="margin-left:8px"><?php echo esc_html( $perf['memory_pct'] ); ?>%</span></td></tr>
						<tr><td><strong><?php esc_html_e( 'Database Queries', 'za-creative-login-shield' ); ?></strong></td><td><?php echo esc_html( $perf['db_queries'] ); ?></td></tr>
					</tbody>
				</table>
			</div>
		</div>

		<div class="zacls-section-card">
			<div class="zacls-section-card-header">
				<span class="dashicons dashicons-clock"></span>
				<h3><?php esc_html_e( 'Cron Jobs', 'za-creative-login-shield' ); ?></h3>
			</div>
			<div class="zacls-section-card-body">
				<table class="widefat striped">
					<tbody>
						<tr><td><strong><?php esc_html_e( 'Total Scheduled Tasks', 'za-creative-login-shield' ); ?></strong></td><td><?php echo esc_html( $perf['crons'] ); ?></td></tr>
						<tr><td><strong><?php esc_html_e( 'Overdue Tasks', 'za-creative-login-shield' ); ?></strong></td><td><?php echo esc_html( $perf['overdue_crons'] ); ?> <span class="zacls-status-badge <?php echo esc_attr( $cron_class ); ?>" style="margin-left:8px"><?php echo $perf['overdue_crons'] > 0 ? esc_html__( 'Action Needed', 'za-creative-login-shield' ) : esc_html__( 'OK', 'za-creative-login-shield' ); ?></span></td></tr>
					</tbody>
				</table>
			</div>
		</div>
	</div>

	<div class="zacls-card">
		<h2 class="zacls-card-title">
			<span class="dashicons dashicons-database"></span>
			<?php esc_html_e( 'Database Overview', 'za-creative-login-shield' ); ?>
		</h2>
		<div class="zacls-table-wrap">
			<table class="widefat striped">
				<thead><tr><th><?php esc_html_e( 'Metric', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Count', 'za-creative-login-shield' ); ?></th></tr></thead>
				<tbody>
					<tr><td><?php esc_html_e( 'Total Database Tables', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['db_tables'] ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Plugin Tables (zacls_*)', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['plugin_tables'] ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Database Size', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['db_size'] ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Login Log Entries', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['login_log_count'] ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Audit Log Entries', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['audit_log_count'] ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Blocked IPs', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['blocked_count'] ); ?></td></tr>
					<tr><td><?php esc_html_e( 'Risk Scores', 'za-creative-login-shield' ); ?></td><td><?php echo esc_html( $perf['risk_scores'] ); ?></td></tr>
					<tr><td><strong><?php esc_html_e( 'Total Plugin Rows', 'za-creative-login-shield' ); ?></strong></td><td><strong><?php echo esc_html( $perf['total_db_rows'] ); ?></strong></td></tr>
				</tbody>
			</table>
		</div>
		<p class="description"><?php esc_html_e( 'Use the Auto Cleanup feature to manage old data and reduce database size.', 'za-creative-login-shield' ); ?></p>
	</div>
</div>
