<?php
/**
 * HVAC Cache Optimizer
 * 
 * Provides intelligent caching strategies, cache warming, and optimization
 * for maximum performance of the HVAC Community Events plugin
 * 
 * @package HVAC_Community_Events
 * @since 1.0.8
 */

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

/**
 * HVAC_Cache_Optimizer class
 */
class HVAC_Cache_Optimizer {
    
    /**
     * Cache groups
     */
    const CACHE_GROUP_EVENTS = 'hvac_events';
    const CACHE_GROUP_TRAINERS = 'hvac_trainers';
    const CACHE_GROUP_CERTIFICATES = 'hvac_certificates';
    const CACHE_GROUP_DASHBOARD = 'hvac_dashboard';
    const CACHE_GROUP_API = 'hvac_api';
    
    /**
     * Cache strategies
     */
    const STRATEGY_AGGRESSIVE = 'aggressive';
    const STRATEGY_BALANCED = 'balanced';
    const STRATEGY_CONSERVATIVE = 'conservative';
    
    /**
     * Cache settings
     */
    private static $settings = [
        'strategy' => self::STRATEGY_BALANCED,
        'enable_object_cache' => true,
        'enable_database_cache' => true,
        'enable_api_cache' => true,
        'enable_page_cache' => true,
        'cache_warming_enabled' => true,
        'preload_critical_data' => true,
        'cache_compression' => true,
        'cache_ttl_multiplier' => 1.0
    ];
    
    /**
     * Cache TTL configurations by strategy
     */
    private static $cache_ttls = [
        self::STRATEGY_AGGRESSIVE => [
            'events_list' => 3600,        // 1 hour
            'trainer_profile' => 7200,    // 2 hours
            'dashboard_stats' => 1800,    // 30 minutes
            'certificate_data' => 86400,  // 24 hours
            'api_responses' => 900,       // 15 minutes
            'database_queries' => 600,    // 10 minutes
        ],
        self::STRATEGY_BALANCED => [
            'events_list' => 1800,        // 30 minutes
            'trainer_profile' => 3600,    // 1 hour
            'dashboard_stats' => 900,     // 15 minutes
            'certificate_data' => 43200,  // 12 hours
            'api_responses' => 300,       // 5 minutes
            'database_queries' => 300,    // 5 minutes
        ],
        self::STRATEGY_CONSERVATIVE => [
            'events_list' => 600,         // 10 minutes
            'trainer_profile' => 1800,    // 30 minutes
            'dashboard_stats' => 300,     // 5 minutes
            'certificate_data' => 21600,  // 6 hours
            'api_responses' => 120,       // 2 minutes
            'database_queries' => 120,    // 2 minutes
        ]
    ];
    
    /**
     * Cache warming tasks
     */
    private static $warming_tasks = [];
    
    /**
     * Cache statistics
     */
    private static $stats = [
        'hits' => 0,
        'misses' => 0,
        'sets' => 0,
        'deletes' => 0,
        'flushes' => 0
    ];
    
    /**
     * Initialize cache optimizer
     */
    public static function init() {
        // Load settings
        self::$settings = array_merge(
            self::$settings,
            get_option('hvac_cache_settings', [])
        );
        
        // Initialize cache groups
        self::setup_cache_groups();
        
        // Hook into WordPress cache functions
        add_action('wp_cache_set', [__CLASS__, 'track_cache_set'], 10, 5);
        add_action('wp_cache_get', [__CLASS__, 'track_cache_get'], 10, 4);
        add_action('wp_cache_delete', [__CLASS__, 'track_cache_delete'], 10, 2);
        add_action('wp_cache_flush', [__CLASS__, 'track_cache_flush']);
        
        // Database query caching
        if (self::$settings['enable_database_cache']) {
            add_filter('query', [__CLASS__, 'maybe_cache_query'], 10, 1);
        }
        
        // API response caching
        if (self::$settings['enable_api_cache']) {
            add_filter('hvac_api_response', [__CLASS__, 'cache_api_response'], 10, 3);
            add_filter('hvac_api_request', [__CLASS__, 'get_cached_api_response'], 10, 2);
        }
        
        // Cache warming
        if (self::$settings['cache_warming_enabled']) {
            self::setup_cache_warming();
        }
        
        // Preload critical data
        if (self::$settings['preload_critical_data']) {
            add_action('init', [__CLASS__, 'preload_critical_data'], 20);
        }
        
        // Cache invalidation hooks
        self::setup_cache_invalidation();
        
        // Admin interface
        if (is_admin()) {
            add_action('admin_menu', [__CLASS__, 'add_admin_menu']);
            add_action('wp_ajax_hvac_cache_action', [__CLASS__, 'handle_cache_action']);
        }
        
        // REST API endpoints
        add_action('rest_api_init', [__CLASS__, 'register_rest_endpoints']);
        
        // WP-CLI integration
        if (defined('WP_CLI') && WP_CLI) {
            WP_CLI::add_command('hvac cache', [__CLASS__, 'wp_cli_cache']);
        }
        
        // Performance monitoring integration
        add_action('hvac_performance_check', [__CLASS__, 'analyze_cache_performance']);
    }
    
