<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<div class="wrap">
	<?php
	$health         = ZACLS_Health::calculate_score();
	$stats          = ZACLS_Analytics::get_summary_stats( 1 );
	$recommendations = ZACLS_Health::get_recommendations();
	$activity       = ZACLS_Analytics::get_login_activity( 14 );
	$attackers      = ZACLS_Analytics::get_top_attackers();
	$recent_audit   = ZACLS_Audit_Trail::get_logs( 10, 0 );
	$lockdown_active = ZACLS_Emergency::is_lockdown_active();
	$notif_count    = ZACLS_Notifications::count_unread();
	$score          = $health['score'];
	$stars          = $score >= 90 ? 5 : ( $score >= 75 ? 4 : ( $score >= 60 ? 3 : ( $score >= 40 ? 2 : 1 ) ) );
	$ring_color     = $score >= 80 ? '#00a32a' : ( $score >= 50 ? '#dba617' : '#d63638' );
	$grade          = $health['grade'];
	$ai_briefing    = ZACLS_AI_Assistant::get_briefing( 1 );
	$trend          = $stats['failed'] > $stats['total'] * 0.5 ? 'up' : ( $stats['failed'] < $stats['total'] * 0.2 ? 'down' : 'neutral' );
	?>

	<?php if ( $lockdown_active ) : ?>
		<div class="zacls-alert zacls-alert-error">
			<strong><?php esc_html_e( 'EMERGENCY LOCKDOWN ACTIVE', 'za-creative-login-shield' ); ?></strong>
			<?php esc_html_e( ' All login attempts are blocked except whitelisted IPs.', 'za-creative-login-shield' ); ?>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=lockdown' ) ); ?>" class="zacls-btn zacls-btn-primary" style="margin-left:8px"><?php esc_html_e( 'Manage Lockdown', 'za-creative-login-shield' ); ?></a>
		</div>
	<?php endif; ?>

	<?php if ( $notif_count > 0 ) : ?>
		<div class="zacls-alert zacls-alert-info">
			<?php printf( esc_html__( 'You have %d unread security notification(s).', 'za-creative-login-shield' ), (int) $notif_count ); ?>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=notifications' ) ); ?>"><?php esc_html_e( 'View Notifications', 'za-creative-login-shield' ); ?></a>
		</div>
	<?php endif; ?>

	<!-- Hero Section -->
	<div class="zacls-card" style="display:flex;align-items:center;gap:24px;padding:20px 24px;margin-bottom:24px">
		<div class="zacls-score-ring" style="width:100px;height:100px;flex-shrink:0">
			<svg width="100" height="100" viewBox="0 0 120 120">
				<circle cx="60" cy="60" r="48" fill="none" stroke="#f0f0f1" stroke-width="8"/>
				<circle cx="60" cy="60" r="48" fill="none" stroke="<?php echo esc_attr( $ring_color ); ?>" stroke-width="8"
					stroke-dasharray="301.6" stroke-dashoffset="<?php echo esc_attr( 301.6 - ( $score / 100 ) * 301.6 ); ?>"
					stroke-linecap="round" transform="rotate(-90 60 60)"/>
			</svg>
			<div class="score-value" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);font-size:24px;font-weight:700;color:<?php echo esc_attr( $ring_color ); ?>"><?php echo esc_html( $score ); ?>%</div>
		</div>
		<div style="flex:1">
			<div style="font-size:22px;letter-spacing:3px;color:<?php echo esc_attr( $ring_color ); ?>"><?php echo wp_kses( str_repeat( '&#9733;', $stars ) . str_repeat( '&#9734;', 5 - $stars ), array() ); ?></div>
			<div style="font-size:13px;color:#50575e;margin-top:4px">
				<?php printf( esc_html__( 'Security Health: %s', 'za-creative-login-shield' ), '<strong>' . esc_html( ucfirst( $grade ) ) . '</strong>' ); ?>
				&middot; <a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=health' ) ); ?>"><?php esc_html_e( 'View Details', 'za-creative-login-shield' ); ?></a>
				<?php if ( count( $recommendations ) > 0 ) : ?>
					&middot; <a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=health' ) ); ?>" style="color:#d63638"><?php printf( esc_html__( '%d recommendations', 'za-creative-login-shield' ), count( $recommendations ) ); ?></a>
				<?php endif; ?>
			</div>
		</div>
		<div style="text-align:right;flex-shrink:0">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=wizard' ) ); ?>" class="zacls-btn zacls-btn-primary"><?php esc_html_e( 'Security Wizard', 'za-creative-login-shield' ); ?></a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=ai-assistant' ) ); ?>" class="zacls-btn zacls-btn-outline"><?php esc_html_e( 'AI Briefing', 'za-creative-login-shield' ); ?></a>
		</div>
	</div>

	<!-- KPI Cards -->
	<div class="zacls-stats-grid">
		<div class="zacls-kpi-card">
			<div class="kpi-header">
				<span class="kpi-label"><?php esc_html_e( 'Login Attempts', 'za-creative-login-shield' ); ?></span>
				<div class="kpi-icon attempts"><span class="dashicons dashicons-chart-area"></span></div>
			</div>
			<div class="kpi-value"><?php echo esc_html( $stats['total'] ); ?></div>
			<div class="kpi-trend <?php echo esc_attr( $trend ); ?>">
				<span class="dashicons dashicons-arrow-<?php echo 'up' === $trend ? 'up' : ( 'down' === $trend ? 'down' : 'right' ); ?>"></span>
				<?php esc_html_e( 'Last 24 hours', 'za-creative-login-shield' ); ?>
			</div>
		</div>
		<div class="zacls-kpi-card">
			<div class="kpi-header">
				<span class="kpi-label"><?php esc_html_e( 'Failed Logins', 'za-creative-login-shield' ); ?></span>
				<div class="kpi-icon failed-logins"><span class="dashicons dashicons-dismiss"></span></div>
			</div>
			<div class="kpi-value" style="color:var(--zacls-danger)"><?php echo esc_html( $stats['failed'] ); ?></div>
			<div class="kpi-trend down">
				<?php $fail_pct = $stats['total'] > 0 ? round( ( $stats['failed'] / $stats['total'] ) * 100 ) : 0; ?>
				<?php echo esc_html( $fail_pct ); ?>% <?php esc_html_e( 'of all attempts', 'za-creative-login-shield' ); ?>
			</div>
		</div>
		<div class="zacls-kpi-card">
			<div class="kpi-header">
				<span class="kpi-label"><?php esc_html_e( 'Blocked IPs', 'za-creative-login-shield' ); ?></span>
				<div class="kpi-icon blocked"><span class="dashicons dashicons-shield"></span></div>
			</div>
			<div class="kpi-value"><?php echo esc_html( $stats['blocked'] ); ?></div>
			<div class="kpi-trend neutral">
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=blocked-ips' ) ); ?>"><?php esc_html_e( 'View blocked IPs', 'za-creative-login-shield' ); ?></a>
			</div>
		</div>
		<div class="zacls-kpi-card">
			<div class="kpi-header">
				<span class="kpi-label"><?php esc_html_e( '2FA Adoption', 'za-creative-login-shield' ); ?></span>
				<div class="kpi-icon twofa"><span class="dashicons dashicons-lock"></span></div>
			</div>
			<div class="kpi-value"><?php echo esc_html( $stats['twofa_enabled'] ); ?><span style="font-size:18px;color:#757575">/<?php echo esc_html( $stats['twofa_total'] ); ?></span></div>
			<div class="kpi-trend neutral">
				<?php $adoption = $stats['twofa_total'] > 0 ? round( ( $stats['twofa_enabled'] / $stats['twofa_total'] ) * 100 ) : 0; ?>
				<?php echo esc_html( $adoption ); ?>% <?php esc_html_e( 'adoption rate', 'za-creative-login-shield' ); ?>
			</div>
		</div>
	</div>

	<div class="zacls-dashboard-grid">
		<div class="zacls-dashboard-main">
			<!-- Activity Chart -->
			<div class="zacls-card">
				<h2 class="zacls-card-title"><?php esc_html_e( 'Login Activity (14 days)', 'za-creative-login-shield' ); ?></h2>
				<div class="zacls-chart" id="zacls-activity-chart">
					<?php
					$max = 1;
					foreach ( $activity as $day ) {
						if ( $day['failed'] + $day['success'] > $max ) $max = $day['failed'] + $day['success'];
					}
					?>
					<div class="zacls-chart-container">
					<?php foreach ( $activity as $i => $day ) :
						$failedH = $max > 0 ? ( $day['failed'] / $max ) * 120 : 0;
						$successH = $max > 0 ? ( $day['success'] / $max ) * 120 : 0;
						$totalH = $failedH + $successH;
						$isToday = $i === count( $activity ) - 1;
					?>
						<div class="zacls-chart-bar-wrapper <?php echo $isToday ? 'chart-today' : ''; ?>">
							<div class="zacls-chart-tooltip"><?php echo esc_html( $day['label'] ); ?>: <?php echo (int) $day['failed']; ?> failed / <?php echo (int) $day['success']; ?> success</div>
							<div class="zacls-chart-bar" style="height:<?php echo esc_attr( max( $totalH, 2 ) ); ?>px">
								<?php if ( $day['failed'] > 0 ) : ?><div class="zacls-chart-bar-failed" style="height:<?php echo esc_attr( $failedH ); ?>px"></div><?php endif; ?>
								<?php if ( $day['success'] > 0 ) : ?><div class="zacls-chart-bar-success" style="height:<?php echo esc_attr( $successH ); ?>px"></div><?php endif; ?>
							</div>
							<div class="chart-label"><?php echo esc_html( $day['label'] ); ?></div>
						</div>
					<?php endforeach; ?>
					</div>
				</div>
			</div>

			<!-- Recent Activity -->
			<div class="zacls-card">
				<h2 class="zacls-card-title"><?php esc_html_e( 'Recent Activity', 'za-creative-login-shield' ); ?></h2>
				<div class="zacls-timeline">
					<?php if ( empty( $recent_audit ) ) : ?>
						<p class="description"><?php esc_html_e( 'No recent activity.', 'za-creative-login-shield' ); ?></p>
					<?php else : ?>
						<?php foreach ( $recent_audit as $entry ) : ?>
							<div class="zacls-timeline-item">
								<div class="timeline-icon"><span class="dashicons dashicons-<?php echo esc_attr( in_array( $entry['action'], array( 'ip_auto_blocked', 'lockdown_activated' ) ) ? 'warning' : 'info' ); ?>"></span></div>
								<div class="timeline-content">
									<strong><code><?php echo esc_html( $entry['action'] ); ?></code></strong>
									<span><?php echo esc_html( $entry['details'] ); ?></span>
									<span class="timeline-time"><?php echo esc_html( $entry['created_at'] ); ?></span>
								</div>
							</div>
						<?php endforeach; ?>
					<?php endif; ?>
				</div>
				<p class="zacls-view-all">
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=audit-trail' ) ); ?>" class="zacls-btn zacls-btn-outline"><?php esc_html_e( 'View All Activity', 'za-creative-login-shield' ); ?></a>
				</p>
			</div>

			<!-- Quick Actions -->
			<div class="zacls-card">
				<h2 class="zacls-card-title"><?php esc_html_e( 'Quick Actions', 'za-creative-login-shield' ); ?></h2>
				<div class="zacls-actions-grid">
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=wizard' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-yes-alt"></span>
						<span><?php esc_html_e( 'Security Wizard', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=health' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-heart"></span>
						<span><?php esc_html_e( 'Health Check', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=analytics' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-chart-bar"></span>
						<span><?php esc_html_e( 'Analytics', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=file-monitor' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-shield"></span>
						<span><?php esc_html_e( 'File Scan', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=vulnerability' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-warning"></span>
						<span><?php esc_html_e( 'Vuln Scan', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=sessions' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-groups"></span>
						<span><?php esc_html_e( 'Sessions', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=ai-assistant' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-lightbulb"></span>
						<span><?php esc_html_e( 'AI Briefing', 'za-creative-login-shield' ); ?></span>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=performance' ) ); ?>" class="zacls-action-card">
						<span class="dashicons dashicons-performance"></span>
						<span><?php esc_html_e( 'Performance', 'za-creative-login-shield' ); ?></span>
					</a>
				</div>
			</div>
		</div>

		<div class="zacls-dashboard-side">
			<!-- AI Briefing Panel -->
			<div class="zacls-card">
				<h2 class="zacls-card-title">
					<span class="dashicons dashicons-lightbulb"></span>
					<?php esc_html_e( 'AI Insights', 'za-creative-login-shield' ); ?>
				</h2>
				<?php if ( ! empty( $ai_briefing['summary'] ) ) : ?>
					<p style="font-size:13px;color:#50575e;line-height:1.6;white-space:pre-line"><?php echo esc_html( $ai_briefing['summary'] ); ?></p>
					<p style="margin-top:8px"><a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=ai-assistant' ) ); ?>" class="rec-action"><?php esc_html_e( 'Full Briefing &rarr;', 'za-creative-login-shield' ); ?></a></p>
				<?php else : ?>
					<p class="description"><?php esc_html_e( 'Insufficient data for AI insights.', 'za-creative-login-shield' ); ?></p>
				<?php endif; ?>
			</div>

			<!-- Recommendations -->
			<div class="zacls-card">
				<h2 class="zacls-card-title">
					<?php esc_html_e( 'Recommendations', 'za-creative-login-shield' ); ?>
					<span class="zacls-status-badge <?php echo count( $recommendations ) > 0 ? 'status-failed' : 'status-success'; ?>" style="margin-left:8px;font-size:10px"><?php echo count( $recommendations ); ?></span>
				</h2>
				<?php if ( empty( $recommendations ) ) : ?>
					<p class="description"><?php esc_html_e( 'All checks passed!', 'za-creative-login-shield' ); ?></p>
				<?php else : ?>
					<ul class="zacls-recommendation-list">
						<?php foreach ( array_slice( $recommendations, 0, 5 ) as $rec ) : ?>
							<li class="zacls-recommendation-item">
								<span class="dashicons dashicons-warning rec-icon"></span>
								<div class="rec-content">
									<span class="rec-text"><?php echo esc_html( $rec['title'] ); ?></span>
									<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=' . $rec['tab'] ) ); ?>" class="rec-action"><?php esc_html_e( 'Fix Issue', 'za-creative-login-shield' ); ?></a>
								</div>
							</li>
						<?php endforeach; ?>
					</ul>
					<?php if ( count( $recommendations ) > 5 ) : ?>
						<p><a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=health' ) ); ?>"><?php printf( esc_html__( 'View all %d recommendations', 'za-creative-login-shield' ), count( $recommendations ) ); ?></a></p>
					<?php endif; ?>
				<?php endif; ?>
			</div>

			<!-- Top Attackers -->
			<div class="zacls-card">
				<h2 class="zacls-card-title"><?php esc_html_e( 'Top Attackers', 'za-creative-login-shield' ); ?></h2>
				<?php if ( empty( $attackers ) ) : ?>
					<p class="description"><?php esc_html_e( 'No attackers detected.', 'za-creative-login-shield' ); ?></p>
				<?php else : ?>
					<div class="zacls-table-wrap">
						<table class="widefat" style="margin-top:0">
							<thead><tr><th><?php esc_html_e( 'IP', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Attempts', 'za-creative-login-shield' ); ?></th></tr></thead>
							<tbody>
							<?php foreach ( array_slice( $attackers, 0, 5 ) as $a ) : ?>
								<tr><td><code><?php echo esc_html( $a['ip'] ); ?></code></td><td><strong><?php echo esc_html( $a['attempts'] ); ?></strong></td></tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					</div>
					<p><a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=logs' ) ); ?>"><?php esc_html_e( 'View All Logs', 'za-creative-login-shield' ); ?></a></p>
				<?php endif; ?>
			</div>

			<!-- Login Origins -->
			<?php if ( 'yes' === get_option( 'zacls_geoip_enabled', 'no' ) ) : ?>
			<div class="zacls-card">
				<h2 class="zacls-card-title"><?php esc_html_e( 'Login Origins', 'za-creative-login-shield' ); ?></h2>
				<?php $countries = ZACLS_Analytics::get_country_stats( 7 ); ?>
				<?php if ( ! empty( $countries ) ) : ?>
					<div class="zacls-table-wrap">
						<table class="widefat" style="margin-top:0">
							<thead><tr><th><?php esc_html_e( 'Country', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Attempts', 'za-creative-login-shield' ); ?></th></tr></thead>
							<tbody>
							<?php foreach ( array_slice( $countries, 0, 5 ) as $c ) : ?>
								<tr><td><?php echo esc_html( $c['country_code'] ?: '??' ); ?></td><td><?php echo esc_html( $c['cnt'] ); ?></td></tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				<?php else : ?>
					<p class="description"><?php esc_html_e( 'No country data available.', 'za-creative-login-shield' ); ?></p>
				<?php endif; ?>
			</div>
			<?php endif; ?>
		</div>
	</div>
</div>
