<?php
/**
 * Email Report Section: Lean Analytics
 *
 * 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
$pageviews = isset($data['pageviews']) ? number_format($data['pageviews']) : '0';
$visits = isset($data['visits']) ? number_format($data['visits']) : '0';
$unique_visitors = isset($data['unique_visitors']) ? number_format($data['unique_visitors']) : (isset($data['visitors']) ? number_format($data['visitors']) : '0');
$bounce_rate = isset($data['bounce_rate']) ? number_format($data['bounce_rate'], 1) . '%' : '0%';
?>

<!-- Stats Grid -->
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="margin-bottom: 20px;">
    <tr>
        <td width="25%" 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 $pageviews; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Pageviews', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="25%" 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 $visits; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Visits', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="25%" 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_visitors; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Visitors', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="25%" 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 $bounce_rate; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Bounce Rate', 'wpswitchboard'); ?></div>
        </td>
    </tr>
</table>

<?php if (!empty($data['top_pages'])) : ?>
<!-- Top Pages -->
<h4 style="color: #1d2327; margin: 20px 0 10px; font-size: 14px; font-weight: 600;"><?php esc_html_e('Top Pages', 'wpswitchboard'); ?></h4>
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="font-size: 13px;">
    <?php
    $count = 0;
    foreach ($data['top_pages'] as $page) :
        if ($count >= 5) break;
        $title = !empty($page['title']) ? $page['title'] : $page['path'];
        $views = isset($page['pageviews']) ? number_format($page['pageviews']) : '';
    ?>
    <tr style="border-bottom: 1px solid #e0e0e0;">
        <td style="padding: 8px 0; color: #1d2327;"><?php echo esc_html($title); ?></td>
        <td style="padding: 8px 0; color: #646970; text-align: right; width: 80px;"><?php echo esc_html($views); ?></td>
    </tr>
    <?php
        $count++;
    endforeach;
    ?>
</table>
<?php endif; ?>

<?php if (!empty($data['top_referrers'])) : ?>
<!-- Top Referrers -->
<h4 style="color: #1d2327; margin: 20px 0 10px; font-size: 14px; font-weight: 600;"><?php esc_html_e('Top Referrers', 'wpswitchboard'); ?></h4>
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="font-size: 13px;">
    <?php
    $count = 0;
    foreach ($data['top_referrers'] as $referrer) :
        if ($count >= 5) break;
        $domain = $referrer['url'] ?? $referrer['ref_domain'] ?? $referrer['domain'] ?? '';
        $visits = isset($referrer['visits']) ? number_format($referrer['visits']) : (isset($referrer['pageviews']) ? number_format($referrer['pageviews']) : '');
    ?>
    <tr style="border-bottom: 1px solid #e0e0e0;">
        <td style="padding: 8px 0; color: #1d2327;"><?php echo esc_html($domain); ?></td>
        <td style="padding: 8px 0; color: #646970; text-align: right; width: 80px;"><?php echo esc_html($visits); ?></td>
    </tr>
    <?php
        $count++;
    endforeach;
    ?>
</table>
<?php endif; ?>