    /**
     * Setup cache groups
     */
    private static function setup_cache_groups() {
        $cache_groups = [
            self::CACHE_GROUP_EVENTS,
            self::CACHE_GROUP_TRAINERS,
            self::CACHE_GROUP_CERTIFICATES,
            self::CACHE_GROUP_DASHBOARD,
            self::CACHE_GROUP_API
        ];
        
        // Register cache groups as non-persistent if using object cache
        if (function_exists('wp_cache_add_non_persistent_groups')) {
            wp_cache_add_non_persistent_groups($cache_groups);
        }
    }
    
    /**
     * Get cache TTL for key type
     */
    private static function get_cache_ttl($key_type) {
        $strategy = self::$settings['strategy'];
        $base_ttl = self::$cache_ttls[$strategy][$key_type] ?? 300;
        
        // Apply multiplier
        return intval($base_ttl * self::$settings['cache_ttl_multiplier']);
    }
    
    /**
     * Enhanced cache set with compression and statistics
     */
    public static function cache_set($key, $data, $group = '', $expire = 0) {
        // Compress data if enabled and data is large
        if (self::$settings['cache_compression'] && is_string($data) && strlen($data) > 1024) {
            $data = gzcompress($data, 6);
            $key .= '_compressed';
        }
        
        $result = wp_cache_set($key, $data, $group, $expire);
        
        if ($result) {
            self::$stats['sets']++;
        }
        
        return $result;
    }
    
    /**
     * Enhanced cache get with decompression
     */
    public static function cache_get($key, $group = '') {
        $data = wp_cache_get($key, $group);
        
        if ($data !== false) {
            self::$stats['hits']++;
            
            // Check for compressed data
            if (str_ends_with($key, '_compressed')) {
                $data = gzuncompress($data);
            }
        } else {
            self::$stats['misses']++;
            
            // Try compressed version if original not found
            if (!str_ends_with($key, '_compressed')) {
                $compressed_data = wp_cache_get($key . '_compressed', $group);
                if ($compressed_data !== false) {
                    self::$stats['hits']++;
                    $data = gzuncompress($compressed_data);
                }
            }
        }
        
        return $data;
    }
    
    /**
     * Smart cache delete
     */
    public static function cache_delete($key, $group = '') {
        $result = wp_cache_delete($key, $group);
        
        // Also try to delete compressed version
        wp_cache_delete($key . '_compressed', $group);
        
        if ($result) {
            self::$stats['deletes']++;
        }
        
        return $result;
    }
    
    /**
     * Cache events list with smart invalidation
     */
    public static function cache_events_list($args = [], $force_refresh = false) {
        $cache_key = 'events_list_' . md5(serialize($args));
        
        if (!$force_refresh) {
            $cached_data = self::cache_get($cache_key, self::CACHE_GROUP_EVENTS);
            if ($cached_data !== false) {
                return $cached_data;
            }
        }
        
        // Generate fresh data
        $events_data = self::generate_events_list($args);
        
        // Cache the results
        $ttl = self::get_cache_ttl('events_list');
        self::cache_set($cache_key, $events_data, self::CACHE_GROUP_EVENTS, $ttl);
        
        return $events_data;
    }
    
    /**
     * Cache trainer profile data
     */
    public static function cache_trainer_profile($trainer_id, $force_refresh = false) {
        $cache_key = "trainer_profile_$trainer_id";
        
        if (!$force_refresh) {
            $cached_data = self::cache_get($cache_key, self::CACHE_GROUP_TRAINERS);
            if ($cached_data !== false) {
                return $cached_data;
            }
        }
        
        // Generate fresh profile data
        $profile_data = self::generate_trainer_profile($trainer_id);
        
        // Cache the results
        $ttl = self::get_cache_ttl('trainer_profile');
        self::cache_set($cache_key, $profile_data, self::CACHE_GROUP_TRAINERS, $ttl);
        
        return $profile_data;
    }
    
