<?php
/**
 * Email Report Section: Security Summary (Limit Login Attempts)
 *
 * Variables available:
 * - $data (array) Section data from callback
 * - $section_id (string) Section identifier
 */

if (!defined('ABSPATH')) {
    exit;
}

$primary_color = esc_attr($branding['primary_color'] ?? '#2271b1');

// Extract stats
$lockouts = isset($data['lockouts']) ? number_format($data['lockouts']) : '0';
$blocked_ips = isset($data['blocked_ips']) ? number_format($data['blocked_ips']) : '0';
$failed_attempts = isset($data['failed_attempts']) ? number_format($data['failed_attempts']) : '0';

// Determine severity for coloring
$lockouts_count = intval($data['lockouts'] ?? 0);
$is_high_risk = $lockouts_count > 20;
?>

<!-- Security Stats -->
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="margin-bottom: 15px;">
    <tr>
        <td width="33%" style="text-align: center; padding: 15px 10px; background-color: <?php echo $is_high_risk ? '#f8d7da' : '#f9f9f9'; ?>; border-radius: 4px;">
            <div style="font-size: 28px; font-weight: 600; color: <?php echo $is_high_risk ? '#721c24' : $primary_color; ?>;"><?php echo $lockouts; ?></div>
            <div style="font-size: 11px; color: <?php echo $is_high_risk ? '#721c24' : '#646970'; ?>; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Lockouts', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="33%" style="text-align: center; padding: 15px 10px; background-color: #f9f9f9; border-radius: 4px;">
            <div style="font-size: 28px; font-weight: 600; color: <?php echo $primary_color; ?>;"><?php echo $blocked_ips; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Blocked IPs', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="33%" style="text-align: center; padding: 15px 10px; background-color: #f9f9f9; border-radius: 4px;">
            <div style="font-size: 28px; font-weight: 600; color: <?php echo $primary_color; ?>;"><?php echo $failed_attempts; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Failed Attempts', 'wpswitchboard'); ?></div>
        </td>
    </tr>
</table>

<?php if ($is_high_risk) : ?>
<!-- High Risk Warning -->
<div style="background-color: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; padding: 12px; margin-top: 15px;">
    <strong style="color: #856404;"><?php esc_html_e('Security Alert:', 'wpswitchboard'); ?></strong>
    <span style="color: #856404;"><?php esc_html_e('Higher than normal lockout activity detected. Consider reviewing your security settings.', 'wpswitchboard'); ?></span>
</div>
<?php endif; ?>

<?php if (!empty($data['top_blocked_ips'])) : ?>
<!-- Top Blocked IPs -->
<h4 style="color: #1d2327; margin: 20px 0 10px; font-size: 14px; font-weight: 600;"><?php esc_html_e('Top Blocked IPs', 'wpswitchboard'); ?></h4>
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="font-size: 13px;">
    <?php
    $count = 0;
    foreach ($data['top_blocked_ips'] as $ip => $attempts) :
        if ($count >= 5) break;
        if (is_array($attempts)) {
            $ip_address = $attempts['ip'] ?? $ip;
            $attempt_count = $attempts['count'] ?? $attempts['attempts'] ?? 0;
        } else {
            $ip_address = $ip;
            $attempt_count = $attempts;
        }
    ?>
    <tr style="border-bottom: 1px solid #e0e0e0;">
        <td style="padding: 8px 0; color: #1d2327; font-family: monospace;"><?php echo esc_html($ip_address); ?></td>
        <td style="padding: 8px 0; color: #646970; text-align: right; width: 100px;"><?php echo esc_html(number_format($attempt_count)); ?> <?php esc_html_e('attempts', 'wpswitchboard'); ?></td>
    </tr>
    <?php
        $count++;
    endforeach;
    ?>
</table>
<?php endif; ?>
