<?php
/**
 * Email Report Section: Activity Log
 *
 * 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_events = isset($data['total_events']) ? number_format($data['total_events']) : '0';
$user_logins = isset($data['user_logins']) ? number_format($data['user_logins']) : '0';
$failed_logins = isset($data['failed_logins']) ? number_format($data['failed_logins']) : '0';
$content_changes = isset($data['content_changes']) ? number_format($data['content_changes']) : '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 $total_events; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Total Events', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="25%" style="text-align: center; padding: 15px 10px; background-color: #d4edda; border-radius: 4px;">
            <div style="font-size: 28px; font-weight: 600; color: #155724;"><?php echo $user_logins; ?></div>
            <div style="font-size: 11px; color: #155724; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('User Logins', 'wpswitchboard'); ?></div>
        </td>
        <td width="5"></td>
        <td width="25%" style="text-align: center; padding: 15px 10px; background-color: <?php echo intval($data['failed_logins'] ?? 0) > 10 ? '#f8d7da' : '#f9f9f9'; ?>; border-radius: 4px;">
            <div style="font-size: 28px; font-weight: 600; color: <?php echo intval($data['failed_logins'] ?? 0) > 10 ? '#721c24' : $primary_color; ?>;"><?php echo $failed_logins; ?></div>
            <div style="font-size: 11px; color: <?php echo intval($data['failed_logins'] ?? 0) > 10 ? '#721c24' : '#646970'; ?>; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Failed Logins', '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 $content_changes; ?></div>
            <div style="font-size: 11px; color: #646970; text-transform: uppercase; margin-top: 5px;"><?php esc_html_e('Content Changes', 'wpswitchboard'); ?></div>
        </td>
    </tr>
</table>

<?php if (!empty($data['critical_events'])) : ?>
<!-- Critical Events -->
<h4 style="color: #721c24; margin: 20px 0 10px; font-size: 14px; font-weight: 600;"><?php esc_html_e('Critical Events', 'wpswitchboard'); ?></h4>
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="font-size: 13px; background-color: #fff5f5; border-radius: 4px;">
    <?php
    $count = 0;
    foreach ($data['critical_events'] as $event) :
        if ($count >= 5) break;
        $action = $event['action'] ?? '';
        $description = $event['description'] ?? $event['object_name'] ?? '';
        $user = $event['user_name'] ?? '';
        $date = !empty($event['created_at']) ? wp_date(get_option('date_format') . ' ' . get_option('time_format'), strtotime($event['created_at'])) : '';
    ?>
    <tr style="border-bottom: 1px solid #f5c6cb;">
        <td style="padding: 10px; color: #721c24;">
            <strong><?php echo esc_html(ucwords(str_replace('_', ' ', $action))); ?></strong>
            <?php if ($description) : ?>
                <br><span style="color: #856404;"><?php echo esc_html($description); ?></span>
            <?php endif; ?>
            <?php if ($user || $date) : ?>
                <br><span style="font-size: 11px; color: #999;"><?php echo esc_html($user); ?> <?php echo esc_html($date); ?></span>
            <?php endif; ?>
        </td>
    </tr>
    <?php
        $count++;
    endforeach;
    ?>
</table>
<?php endif; ?>

<?php if (!empty($data['recent_events'])) : ?>
<!-- Recent Activity -->
<h4 style="color: #1d2327; margin: 20px 0 10px; font-size: 14px; font-weight: 600;"><?php esc_html_e('Recent Activity', 'wpswitchboard'); ?></h4>
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="font-size: 13px;">
    <?php
    $count = 0;
    foreach ($data['recent_events'] as $event) :
        if ($count >= 5) break;
        $action = $event['action'] ?? '';
        $object = $event['object_name'] ?? '';
        $user = $event['user_name'] ?? '';
    ?>
    <tr style="border-bottom: 1px solid #e0e0e0;">
        <td style="padding: 8px 0; color: #1d2327;">
            <strong><?php echo esc_html($user); ?></strong> <?php echo esc_html(str_replace('_', ' ', $action)); ?>
            <?php if ($object) : ?>
                <span style="color: #646970;"><?php echo esc_html($object); ?></span>
            <?php endif; ?>
        </td>
    </tr>
    <?php
        $count++;
    endforeach;
    ?>
</table>
<?php endif; ?>