    /**
     * Cache dashboard statistics
     */
    public static function cache_dashboard_stats($user_id, $force_refresh = false) {
        $cache_key = "dashboard_stats_$user_id";
        
        if (!$force_refresh) {
            $cached_data = self::cache_get($cache_key, self::CACHE_GROUP_DASHBOARD);
            if ($cached_data !== false) {
                return $cached_data;
            }
        }
        
        // Generate fresh dashboard data
        if (class_exists('HVAC_Dashboard_Data')) {
            $dashboard = new HVAC_Dashboard_Data($user_id);
            $stats_data = [
                'total_events' => $dashboard->get_total_events_count(),
                'upcoming_events' => $dashboard->get_upcoming_events_count(),
                'completed_events' => $dashboard->get_completed_events_count(),
                'total_attendees' => $dashboard->get_total_attendees_count()
            ];
        } else {
            $stats_data = [];
        }
        
        // Cache the results
        $ttl = self::get_cache_ttl('dashboard_stats');
        self::cache_set($cache_key, $stats_data, self::CACHE_GROUP_DASHBOARD, $ttl);
        
        return $stats_data;
    }
    
    /**
     * Cache certificate data
     */
    public static function cache_certificate_data($event_id, $force_refresh = false) {
        $cache_key = "certificate_data_$event_id";
        
        if (!$force_refresh) {
            $cached_data = self::cache_get($cache_key, self::CACHE_GROUP_CERTIFICATES);
            if ($cached_data !== false) {
                return $cached_data;
            }
        }
        
        // Generate fresh certificate data
        $certificate_data = self::generate_certificate_data($event_id);
        
        // Cache the results with longer TTL (certificates don't change often)
        $ttl = self::get_cache_ttl('certificate_data');
        self::cache_set($cache_key, $certificate_data, self::CACHE_GROUP_CERTIFICATES, $ttl);
        
        return $certificate_data;
    }
    
    /**
     * Database query caching
     */
    public static function maybe_cache_query($query) {
        // Only cache SELECT queries
        if (!preg_match('/^\s*SELECT/i', $query)) {
            return $query;
        }
        
        // Skip queries with functions that shouldn't be cached
        $skip_patterns = [
            '/NOW\(\)/',
            '/RAND\(\)/',
            '/CURRENT_TIMESTAMP/',
            '/USER\(\)/',
            '/CONNECTION_ID\(\)/'
        ];
        
        foreach ($skip_patterns as $pattern) {
            if (preg_match($pattern, $query)) {
                return $query;
            }
        }
        
        $cache_key = 'query_' . md5($query);
        $cached_result = self::cache_get($cache_key, 'hvac_db_cache');
        
        if ($cached_result !== false) {
            return $cached_result;
        }
        
        // Execute query and cache result
        global $wpdb;
        $result = $wpdb->get_results($query);
        
        if (!$wpdb->last_error) {
            $ttl = self::get_cache_ttl('database_queries');
            self::cache_set($cache_key, $result, 'hvac_db_cache', $ttl);
        }
        
        return $query;
    }
    
    /**
     * Cache API responses
     */
    public static function cache_api_response($response, $endpoint, $params) {
        $cache_key = 'api_' . md5($endpoint . serialize($params));
        $ttl = self::get_cache_ttl('api_responses');
        
        self::cache_set($cache_key, $response, self::CACHE_GROUP_API, $ttl);
        
        return $response;
    }
    
    /**
     * Get cached API response
     */
    public static function get_cached_api_response($endpoint, $params) {
        $cache_key = 'api_' . md5($endpoint . serialize($params));
        return self::cache_get($cache_key, self::CACHE_GROUP_API);
    }
    
    /**
     * Setup cache warming
     */
    private static function setup_cache_warming() {
        // Schedule cache warming
        if (!wp_next_scheduled('hvac_warm_cache')) {
            wp_schedule_event(time(), 'hourly', 'hvac_warm_cache');
        }
        
        add_action('hvac_warm_cache', [__CLASS__, 'warm_critical_caches']);
        
        // Warm cache on user login
        add_action('wp_login', [__CLASS__, 'warm_user_specific_cache'], 10, 2);
    }
    
