<?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( 'AI Security Assistant', 'za-creative-login-shield' ); ?>
				<span class="zacls-version-badge" style="vertical-align:middle;font-size:10px"><?php esc_html_e( 'Beta', 'za-creative-login-shield' ); ?></span>
			</h1>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Intelligent daily security briefing and recommendations based on your site\'s login activity.', 'za-creative-login-shield' ); ?></p>

	<?php
	$briefing      = ZACLS_AI_Assistant::get_briefing( 1 );
	$weekly        = ZACLS_AI_Assistant::get_briefing( 7 );
	$stats         = $briefing['stats'];
	$week_stats    = $weekly['stats'];
	$recs          = $briefing['recommendations'];
	$week_recs     = $weekly['recommendations'];
	?>

	<div class="zacls-stats-grid">
		<div class="zacls-stat-card">
			<div class="stat-icon attempts"><span class="dashicons dashicons-chart-area"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $stats['total'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Today', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon failed-logins"><span class="dashicons dashicons-dismiss"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $stats['failed'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Failed Today', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon blocked"><span class="dashicons dashicons-shield"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $stats['blocked'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( 'Blocked Today', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon sessions"><span class="dashicons dashicons-chart-line"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $week_stats['total'] ); ?></span>
				<span class="stat-label"><?php esc_html_e( '7-Day Total', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
	</div>

	<div class="zacls-card">
		<h2 class="zacls-card-title">
			<span class="dashicons dashicons-lightbulb"></span>
			<?php esc_html_e( '24-Hour Security Briefing', 'za-creative-login-shield' ); ?>
		</h2>
		<div class="zacls-note <?php echo $stats['total'] > 0 ? 'info' : 'success'; ?>" style="white-space:pre-line;font-size:14px;line-height:1.6">
			<span class="dashicons dashicons-<?php echo $stats['total'] > 0 ? 'warning' : 'yes'; ?>" style="font-size:20px;width:20px;height:20px;flex-shrink:0;margin-top:2px"></span>
			<?php echo esc_html( $briefing['summary'] ); ?>
		</div>
	</div>

	<div class="zacls-card">
		<h2 class="zacls-card-title">
			<span class="dashicons dashicons-calendar"></span>
			<?php esc_html_e( '7-Day Overview', 'za-creative-login-shield' ); ?>
		</h2>
		<div class="zacls-note <?php echo $week_stats['total'] > 0 ? 'info' : 'success'; ?>" style="white-space:pre-line;font-size:14px;line-height:1.6">
			<span class="dashicons dashicons-<?php echo $week_stats['total'] > 0 ? 'chart-line' : 'yes'; ?>" style="font-size:20px;width:20px;height:20px;flex-shrink:0;margin-top:2px"></span>
			<?php echo esc_html( $weekly['summary'] ); ?>
		</div>
	</div>

	<div class="zacls-grid-2">
		<div class="zacls-card">
			<h2 class="zacls-card-title">
				<span class="dashicons dashicons-flag"></span>
				<?php esc_html_e( 'Smart Recommendations', 'za-creative-login-shield' ); ?>
			</h2>
			<?php if ( empty( $recs ) && empty( $week_recs ) ) : ?>
				<div class="zacls-empty-state">
					<span class="dashicons dashicons-yes" style="color:#00a32a;font-size:48px;width:48px;height:48px"></span>
					<p><?php esc_html_e( 'No issues detected. Your security configuration looks good!', 'za-creative-login-shield' ); ?></p>
				</div>
			<?php else : ?>
				<ul class="zacls-recommendation-list">
					<?php foreach ( array_merge( $recs, $week_recs ) as $r ) : ?>
						<li class="zacls-recommendation-item">
							<span class="dashicons <?php echo esc_attr( $r['icon'] ); ?> rec-icon <?php echo 'warning' === $r['type'] ? 'rec-danger' : ''; ?>"></span>
							<div class="rec-content">
								<span class="rec-text"><?php echo esc_html( $r['text'] ); ?></span>
								<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=' . $r['tab'] ) ); ?>" class="rec-action"><?php esc_html_e( 'Take Action', 'za-creative-login-shield' ); ?></a>
							</div>
						</li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>
		</div>

		<div class="zacls-card">
			<h2 class="zacls-card-title">
				<span class="dashicons dashicons-location"></span>
				<?php esc_html_e( 'Login Origins (24h)', 'za-creative-login-shield' ); ?>
			</h2>
			<?php if ( empty( $briefing['countries'] ) ) : ?>
				<div class="zacls-empty-state">
					<span class="dashicons dashicons-admin-site"></span>
					<p><?php esc_html_e( 'No country data available.', 'za-creative-login-shield' ); ?></p>
				</div>
			<?php else : ?>
				<div class="zacls-country-list">
				<?php
				$max_country = max( array_column( $briefing['countries'], 'cnt' ) );
				foreach ( $briefing['countries'] as $c ) :
					$pct = $max_country > 0 ? ( $c['cnt'] / $max_country ) * 100 : 0;
					$cc = strtolower( $c['country_code'] ?: '??' );
				?>
					<div class="zacls-country-item">
						<span class="zacls-country-flag"><?php echo esc_html( $cc ); ?></span>
						<span class="zacls-country-name"><?php echo esc_html( $c['country_code'] ?: 'Unknown' ); ?></span>
						<div class="zacls-country-bar-wrap">
							<div class="zacls-country-bar" style="width:<?php echo esc_attr( $pct ); ?>%"></div>
						</div>
						<span class="zacls-country-count"><?php echo esc_html( $c['cnt'] ); ?></span>
					</div>
				<?php endforeach; ?>
				</div>
			<?php endif; ?>
			<?php if ( $briefing['top_username'] ) : ?>
				<p class="description" style="margin-top:12px">
					<?php printf( esc_html__( 'Most targeted username: %s', 'za-creative-login-shield' ), '<code>' . esc_html( $briefing['top_username'] ) . '</code>' ); ?>
				</p>
			<?php endif; ?>
		</div>
	</div>
</div>
