<?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( 'Security Health', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<button type="button" class="zacls-btn zacls-btn-primary zacls-apply-recommended"><?php esc_html_e( 'Apply All Recommendations', 'za-creative-login-shield' ); ?></button>
		</div>
	</div>

	<?php
	$health         = ZACLS_Health::calculate_score();
	$recommendations = ZACLS_Health::get_recommendations();
	$score          = $health['score'];
	$grade          = $health['grade'];
	$checks         = $health['checks'];
	$passed_checks  = count( array_filter( $checks, function( $c ) { return $c['pass']; } ) );
	$total_checks   = count( $checks );
	$stars          = $score >= 90 ? 5 : ( $score >= 75 ? 4 : ( $score >= 60 ? 3 : ( $score >= 40 ? 2 : 1 ) ) );
	$ring_color     = $score >= 80 ? '#00a32a' : ( $score >= 50 ? '#dba617' : '#d63638' );
	?>

	<div class="zacls-card">
		<div class="zacls-security-score">
			<div class="zacls-score-ring">
				<svg width="160" height="160" viewBox="0 0 180 180">
					<circle cx="90" cy="90" r="70" fill="none" stroke="#f0f0f1" stroke-width="12"/>
					<circle cx="90" cy="90" r="70" fill="none" stroke="<?php echo esc_attr( $ring_color ); ?>" stroke-width="12"
						stroke-dasharray="440" stroke-dashoffset="<?php echo esc_attr( 440 - ( $score / 100 ) * 440 ); ?>"
						stroke-linecap="round" transform="rotate(-90 90 90)"/>
				</svg>
				<div class="score-value" style="color:<?php echo esc_attr( $ring_color ); ?>"><?php echo esc_html( $score ); ?>%</div>
				<div class="score-label"><?php echo esc_html( ucfirst( $grade ) ); ?></div>
			</div>
			<div class="score-details">
				<div style="font-size:28px;margin-bottom:4px;letter-spacing:4px;color:<?php echo esc_attr( $ring_color ); ?>">
					<?php echo wp_kses( str_repeat( '&#9733;', $stars ) . str_repeat( '&#9734;', 5 - $stars ), array() ); ?>
				</div>
				<p style="font-size:16px;font-weight:600;margin:0 0 8px">
					<?php printf( esc_html__( '%d of %d checks passed', 'za-creative-login-shield' ), (int) $passed_checks, (int) $total_checks ); ?>
					<?php if ( count( $recommendations ) > 0 ) : ?>
						<span class="zacls-status-badge status-failed" style="margin-left:8px"><?php echo esc_html( count( $recommendations ) ); ?> <?php esc_html_e( 'recommendations', 'za-creative-login-shield' ); ?></span>
					<?php else : ?>
						<span class="zacls-status-badge status-success" style="margin-left:8px"><?php esc_html_e( 'All clear!', 'za-creative-login-shield' ); ?></span>
					<?php endif; ?>
				</p>
				<p style="color:#50575e;margin:0"><?php esc_html_e( 'Evaluates your site\'s login security posture based on enabled features, configuration, and best practices.', 'za-creative-login-shield' ); ?></p>
			</div>
		</div>
	</div>

	<div class="zacls-card">
		<h2 class="zacls-card-title">
			<span class="dashicons dashicons-list-view"></span>
			<?php esc_html_e( 'Check Details', 'za-creative-login-shield' ); ?>
		</h2>
		<div class="zacls-health-check-list">
			<?php foreach ( $health['checks'] as $check ) : ?>
				<div class="zacls-health-item <?php echo $check['pass'] ? 'health-pass' : 'health-fail'; ?>">
					<div class="health-icon">
						<span class="dashicons dashicons-<?php echo $check['pass'] ? 'yes' : 'no'; ?>"></span>
					</div>
					<div class="health-info">
						<div class="health-label"><?php echo esc_html( $check['title'] ); ?></div>
						<?php if ( ! $check['pass'] && ! empty( $check['recommendation'] ) ) : ?>
							<div class="health-desc"><?php echo esc_html( $check['recommendation'] ); ?></div>
						<?php endif; ?>
					</div>
					<div class="health-status-text"><?php echo $check['pass'] ? esc_html__( 'Pass', 'za-creative-login-shield' ) : esc_html__( 'Fail', 'za-creative-login-shield' ); ?></div>
				</div>
			<?php endforeach; ?>
		</div>
	</div>

	<?php if ( ! empty( $recommendations ) ) : ?>
		<div class="zacls-card">
			<h2 class="zacls-card-title">
				<span class="dashicons dashicons-warning"></span>
				<?php esc_html_e( 'Recommendations', 'za-creative-login-shield' ); ?>
			</h2>
			<ul class="zacls-recommendation-list">
				<?php foreach ( $recommendations as $rec ) : ?>
					<li class="zacls-recommendation-item">
						<span class="dashicons dashicons-warning rec-icon <?php echo 'high' === $rec['severity'] ? 'rec-danger' : ( 'low' === $rec['severity'] ? 'rec-success' : '' ); ?>"></span>
						<div class="rec-content">
							<span class="rec-text"><strong><?php echo esc_html( $rec['title'] ); ?></strong>
								<span class="zacls-severity-badge severity-<?php echo esc_attr( $rec['severity'] ); ?>" style="margin-left:8px"><?php echo esc_html( ucfirst( $rec['severity'] ) ); ?></span>
							</span>
							<span class="rec-description" style="font-size:12px;color:#757575"><?php echo esc_html( $rec['description'] ); ?></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>
		</div>
	<?php endif; ?>

	<div class="zacls-card">
		<h2 class="zacls-card-title">
			<span class="dashicons dashicons-update"></span>
			<?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=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=lockdown' ) ); ?>" class="zacls-action-card">
				<span class="dashicons dashicons-lock"></span>
				<span><?php esc_html_e( 'Lockdown', '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>