    /**
     * Warm critical caches
     */
    public static function warm_critical_caches() {
        // Warm events list cache
        self::cache_events_list();
        self::cache_events_list(['post_status' => 'publish', 'meta_query' => [
            ['key' => '_EventStartDate', 'value' => date('Y-m-d'), 'compare' => '>=']
        ]]);
        
        // Warm trainer profiles for active trainers
        $trainers = get_users(['role__in' => ['hvac_trainer', 'hvac_master_trainer'], 'number' => 20]);
        foreach ($trainers as $trainer) {
            self::cache_trainer_profile($trainer->ID);
        }
        
        // Warm certificate data for recent events
        $recent_events = get_posts([
            'post_type' => 'tribe_events',
            'post_status' => 'publish',
            'numberposts' => 10,
            'meta_query' => [
                ['key' => '_EventStartDate', 'value' => date('Y-m-d', strtotime('-30 days')), 'compare' => '>=']
            ]
        ]);
        
        foreach ($recent_events as $event) {
            self::cache_certificate_data($event->ID);
        }
        
        HVAC_Logger::info('Critical caches warmed successfully', 'Cache Optimizer');
    }
    
    /**
     * Warm user-specific cache
     */
    public static function warm_user_specific_cache($user_login, $user) {
        if (in_array('hvac_trainer', $user->roles) || in_array('hvac_master_trainer', $user->roles)) {
            // Warm dashboard stats
            self::cache_dashboard_stats($user->ID);
            
            // Warm trainer profile
            self::cache_trainer_profile($user->ID);
        }
    }
    
    /**
     * Preload critical data
     */
    public static function preload_critical_data() {
        // Skip if already preloaded in this request
        if (defined('HVAC_CRITICAL_DATA_PRELOADED')) {
            return;
        }
        
        // Preload plugin options
        $critical_options = [
            'hvac_plugin_settings',
            'hvac_cache_settings',
            'hvac_performance_settings',
            'tribe_events_calendar_options'
        ];
        
        foreach ($critical_options as $option) {
            get_option($option);
        }
        
        // Preload active trainer count
        wp_count_posts('hvac_trainer');
        
        // Preload recent events count
        wp_count_posts('tribe_events');
        
        define('HVAC_CRITICAL_DATA_PRELOADED', true);
    }
    
    /**
     * Setup cache invalidation
     */
    private static function setup_cache_invalidation() {
        // Clear events cache when events are modified
        add_action('save_post', [__CLASS__, 'invalidate_events_cache']);
        add_action('delete_post', [__CLASS__, 'invalidate_events_cache']);
        
        // Clear trainer cache when user profiles are updated
        add_action('profile_update', [__CLASS__, 'invalidate_trainer_cache']);
        add_action('user_register', [__CLASS__, 'invalidate_trainer_cache']);
        add_action('delete_user', [__CLASS__, 'invalidate_trainer_cache']);
        
        // Clear certificate cache when events are updated
        add_action('save_post', [__CLASS__, 'invalidate_certificate_cache']);
        
        // Clear dashboard cache when relevant data changes
        add_action('save_post', [__CLASS__, 'invalidate_dashboard_cache']);
        add_action('profile_update', [__CLASS__, 'invalidate_dashboard_cache']);
    }
    
    /**
     * Invalidate events cache
     */
    public static function invalidate_events_cache($post_id = null) {
        if ($post_id && get_post_type($post_id) !== 'tribe_events') {
            return;
        }
        
        wp_cache_flush_group(self::CACHE_GROUP_EVENTS);
        
        HVAC_Logger::info('Events cache invalidated', 'Cache Optimizer');
    }
    
    /**
     * Invalidate trainer cache
     */
    public static function invalidate_trainer_cache($user_id = null) {
        if ($user_id) {
            self::cache_delete("trainer_profile_$user_id", self::CACHE_GROUP_TRAINERS);
        } else {
            wp_cache_flush_group(self::CACHE_GROUP_TRAINERS);
        }
        
        HVAC_Logger::info('Trainer cache invalidated', 'Cache Optimizer');
    }
    
    /**
     * Invalidate certificate cache
     */
    public static function invalidate_certificate_cache($post_id = null) {
        if ($post_id && get_post_type($post_id) === 'tribe_events') {
            self::cache_delete("certificate_data_$post_id", self::CACHE_GROUP_CERTIFICATES);
        } else {
            wp_cache_flush_group(self::CACHE_GROUP_CERTIFICATES);
        }
        
        HVAC_Logger::info('Certificate cache invalidated', 'Cache Optimizer');
    }
    
