<?php
/**
 * VMPFence Full Activity Log Page
 *
 * @package VMPFence
 * @since 1.0.0
 * @since 2.0.0 Updated to reflect VMP WordPress Security branding.
 * @since 2.2.2 Updated to reflect VMP Security branding.
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Check user capabilities
if (!current_user_can('manage_options')) {
    wp_die( esc_html__('You do not have sufficient permissions to access this page.', 'vmpfence-security') );
}

// Fetch debug logs from options
$debug_index = get_option('vmpfence_last_scan_debug_logs', []);
$all_logs = [];

// Collect all scanner logs
if (isset($debug_index['available_logs']) && is_array($debug_index['available_logs'])) {
    foreach ($debug_index['available_logs'] as $scan_type) {
        $logs = get_option("vmpfence_debug_logs_{$scan_type}", []);
        if (!empty($logs)) {
            foreach ($logs as $log_entry) {
                $all_logs[] = [
                    'scanner' => $scan_type,
                    'message' => $log_entry,
                    'timestamp' => $debug_index['timestamp'] ?? current_time('mysql')
                ];
            }
        }
    }
}

// Function to format timestamp in vmpfence style
function format_vmpfence_timestamp($mysql_timestamp) {
    $date = new DateTime($mysql_timestamp);
    $months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    
    $month = $months[(int)$date->format('n') - 1];
    $day = $date->format('d');
    $time = $date->format('H:i:s');
    $timestamp_ms = $date->getTimestamp();
    
    return "{$month} {$day} {$time}.{$timestamp_ms} {$date->format('Y')} 2 info";
}

$scan_timestamp = isset($debug_index['timestamp']) ? $debug_index['timestamp'] : current_time('mysql');
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>VMP Security Full Activity Log</title>
    <?php
    // Load CSS from external file for standalone page
    $css_file = plugin_dir_path(dirname(__FILE__)) . 'assets/css/full-activity-log.css';
    $css_content = file_exists($css_file) ? file_get_contents($css_file) : '';
    ?>
    <style><?php echo $css_content; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS from plugin file */ ?></style>
</head>
<body>
    <div class="vmpfence-header">
        <h1>
            <span class="vmpfence-logo">V</span>
            VMP Security Full Activity Log
        </h1>
    </div>
    
    <a href="<?php echo esc_url( admin_url('admin.php?page=vmpfence-scan') ); ?>" class="back-button">← Back to Scan</a>
    
    <div class="activity-log-container">
        <div class="log-content">
            <?php if (empty($all_logs)): ?>
                <div class="no-logs">
                    No activity logs available.
                </div>
            <?php else: ?>
                <?php foreach ($all_logs as $log): ?>
                    <div class="log-entry">
[<?php echo esc_html( format_vmpfence_timestamp($log['timestamp']) ); ?>] <?php echo esc_html($log['message']); ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
