<?php
/**
 * Admin View: Overview Dashboard
 *
 * @package GuardianShield
 * @var int    $score      Security score 0-100
 * @var array  $deductions List of score deductions
 * @var array  $stats      Total stats array
 * @var array  $attacks    Attack counts by type
 */
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<div class="wrap gs-wrap">

	<div class="gs-header">
		<div class="gs-header__logo">
			<span class="dashicons dashicons-shield"></span>
			<h1><?php esc_html_e( 'Guardian Shield', 'guardian-shield' ); ?></h1>
		</div>
		<div class="gs-header__status">
			<span class="gs-badge gs-badge--<?php echo $score >= 80 ? 'green' : ( $score >= 50 ? 'yellow' : 'red' ); ?>">
				<?php echo $score >= 80 ? '✓ ' . esc_html__( 'Protected', 'guardian-shield' ) : ( $score >= 50 ? '⚠ ' . esc_html__( 'Needs Attention', 'guardian-shield' ) : '✗ ' . esc_html__( 'At Risk', 'guardian-shield' ) ); ?>
			</span>
		</div>
	</div>

	<!-- Security Score -->
	<div class="gs-score-card">
		<div class="gs-score-card__gauge">
			<svg viewBox="0 0 200 120" class="gs-gauge">
				<path d="M 20 100 A 80 80 0 0 1 180 100" fill="none" stroke="#2d3a4f" stroke-width="16" stroke-linecap="round"/>
				<?php
				$pct        = $score / 100;
				$arc_length = 251.2 * $pct; // ~π * r, r=80
				$color      = $score >= 80 ? '#22c55e' : ( $score >= 50 ? '#f59e0b' : '#ef4444' );
				?>
				<path d="M 20 100 A 80 80 0 0 1 180 100"
					fill="none"
					stroke="<?php echo esc_attr( $color ); ?>"
					stroke-width="16"
					stroke-linecap="round"
					stroke-dasharray="<?php echo esc_attr( $arc_length ); ?> 251.2"
					class="gs-gauge__arc"/>
				<text x="100" y="88" text-anchor="middle" class="gs-gauge__score" fill="<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $score ); ?></text>
				<text x="100" y="108" text-anchor="middle" class="gs-gauge__label" fill="#94a3b8"><?php esc_html_e( 'Security Score', 'guardian-shield' ); ?></text>
			</svg>
		</div>

		<div class="gs-score-card__details">
			<h2><?php esc_html_e( 'Security Overview', 'guardian-shield' ); ?></h2>
			<?php if ( ! empty( $deductions ) ) : ?>
				<div class="gs-issues">
					<h3><?php esc_html_e( 'Issues Found:', 'guardian-shield' ); ?></h3>
					<ul>
						<?php foreach ( $deductions as $d ) : ?>
							<li class="gs-issue"><span class="gs-issue__icon">⚠</span> <?php echo esc_html( $d ); ?></li>
						<?php endforeach; ?>
					</ul>
				</div>
			<?php else : ?>
				<p class="gs-all-clear"><?php esc_html_e( '✓ All security features are active.', 'guardian-shield' ); ?></p>
			<?php endif; ?>
		</div>
	</div>

	<!-- Stats Grid -->
	<div class="gs-stats-grid">
		<div class="gs-stat-card gs-stat-card--<?php echo $stats['attacks_24h'] > 0 ? 'danger' : 'safe'; ?>">
			<div class="gs-stat-card__icon">🛡</div>
			<div class="gs-stat-card__value"><?php echo esc_html( number_format( $stats['attacks_24h'] ) ); ?></div>
			<div class="gs-stat-card__label"><?php esc_html_e( 'Attacks (24h)', 'guardian-shield' ); ?></div>
		</div>
		<div class="gs-stat-card gs-stat-card--info">
			<div class="gs-stat-card__icon">🚫</div>
			<div class="gs-stat-card__value"><?php echo esc_html( number_format( $stats['blocked_24h'] ) ); ?></div>
			<div class="gs-stat-card__label"><?php esc_html_e( 'Blocked (24h)', 'guardian-shield' ); ?></div>
		</div>
		<div class="gs-stat-card gs-stat-card--<?php echo $stats['logins_24h'] > 10 ? 'warning' : 'safe'; ?>">
			<div class="gs-stat-card__icon">🔐</div>
			<div class="gs-stat-card__value"><?php echo esc_html( number_format( $stats['logins_24h'] ) ); ?></div>
			<div class="gs-stat-card__label"><?php esc_html_e( 'Failed Logins (24h)', 'guardian-shield' ); ?></div>
		</div>
		<div class="gs-stat-card gs-stat-card--<?php echo $stats['scan_threats'] > 0 ? 'danger' : 'safe'; ?>">
			<div class="gs-stat-card__icon">🦠</div>
			<div class="gs-stat-card__value"><?php echo esc_html( number_format( $stats['scan_threats'] ) ); ?></div>
			<div class="gs-stat-card__label"><?php esc_html_e( 'Active Threats', 'guardian-shield' ); ?></div>
		</div>
	</div>

	<!-- Threat Chart -->
	<div class="gs-card">
		<div class="gs-card__header">
			<h2><?php esc_html_e( 'Attack Types (Last 7 Days)', 'guardian-shield' ); ?></h2>
		</div>
		<div class="gs-card__body">
			<canvas id="gs-attack-chart" height="120"></canvas>
		</div>
	</div>

	<!-- Quick Actions -->
	<div class="gs-card">
		<div class="gs-card__header">
			<h2><?php esc_html_e( 'Quick Actions', 'guardian-shield' ); ?></h2>
		</div>
		<div class="gs-card__body gs-actions">
			<button class="gs-btn gs-btn--primary" id="gs-run-scan">
				🔍 <?php esc_html_e( 'Run Malware Scan', 'guardian-shield' ); ?>
			</button>
			<button class="gs-btn gs-btn--secondary" id="gs-build-baseline">
				📸 <?php esc_html_e( 'Rebuild File Baseline', 'guardian-shield' ); ?>
			</button>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=guardian-shield-settings' ) ); ?>" class="gs-btn gs-btn--outline">
				⚙️ <?php esc_html_e( 'Settings', 'guardian-shield' ); ?>
			</a>
		</div>
		<div id="gs-action-result" class="gs-action-result" style="display:none"></div>
	</div>

	<!-- Module Status -->
	<div class="gs-card">
		<div class="gs-card__header">
			<h2><?php esc_html_e( 'Module Status', 'guardian-shield' ); ?></h2>
		</div>
		<div class="gs-card__body">
			<table class="gs-table">
				<thead><tr>
					<th><?php esc_html_e( 'Module', 'guardian-shield' ); ?></th>
					<th><?php esc_html_e( 'Status', 'guardian-shield' ); ?></th>
				</tr></thead>
				<tbody>
					<?php
					$modules = [
						[ 'Firewall',           gs_get_option( 'firewall_enabled', true ) ],
						[ 'Rate Limiting',      gs_get_option( 'rate_limit_enabled', true ) ],
						[ 'Login Protection',   true ],
						[ 'File Monitoring',    gs_get_option( 'file_monitor_enabled', true ) ],
						[ 'Malware Scanner',    gs_get_option( 'scan_enabled', true ) ],
						[ 'Security Headers',   gs_get_option( 'headers_enabled', true ) ],
						[ 'XML-RPC Guard',      true ],
						[ 'Bot Detection',      gs_get_option( 'block_bad_bots', true ) ],
						[ 'Email Alerts',       gs_get_option( 'email_alerts', true ) ],
					];
					foreach ( $modules as [ $name, $active ] ) :
					?>
					<tr>
						<td><?php echo esc_html( $name ); ?></td>
						<td>
							<?php if ( $active ) : ?>
								<span class="gs-status gs-status--active">● <?php esc_html_e( 'Active', 'guardian-shield' ); ?></span>
							<?php else : ?>
								<span class="gs-status gs-status--inactive">● <?php esc_html_e( 'Inactive', 'guardian-shield' ); ?></span>
							<?php endif; ?>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	</div>