    /**
     * Invalidate dashboard cache
     */
    public static function invalidate_dashboard_cache($user_id = null) {
        if ($user_id) {
            self::cache_delete("dashboard_stats_$user_id", self::CACHE_GROUP_DASHBOARD);
        } else {
            wp_cache_flush_group(self::CACHE_GROUP_DASHBOARD);
        }
        
        HVAC_Logger::info('Dashboard cache invalidated', 'Cache Optimizer');
    }
    
    /**
     * Generate events list data
     */
    private static function generate_events_list($args = []) {
        $default_args = [
            'post_type' => 'tribe_events',
            'post_status' => 'publish',
            'numberposts' => 50,
            'orderby' => 'meta_value',
            'meta_key' => '_EventStartDate',
            'order' => 'ASC'
        ];
        
        $query_args = array_merge($default_args, $args);
        $events = get_posts($query_args);
        
        $events_data = [];
        foreach ($events as $event) {
            $events_data[] = [
                'id' => $event->ID,
                'title' => $event->post_title,
                'start_date' => get_post_meta($event->ID, '_EventStartDate', true),
                'end_date' => get_post_meta($event->ID, '_EventEndDate', true),
                'venue_id' => get_post_meta($event->ID, '_EventVenueID', true),
                'organizer_id' => get_post_meta($event->ID, '_EventOrganizerID', true)
            ];
        }
        
        return $events_data;
    }
    
    /**
     * Generate trainer profile data
     */
    private static function generate_trainer_profile($trainer_id) {
        $user = get_user_by('id', $trainer_id);
        
        if (!$user) {
            return null;
        }
        
        $profile_data = [
            'id' => $user->ID,
            'display_name' => $user->display_name,
            'email' => $user->user_email,
            'roles' => $user->roles,
            'meta' => []
        ];
        
        // Get relevant user meta
        $meta_keys = [
            'hvac_trainer_phone',
            'hvac_trainer_company',
            'hvac_trainer_location',
            'hvac_certification_type',
            'hvac_certification_status'
        ];
        
        foreach ($meta_keys as $key) {
            $profile_data['meta'][$key] = get_user_meta($trainer_id, $key, true);
        }
        
        return $profile_data;
    }
    
    /**
     * Generate certificate data
     */
    private static function generate_certificate_data($event_id) {
        $event = get_post($event_id);
        
        if (!$event || $event->post_type !== 'tribe_events') {
            return null;
        }
        
        $certificate_data = [
            'event_id' => $event->ID,
            'event_title' => $event->post_title,
            'start_date' => get_post_meta($event->ID, '_EventStartDate', true),
            'venue_id' => get_post_meta($event->ID, '_EventVenueID', true),
            'organizer_id' => get_post_meta($event->ID, '_EventOrganizerID', true),
            'attendees_count' => 0 // Would be populated by actual attendee system
        ];
        
        return $certificate_data;
    }
    
    /**
     * Analyze cache performance
     */
    public static function analyze_cache_performance() {
        $hit_rate = self::get_cache_hit_rate();
        $memory_usage = self::get_cache_memory_usage();
        
        $performance_data = [
            'hit_rate' => $hit_rate,
            'memory_usage' => $memory_usage,
            'total_operations' => array_sum(self::$stats),
            'stats' => self::$stats
        ];
        
        // Log performance issues
        if ($hit_rate < 70) {
            HVAC_Logger::warning(
                "Low cache hit rate detected: {$hit_rate}%",
                'Cache Optimizer'
            );
        }
        
        if ($memory_usage > 50 * 1024 * 1024) { // 50MB
            HVAC_Logger::warning(
                "High cache memory usage: " . size_format($memory_usage),
                'Cache Optimizer'
            );
        }
        
        return $performance_data;
    }
    
    /**
     * Get cache hit rate
     */
    public static function get_cache_hit_rate() {
        $total_gets = self::$stats['hits'] + self::$stats['misses'];
        
        if ($total_gets == 0) {
            return 0;
        }
        
        return round((self::$stats['hits'] / $total_gets) * 100, 2);
    }
    
    /**
     * Get cache memory usage
     */
    public static function get_cache_memory_usage() {
        // This would require object cache backend specific implementation
        // For now, return estimated usage based on operations
        return self::$stats['sets'] * 1024; // Estimate 1KB per cached item
    }
    
