<?php
/**
 * AI Auto Tool - Migration Manager
 * 
 * Quản lý migration từ cấu trúc cũ sang cấu trúc mới
 * 
 * @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_Migration {
    
    /**
     * Migration version
     * @var string
     */
    private static $migrationVersion = '2.3.0';
    
    /**
     * Initialize migration
     */
    public static function init() {
        // Check if migration is needed
        $currentVersion = get_option('aiautotool_migration_version', '0.0.0');
        
        if (version_compare($currentVersion, self::$migrationVersion, '<')) {
            self::run_migration();
        }
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
    
    /**
     * Run migration
     */
    private static function run_migration() {
        // Initialize feature options first
        if (class_exists('AIAutoTool_FeatureOptions')) {
            AIAutoTool_FeatureOptions::init_default_options();
            AIAutoTool_FeatureOptions::migrate_old_options();
        }
        
        // Migrate menu structure
        self::migrate_menu_structure();
        
        // Migrate autoloader settings
        self::migrate_autoloader_settings();
        
        // Update migration version
        update_option('aiautotool_migration_version', self::$migrationVersion);
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
    
    /**
     * Migrate menu structure
     */
    private static function migrate_menu_structure() {
        // Ensure all feature options are set
        $features = array(
            'api_manager', 
            'keyword_autopost',
            'singleai',
            'post_exporter',
            'canonical',
            'auto_ex_link',
            'submit_index',
            'install_plugins',
            'ssl',
            'images',
            'autocomment',
            'autocreatetags',
            'cloudflareai',
            'suggetauto',
            'prompt_cpt',
            'security_bot',
            'image404_scanner',
            'extra_config',
        );
        
        foreach ($features as $feature) {
            $option = $feature . '_active';
            if (get_option($option) === false) {
                update_option($option, 'true'); // Default to active
            }
        }
    }
    
    /**
     * Migrate autoloader settings
     */
    private static function migrate_autoloader_settings() {
        // Set autoloader as enabled
        update_option('aiautotool_autoloader_enabled', 'true');
        update_option('aiautotool_lazy_loading_enabled', 'true');
        update_option('aiautotool_menu_manager_enabled', 'true');
        
        // Generate class map if needed
        if (class_exists('AIAutoTool_ClassMapper')) {
            AIAutoTool_ClassMapper::generateClassMap();
        }
    }
    
    /**
     * Check if migration is needed
     */
    public static function is_migration_needed() {
        $currentVersion = get_option('aiautotool_migration_version', '0.0.0');
        return version_compare($currentVersion, self::$migrationVersion, '<');
    }
    
    /**
     * Get migration version
     */
    public static function get_migration_version() {
        return self::$migrationVersion;
    }
    
    /**
     * Get current migration version
     */
    public static function get_current_migration_version() {
        return get_option('aiautotool_migration_version', '0.0.0');
    }
    
    /**
     * Force migration (for debugging)
     */
    public static function force_migration() {
        delete_option('aiautotool_migration_version');
        self::run_migration();
    }
    
    /**
     * Reset migration (for testing)
     */
    public static function reset_migration() {
        delete_option('aiautotool_migration_version');
        
        // Reset feature options
        if (class_exists('AIAutoTool_FeatureOptions')) {
            AIAutoTool_FeatureOptions::reset_to_default();
        }
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
}
?>