<?php
/**
 * Email Report Section: Track 404s
 *
 * 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
$total_404s = isset($data['total_404s']) ? number_format($data['total_404s']) : '0';
$unique_urls = isset($data['unique_urls']) ? number_format($data['unique_urls']) : '0';
?>

<!-- 404 Stats -->
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="margin-bottom: 20px;">
    <tr>
        <td width="50%" 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 $total_404s; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Total 404 Errors', 'wpswitchboard'); ?></div>
        </td>
        <td width="10"></td>
        <td width="50%" 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 $unique_urls; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Unique URLs', 'wpswitchboard'); ?></div>
        </td>
    </tr>
</table>

<?php if (!empty($data['top_404_urls'])) : ?>
<!-- Top 404 URLs -->
<h4 style="color: #1d2327; margin: 20px 0 10px; font-size: 14px; font-weight: 600;"><?php esc_html_e('Most Common 404 URLs', 'wpswitchboard'); ?></h4>
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="font-size: 13px;">
    <?php
    $count = 0;
    foreach ($data['top_404_urls'] as $item) :
        if ($count >= 5) break;
        $url = is_array($item) ? ($item['url'] ?? $item['path'] ?? '') : $item;
        $hits = is_array($item) ? ($item['hits'] ?? $item['count'] ?? '') : '';
    ?>
    <tr style="border-bottom: 1px solid #e0e0e0;">
        <td style="padding: 8px 0; color: #1d2327; word-break: break-all;">
            <code style="background: #f0f0f1; padding: 2px 6px; border-radius: 3px; font-size: 12px;"><?php echo esc_html($url); ?></code>
        </td>
        <?php if ($hits) : ?>
            <td style="padding: 8px 0; color: #646970; text-align: right; width: 80px; white-space: nowrap;"><?php echo esc_html(number_format($hits)); ?> <?php esc_html_e('hits', 'wpswitchboard'); ?></td>
        <?php endif; ?>
    </tr>
    <?php
        $count++;
    endforeach;
    ?>
</table>

<?php if (intval($data['total_404s'] ?? 0) > 50) : ?>
<p style="margin-top: 15px; padding: 10px; background-color: #fff3cd; border-radius: 4px; color: #856404; font-size: 13px;">
    <strong><?php esc_html_e('Tip:', 'wpswitchboard'); ?></strong>
    <?php esc_html_e('Consider setting up redirects for frequently accessed 404 URLs to improve user experience and SEO.', 'wpswitchboard'); ?>
</p>
<?php endif; ?>

<?php else : ?>
<p style="color: #00a32a; margin: 0;">
    <?php esc_html_e('No 404 errors recorded during this period.', 'wpswitchboard'); ?>
</p>
<?php endif; ?>