    /**
     * Get cache statistics
     */
    public static function get_cache_stats() {
        return [
            'hit_rate' => self::get_cache_hit_rate(),
            'memory_usage' => self::get_cache_memory_usage(),
            'operations' => self::$stats,
            'strategy' => self::$settings['strategy'],
            'groups' => [
                self::CACHE_GROUP_EVENTS,
                self::CACHE_GROUP_TRAINERS,
                self::CACHE_GROUP_CERTIFICATES,
                self::CACHE_GROUP_DASHBOARD,
                self::CACHE_GROUP_API
            ]
        ];
    }
    
    /**
     * Flush all HVAC caches
     */
    public static function flush_all_caches() {
        $cache_groups = [
            self::CACHE_GROUP_EVENTS,
            self::CACHE_GROUP_TRAINERS,
            self::CACHE_GROUP_CERTIFICATES,
            self::CACHE_GROUP_DASHBOARD,
            self::CACHE_GROUP_API
        ];
        
        foreach ($cache_groups as $group) {
            wp_cache_flush_group($group);
        }
        
        // Also flush database cache
        wp_cache_flush_group('hvac_db_cache');
        
        self::$stats['flushes']++;
        
        HVAC_Logger::info('All HVAC caches flushed', 'Cache Optimizer');
    }
    
    /**
     * Track cache operations for statistics
     */
    public static function track_cache_set($key, $data, $group, $expire, $result) {
        if (str_starts_with($group, 'hvac_')) {
            self::$stats['sets']++;
        }
    }
    
    public static function track_cache_get($key, $group, $force, $found) {
        if (str_starts_with($group, 'hvac_')) {
            if ($found) {
                self::$stats['hits']++;
            } else {
                self::$stats['misses']++;
            }
        }
    }
    
    public static function track_cache_delete($key, $group) {
        if (str_starts_with($group, 'hvac_')) {
            self::$stats['deletes']++;
        }
    }
    
    public static function track_cache_flush() {
        self::$stats['flushes']++;
    }
    
    /**
     * Add admin menu
     */
    public static function add_admin_menu() {
        if (current_user_can('manage_options')) {
            add_management_page(
                'HVAC Cache Optimizer',
                'HVAC Cache',
                'manage_options',
                'hvac-cache-optimizer',
                [__CLASS__, 'admin_page']
            );
        }
    }
    