</div>

<script>
(function($){
	// Attack type chart.
	const attackData = <?php echo wp_json_encode( array_values( $attacks ) ); ?>;
	if (attackData.length && document.getElementById('gs-attack-chart')) {
		const labels = attackData.map(r => r.attack_type.replace(/_/g,' '));
		const values = attackData.map(r => parseInt(r.total));
		new Chart(document.getElementById('gs-attack-chart'), {
			type: 'bar',
			data: {
				labels,
				datasets: [{
					label: '<?php echo esc_js( __( 'Attacks', 'guardian-shield' ) ); ?>',
					data: values,
					backgroundColor: '#3b82f6',
					borderRadius: 4,
				}]
			},
			options: {
				responsive: true,
				plugins: { legend: { display: false } },
				scales: {
					y: { beginAtZero: true, ticks: { color: '#94a3b8' }, grid: { color: '#1e293b' } },
					x: { ticks: { color: '#94a3b8' }, grid: { display: false } }
				}
			}
		});
	}

	// Quick action: run scan.
	$('#gs-run-scan').on('click', function(){
		if (!confirm(gs_admin.strings.confirm_scan)) return;
		const $btn = $(this).prop('disabled', true).text(gs_admin.strings.scanning);
		$.post(gs_admin.ajax_url, {
			action: 'gs_run_scan',
			nonce: gs_admin.nonce
		}, function(res){
			$btn.prop('disabled', false).text('🔍 <?php echo esc_js( __( 'Run Malware Scan', 'guardian-shield' ) ); ?>');
			const msg = res.success
				? '✅ ' + res.data.count + ' <?php echo esc_js( __( 'threat(s) found.', 'guardian-shield' ) ); ?>'
				: '❌ ' + (res.data || 'Error');
			$('#gs-action-result').text(msg).show();
		});
	});

	// Quick action: build baseline.
	$('#gs-build-baseline').on('click', function(){
		if (!confirm(gs_admin.strings.confirm_baseline)) return;
		const $btn = $(this).prop('disabled', true).text('⏳ <?php echo esc_js( __( 'Building…', 'guardian-shield' ) ); ?>');
		$.post(gs_admin.ajax_url, {
			action: 'gs_build_baseline',
			nonce: gs_admin.nonce
		}, function(res){
			$btn.prop('disabled', false).text('📸 <?php echo esc_js( __( 'Rebuild File Baseline', 'guardian-shield' ) ); ?>');
			$('#gs-action-result').text(res.success ? '✅ ' + res.data.message : '❌ Error').show();
		});
	});
})(jQuery);
</script>
