<?php
/**
 * AI Auto Tool - Feature Options Manager
 * 
 * Quản lý các tùy chọn feature để đảm bảo menu hiển thị đúng
 * 
 * @package AI Auto Tool
 * @version 2.3.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Thoát nếu truy cập trực tiếp

class AIAutoTool_FeatureOptions {
    
    /**
     * Default feature options
     * @var array
     */
    private static $defaultOptions = array();
    
    /**
     * Initialize default options
     */
    public static function init_default_options() {
        self::$defaultOptions = array(
            // Core features (always active)
            'aiautotool_core_active' => 'true',
            
            // Feature toggles
            'aiautotool_api_manager_active' => 'true',
            'aiautotool_keyword_autopost_active' => 'false',
            'aiautotool_singleai_active' => 'true',
            'aiautotool_AIAutoToolsinglepost_active' => 'true', // Single AI menu button - auto activate
            'aiautotool_post_exporter_active' => 'true',
            'aiautotool_canonical_active' => 'false',
            'aiautotool_auto_ex_link_active' => 'false', 
            'aiautotool_submit_index_active' => 'false',
            'aiautotool_install_plugins_active' => 'false',
            'aiautotool_images_active' => 'true',
            'aiautotool_autocomment_active' => 'false',
            'aiautotool_autocreatetags_active' => 'false',
            'aiautotool_AI_AutoTool_Autoblogging_active' => 'false', // Autoblogging - default inactive
            'aiautotool_cloudflareai_active' => 'false',
            'aiautotool_suggetauto_active' => 'false',
            'aiautotool_prompt_cpt_active' => 'false',
            'aiautotool_security_bot_active' => 'false',
            'aiautotool_image404_scanner_active' => 'false',
            'aiautotool_extra_config_active' => 'true',
            
            // AI Providers
            'aiautotool_ai_providers_active' => 'true',
            'aiautotool_openai_active' => 'true',
            'aiautotool_gemini_active' => 'true',
            'aiautotool_claude_active' => 'true',
            
            // Bot Assistant sub-features
            'aiautotool_automation_active' => 'true',
            'aiautotool_tasks_active' => 'true',
            'aiautotool_agent_tools_active' => 'true',
        );
        
        // Set default options if not already set
        foreach (self::$defaultOptions as $option => $defaultValue) {
            if (get_option($option) === false) {
                update_option($option, $defaultValue);
            }
        }
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
    
    /**
     * Normalize option name - ensure it starts with aiautotool_
     */
    private static function normalize_option_name($option) {
        if (strpos($option, 'aiautotool_') !== 0) {
            return 'aiautotool_' . $option;
        }
        return $option;
    }
    
    /**
     * Normalize feature name - remove aiautotool_ prefix if present
     */
    private static function normalize_feature_name($feature) {
        if (strpos($feature, 'aiautotool_') === 0) {
            return substr($feature, 11); // Remove 'aiautotool_' prefix (11 characters)
        }
        return $feature;
    }
    
    /**
     * Get feature option
     * Checks both original name and normalized name, migrates if needed
     */
    public static function get_option($option, $default = 'false') {
        // Try original name first
        $value = get_option($option, false);
        
        if ($value !== false) {
            return $value;
        }
        
        // If not found, try normalized name (for backward compatibility)
        $normalized = self::normalize_option_name($option);
        if ($normalized !== $option) {
            $value = get_option($normalized, false);
            
            // If found with normalized name, migrate to original name
            if ($value !== false) {
                update_option($option, $value, false);
                delete_option($normalized);
                return $value;
            }
        }
        
        return $default;
    }
    
    /**
     * Set feature option
     * Always saves with original name to maintain consistency
     */
    public static function set_option($option, $value) {
        // Always save with original name (not normalized)
        $result = update_option($option, $value, false);
        
        // Check if option exists with normalized name and delete it to avoid confusion
        $normalized = self::normalize_option_name($option);
        if ($normalized !== $option) {
            $normalized_value = get_option($normalized, false);
            if ($normalized_value !== false) {
                // Migrate value if different
                if ($normalized_value !== $value) {
                    update_option($option, $normalized_value, false);
                }
                delete_option($normalized);
            }
        }
        
        return $result;
    }
    
    /**
     * Check if feature is active
     */
    public static function is_feature_active($feature) {
        $feature = self::normalize_feature_name($feature);
        $option = 'aiautotool_' . $feature . '_active';
        return self::get_option($option, 'false') === 'true';
    }
    
    /**
     * Activate feature
     */
    public static function activate_feature($feature) {
        $feature = self::normalize_feature_name($feature);
        $option = 'aiautotool_' . $feature . '_active';
        return self::set_option($option, 'true');
    }
    
    /**
     * Deactivate feature
     */
    public static function deactivate_feature($feature) {
        $feature = self::normalize_feature_name($feature);
        $option = 'aiautotool_' . $feature . '_active';
        return self::set_option($option, 'false');
    }
    
    /**
     * Get all active features
     */
    public static function get_active_features() {
        $activeFeatures = array();
        
        foreach (self::$defaultOptions as $option => $defaultValue) {
            if (strpos($option, '_active') !== false) {
                // Remove 'aiautotool_' prefix and '_active' suffix
                $feature = str_replace('aiautotool_', '', $option);
                $feature = str_replace('_active', '', $feature);
                if (self::is_feature_active($feature)) {
                    $activeFeatures[] = $feature;
                }
            }
        }
        
        return $activeFeatures;
    }
    
    /**
     * Get all features
     */
    public static function get_all_features() {
        $features = array();
        
        foreach (self::$defaultOptions as $option => $defaultValue) {
            if (strpos($option, '_active') !== false) {
                // Remove 'aiautotool_' prefix and '_active' suffix
                $feature = str_replace('aiautotool_', '', $option);
                $feature = str_replace('_active', '', $feature);
                $features[] = $feature;
            }
        }
        
        return $features;
    }
    
    /**
     * Reset all options to default
     */
    public static function reset_to_default() {
        foreach (self::$defaultOptions as $option => $defaultValue) {
            update_option($option, $defaultValue);
        }
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
    
    /**
     * Get default options
     */
    public static function get_default_options() {
        return self::$defaultOptions;
    }
    
    /**
     * Migration from old options
     */
    public static function migrate_old_options() {
        // Map old option names (without aiautotool_ prefix) to new ones (with aiautotool_ prefix)
        $migrationMap = array(
            'api_manager_active' => 'aiautotool_api_manager_active',
            'keyword_autopost_active' => 'aiautotool_keyword_autopost_active',
            'singleai_active' => 'aiautotool_singleai_active',
            'post_exporter_active' => 'aiautotool_post_exporter_active',
            'canonical_active' => 'aiautotool_canonical_active',
            'auto_ex_link_active' => 'aiautotool_auto_ex_link_active',
            'submit_index_active' => 'aiautotool_submit_index_active',
            'install_plugins_active' => 'aiautotool_install_plugins_active',
            'core_active' => 'aiautotool_core_active',
            'AIAutoToolsinglepost_active' => 'aiautotool_AIAutoToolsinglepost_active',
            'images_active' => 'aiautotool_images_active',
            'autocomment_active' => 'aiautotool_autocomment_active',
            'autocreatetags_active' => 'aiautotool_autocreatetags_active',
            'AI_AutoTool_Autoblogging_active' => 'aiautotool_AI_AutoTool_Autoblogging_active',
            'cloudflareai_active' => 'aiautotool_cloudflareai_active',
            'suggetauto_active' => 'aiautotool_suggetauto_active',
            'prompt_cpt_active' => 'aiautotool_prompt_cpt_active',
            'security_bot_active' => 'aiautotool_security_bot_active',
            'image404_scanner_active' => 'aiautotool_image404_scanner_active',
            'extra_config_active' => 'aiautotool_extra_config_active',
            'ai_providers_active' => 'aiautotool_ai_providers_active',
            'openai_active' => 'aiautotool_openai_active',
            'gemini_active' => 'aiautotool_gemini_active',
            'claude_active' => 'aiautotool_claude_active',
            'automation_active' => 'aiautotool_automation_active',
            'tasks_active' => 'aiautotool_tasks_active',
            'agent_tools_active' => 'aiautotool_agent_tools_active',
        );
        
        foreach ($migrationMap as $oldOption => $newOption) {
            $oldValue = get_option($oldOption);
            if ($oldValue !== false) {
                update_option($newOption, $oldValue);
                delete_option($oldOption);
            }
        }
        
        // Also migrate old options that already had aiautotool_ prefix but might be in different format
        $oldPrefixedOptions = array(
            'aiautotool_api_manager_active' => 'aiautotool_api_manager_active',
            'aiautotool_keyword_autopost_active' => 'aiautotool_keyword_autopost_active',
            'aiautotool_singleai_active' => 'aiautotool_singleai_active',
            'aiautotool_post_exporter_active' => 'aiautotool_post_exporter_active',
            'aiautotool_canonical_active' => 'aiautotool_canonical_active',
            'aiautotool_auto_ex_link_active' => 'aiautotool_auto_ex_link_active',
            'aiautotool_submit_index_active' => 'aiautotool_submit_index_active',
            'aiautotool_install_plugins_active' => 'aiautotool_install_plugins_active',
        );
        
        foreach ($oldPrefixedOptions as $oldOption => $newOption) {
            // These are already correct, but ensure they exist
            if (get_option($oldOption) !== false && get_option($newOption) === false) {
                $oldValue = get_option($oldOption);
                update_option($newOption, $oldValue);
            }
        }
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
}
?>