    /**
     * Admin page
     */
    public static function admin_page() {
        $stats = self::get_cache_stats();
        $performance = self::analyze_cache_performance();
        
        ?>
        <div class="wrap">
            <h1>HVAC Cache Optimizer</h1>
            
            <div class="cache-overview">
                <div class="card">
                    <h2>Cache Performance</h2>
                    <p><strong>Hit Rate:</strong> <?php echo $stats['hit_rate']; ?>%</p>
                    <p><strong>Memory Usage:</strong> <?php echo size_format($stats['memory_usage']); ?></p>
                    <p><strong>Strategy:</strong> <?php echo ucfirst($stats['strategy']); ?></p>
                    <p><strong>Total Operations:</strong> <?php echo array_sum($stats['operations']); ?></p>
                </div>
                
                <div class="card">
                    <h2>Cache Operations</h2>
                    <p><strong>Cache Hits:</strong> <?php echo $stats['operations']['hits']; ?></p>
                    <p><strong>Cache Misses:</strong> <?php echo $stats['operations']['misses']; ?></p>
                    <p><strong>Cache Sets:</strong> <?php echo $stats['operations']['sets']; ?></p>
                    <p><strong>Cache Deletes:</strong> <?php echo $stats['operations']['deletes']; ?></p>
                    <p><strong>Cache Flushes:</strong> <?php echo $stats['operations']['flushes']; ?></p>
                </div>
                
                <div class="card">
                    <h2>Cache Management</h2>
                    <p>
                        <button type="button" id="warm-cache" class="button button-primary">Warm Critical Caches</button>
                        <button type="button" id="flush-cache" class="button">Flush All Caches</button>
                    </p>
                    <p>
                        <button type="button" id="analyze-performance" class="button">Analyze Performance</button>
                    </p>
                </div>
            </div>
            
            <div class="card">
                <h2>Cache Groups</h2>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th>Group</th>
                            <th>Description</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $group_descriptions = [
                            self::CACHE_GROUP_EVENTS => 'Event listings and event data',
                            self::CACHE_GROUP_TRAINERS => 'Trainer profiles and trainer data',
                            self::CACHE_GROUP_CERTIFICATES => 'Certificate data and generation',
                            self::CACHE_GROUP_DASHBOARD => 'Dashboard statistics and metrics',
                            self::CACHE_GROUP_API => 'API responses and external data'
                        ];
                        ?>
                        <?php foreach ($stats['groups'] as $group): ?>
                        <tr>
                            <td><?php echo esc_html($group); ?></td>
                            <td><?php echo esc_html($group_descriptions[$group] ?? 'Cache group'); ?></td>
                            <td>
                                <button type="button" class="button button-small flush-group" 
                                        data-group="<?php echo esc_attr($group); ?>">
                                    Flush Group
                                </button>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <div class="card">
                <h2>Cache Strategy Settings</h2>
                <form method="post" action="">
                    <table class="form-table">
                        <tr>
                            <th><label for="cache_strategy">Cache Strategy</label></th>
                            <td>
                                <select name="cache_strategy" id="cache_strategy">
                                    <option value="conservative" <?php selected($stats['strategy'], 'conservative'); ?>>Conservative (Short TTL)</option>
                                    <option value="balanced" <?php selected($stats['strategy'], 'balanced'); ?>>Balanced (Medium TTL)</option>
                                    <option value="aggressive" <?php selected($stats['strategy'], 'aggressive'); ?>>Aggressive (Long TTL)</option>
                                </select>
                                <p class="description">Choose caching strategy based on your content update frequency.</p>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="cache_warming">Cache Warming</label></th>
                            <td>
                                <input type="checkbox" name="cache_warming" id="cache_warming" 
                                       <?php checked(self::$settings['cache_warming_enabled']); ?> />
                                <label for="cache_warming">Enable automatic cache warming</label>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="cache_compression">Cache Compression</label></th>
                            <td>
                                <input type="checkbox" name="cache_compression" id="cache_compression" 
                                       <?php checked(self::$settings['cache_compression']); ?> />
                                <label for="cache_compression">Enable cache compression for large data</label>
                            </td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="update_cache_settings" class="button button-primary" value="Update Settings" />
                    </p>
                </form>
            </div>
            
            <style>
            .cache-overview { display: flex; gap: 20px; margin-bottom: 20px; }
            .cache-overview .card { flex: 1; }
            </style>
            
            <script>
            document.getElementById('warm-cache')?.addEventListener('click', function() {
                this.disabled = true;
                this.textContent = 'Warming Caches...';
                
                fetch(ajaxurl, {
                    method: 'POST',
                    body: new URLSearchParams({
                        action: 'hvac_cache_action',
                        cache_action: 'warm',
                        nonce: '<?php echo wp_create_nonce('hvac_cache_action'); ?>'
                    })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('Cache warming completed successfully');
                        location.reload();
                    } else {
                        alert('Cache warming failed: ' + data.data);
                        this.disabled = false;
                        this.textContent = 'Warm Critical Caches';
                    }
                });
            });
            
