<?php
/**
 * AI Auto Tool - Option Cache Helper
 * 
 * Caches get_option() calls to reduce database queries
 * 
 * @package AI Auto Tool
 * @version 2.3.0
 */

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

class AIAutoTool_OptionCache {
    
    /**
     * Cache for option values
     * @var array
     */
    private static $cache = array();
    
    /**
     * Cache for file_exists() results
     * @var array
     */
    private static $file_exists_cache = array();
    
    /**
     * Get option with caching
     * 
     * @param string $option_name Option name
     * @param mixed $default Default value
     * @return mixed Option value
     */
    public static function get($option_name, $default = false) {
        // Check cache first
        if (isset(self::$cache[$option_name])) {
            return self::$cache[$option_name];
        }
        
        // Get from database
        $value = get_option($option_name, $default);
        
        // Cache it
        self::$cache[$option_name] = $value;
        
        return $value;
    }
    
    /**
     * Set option and update cache
     * 
     * @param string $option_name Option name
     * @param mixed $value Option value
     * @return bool Success
     */
    public static function set($option_name, $value) {
        $result = update_option($option_name, $value);
        
        // Update cache
        self::$cache[$option_name] = $value;
        
        return $result;
    }
    
    /**
     * Check if option is active (returns true for 'true' string or boolean true)
     * 
     * @param string $option_name Option name
     * @return bool Is active
     */
    public static function is_active($option_name) {
        $value = self::get($option_name, 'false');
        return ($value === 'true' || $value === true);
    }
    
    /**
     * Cached file_exists check
     * 
     * @param string $file_path File path
     * @return bool File exists
     */
    public static function file_exists($file_path) {
        // Normalize path for cache key
        $cache_key = realpath($file_path) ?: $file_path;
        
        // Check cache
        if (isset(self::$file_exists_cache[$cache_key])) {
            return self::$file_exists_cache[$cache_key];
        }
        
        // Check file
        $exists = file_exists($file_path);
        
        // Cache result
        self::$file_exists_cache[$cache_key] = $exists;
        
        return $exists;
    }
    
    /**
     * Clear cache
     */
    public static function clear() {
        self::$cache = array();
        self::$file_exists_cache = array();
    }
    
    /**
     * Clear specific option from cache
     * 
     * @param string $option_name Option name
     */
    public static function clear_option($option_name) {
        unset(self::$cache[$option_name]);
    }
    
    /**
     * Get cache stats
     * 
     * @return array Cache statistics
     */
    public static function get_stats() {
        return array(
            'options_cached' => count(self::$cache),
            'files_cached' => count(self::$file_exists_cache),
        );
    }
}