            document.getElementById('flush-cache')?.addEventListener('click', function() {
                if (confirm('Are you sure you want to flush all caches? This may temporarily impact performance.')) {
                    this.disabled = true;
                    this.textContent = 'Flushing Caches...';
                    
                    fetch(ajaxurl, {
                        method: 'POST',
                        body: new URLSearchParams({
                            action: 'hvac_cache_action',
                            cache_action: 'flush',
                            nonce: '<?php echo wp_create_nonce('hvac_cache_action'); ?>'
                        })
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            alert('All caches flushed successfully');
                            location.reload();
                        } else {
                            alert('Cache flush failed: ' + data.data);
                            this.disabled = false;
                            this.textContent = 'Flush All Caches';
                        }
                    });
                }
            });
            
            document.getElementById('analyze-performance')?.addEventListener('click', function() {
                this.disabled = true;
                this.textContent = 'Analyzing...';
                
                fetch(ajaxurl, {
                    method: 'POST',
                    body: new URLSearchParams({
                        action: 'hvac_cache_action',
                        cache_action: 'analyze',
                        nonce: '<?php echo wp_create_nonce('hvac_cache_action'); ?>'
                    })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('Performance analysis completed. Check logs for details.');
                        location.reload();
                    } else {
                        alert('Performance analysis failed: ' + data.data);
                    }
                    this.disabled = false;
                    this.textContent = 'Analyze Performance';
                });
            });
            
            document.querySelectorAll('.flush-group').forEach(button => {
                button.addEventListener('click', function() {
                    const group = this.dataset.group;
                    if (confirm(`Flush cache group: ${group}?`)) {
                        this.disabled = true;
                        this.textContent = 'Flushing...';
                        
                        fetch(ajaxurl, {
                            method: 'POST',
                            body: new URLSearchParams({
                                action: 'hvac_cache_action',
                                cache_action: 'flush_group',
                                cache_group: group,
                                nonce: '<?php echo wp_create_nonce('hvac_cache_action'); ?>'
                            })
                        })
                        .then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                alert(`Cache group ${group} flushed successfully`);
                                location.reload();
                            } else {
                                alert('Cache flush failed: ' + data.data);
                                this.disabled = false;
                                this.textContent = 'Flush Group';
                            }
                        });
                    }
                });
            });
            </script>
        </div>
        <?php
    }
    
    /**
     * Handle cache actions
     */
    public static function handle_cache_action() {
        check_ajax_referer('hvac_cache_action', 'nonce');
        
        if (!current_user_can('manage_options')) {
            wp_send_json_error('Insufficient permissions');
        }
        
        $action = sanitize_text_field($_POST['cache_action']);
        
        switch ($action) {
            case 'warm':
                self::warm_critical_caches();
                wp_send_json_success('Cache warming completed');
                break;
                
            case 'flush':
                self::flush_all_caches();
                wp_send_json_success('All caches flushed');
                break;
                
            case 'flush_group':
                $group = sanitize_text_field($_POST['cache_group']);
                wp_cache_flush_group($group);
                wp_send_json_success("Cache group $group flushed");
                break;
                
            case 'analyze':
                $analysis = self::analyze_cache_performance();
                wp_send_json_success($analysis);
                break;
                
            default:
                wp_send_json_error('Unknown action');
        }
    }
    
    /**
     * Register REST endpoints
     */
    public static function register_rest_endpoints() {
        register_rest_route('hvac/v1', '/cache/stats', [
            'methods' => 'GET',
            'callback' => [__CLASS__, 'rest_cache_stats'],
            'permission_callback' => function() {
                return current_user_can('manage_options');
            }
        ]);
        
        register_rest_route('hvac/v1', '/cache/flush', [
            'methods' => 'POST',
            'callback' => [__CLASS__, 'rest_flush_cache'],
            'permission_callback' => function() {
                return current_user_can('manage_options');
            }
        ]);
    }
    
    /**
     * REST API cache stats
     */
    public static function rest_cache_stats() {
        $stats = self::get_cache_stats();
        
        return new WP_REST_Response([
            'stats' => $stats,
            'timestamp' => time()
        ], 200);
    }
    
    /**
     * REST API flush cache
     */
    public static function rest_flush_cache() {
        self::flush_all_caches();
        
        return new WP_REST_Response([
            'message' => 'All caches flushed successfully',
            'timestamp' => time()
        ], 200);
    }
    
    /**
     * WP-CLI cache command
     */
    public static function wp_cli_cache($args, $assoc_args) {
        $subcommand = $args[0] ?? 'stats';
        
        switch ($subcommand) {
            case 'stats':
                $stats = self::get_cache_stats();
                WP_CLI::line('HVAC Cache Statistics:');
                WP_CLI::line('Hit Rate: ' . $stats['hit_rate'] . '%');
                WP_CLI::line('Memory Usage: ' . size_format($stats['memory_usage']));
                WP_CLI::line('Strategy: ' . ucfirst($stats['strategy']));
                WP_CLI::line('Total Operations: ' . array_sum($stats['operations']));
                break;
                
            case 'warm':
                WP_CLI::line('Warming critical caches...');
                self::warm_critical_caches();
                WP_CLI::success('Cache warming completed');
                break;
                
            case 'flush':
                $group = $assoc_args['group'] ?? '';
                if ($group) {
                    wp_cache_flush_group($group);
                    WP_CLI::success("Cache group $group flushed");
                } else {
                    self::flush_all_caches();
                    WP_CLI::success('All caches flushed');
                }
                break;
                
            case 'analyze':
                WP_CLI::line('Analyzing cache performance...');
                $analysis = self::analyze_cache_performance();
                WP_CLI::line('Hit Rate: ' . $analysis['hit_rate'] . '%');
                WP_CLI::line('Memory Usage: ' . size_format($analysis['memory_usage']));
                WP_CLI::success('Performance analysis completed');
                break;
                
            default:
                WP_CLI::error('Unknown subcommand. Use: stats, warm, flush, analyze');
        }
    }
}