<?php
/**
 * AI Auto Tool - Class Duplicate Fixer
 * 
 * Script để tự động sửa lỗi duplicate class loading
 * 
 * @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_ClassDuplicateFixer {
    
    /**
     * Fix duplicate class loading for all files
     */
    public static function fix_all_files() {
        $plugin_dir = plugin_dir_path(__FILE__) . '../';
        
        // List of files to fix
        $files_to_fix = array(
            'inc/canonical.php' => 'Aiautotool_Canonical',
            'inc/auto-ex-link.php' => 'Aiautotool_exLink_backlink',
            'inc/install-plugin.php' => 'Auto_Install_Plugins',
            'inc/aiautotool_autocomment.php' => 'AIautotool_autocomment',
            'inc/aiautotool_autocreatetag.php' => 'AIautotool_autocreatetags',
            'inc/AI_AutoTool_cloudfalreAI.php' => 'AI_AutoTool_cloudflareAI',
            'inc/AI_AutoTool_suggetauto.php' => 'AI_AutoTool_suggetauto',
            'inc/aiautotool-prompt-cpt.php' => 'AIautotool_Prompt_CPT',
            'inc/AI_AutoTool_Prompt_Customer.php' => 'AI_AutoTool_Prompt_Customer',
            'inc/aiautotool_bot_scrurity.php' => 'AIAutoToolSecurityBot',
            'inc/scan.php' => 'AIAutotool_Image404Scanner',
            'inc/Aiautotool_footer_base.php' => 'Aiautotool_Footer_Base',
            'inc/images.php' => 'AIAutoTool_Images',
            'inc/webstories.php' => 'AIautotool_Web_Stories',
            'inc/aiautotool_create_random_profileuser.php' => 'aiautotool_AutoGenerateUsername',
            'inc/cron-status-widget.php' => 'AIAutoTool_CronStatusWidget',
        );
        
        foreach ($files_to_fix as $file => $class_name) {
            $file_path = $plugin_dir . $file;
            if (file_exists($file_path)) {
                self::fix_file($file_path, $class_name);
            }
        }
        
        if (defined('WP_DEBUG') && WP_DEBUG) {
        }
    }
    
    /**
     * Fix a single file
     */
    private static function fix_file($file_path, $class_name) {
        $content = file_get_contents($file_path);
        
        // Check if already fixed
        if (strpos($content, 'if (!class_exists(') !== false) {
            return;
        }
        
        // Find class declaration
        $pattern = '/^class\s+' . preg_quote($class_name, '/') . '\s+/m';
        if (preg_match($pattern, $content)) {
            // Add class_exists check
            $content = preg_replace(
                '/^class\s+' . preg_quote($class_name, '/') . '\s+/m',
                'if (!class_exists(\'' . $class_name . '\')) {' . "\nclass " . $class_name . ' ',
                $content
            );
            
            // Add closing brace at the end
            $content = rtrim($content) . "\n} // End of class_exists check\n";
            
            // Write back to file
            file_put_contents($file_path, $content);
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
            }
        }
    }
    
    /**
     * Check if file needs fixing
     */
    public static function needs_fixing($file_path, $class_name) {
        if (!file_exists($file_path)) {
            return false;
        }
        
        $content = file_get_contents($file_path);
        
        // Already fixed
        if (strpos($content, 'if (!class_exists(') !== false) {
            return false;
        }
        
        // Has class declaration
        $pattern = '/^class\s+' . preg_quote($class_name, '/') . '\s+/m';
        return preg_match($pattern, $content);
    }
    
    /**
     * Get files that need fixing
     */
    public static function get_files_needing_fix() {
        $plugin_dir = plugin_dir_path(__FILE__) . '../';
        $files_to_check = array(
            'inc/canonical.php' => 'Aiautotool_Canonical',
            'inc/auto-ex-link.php' => 'Aiautotool_exLink_backlink',
            'inc/install-plugin.php' => 'Auto_Install_Plugins',
            'inc/aiautotool_autocomment.php' => 'AIautotool_autocomment',
            'inc/aiautotool_autocreatetag.php' => 'AIautotool_autocreatetags',
            'inc/AI_AutoTool_cloudfalreAI.php' => 'AI_AutoTool_cloudflareAI',
            'inc/AI_AutoTool_suggetauto.php' => 'AI_AutoTool_suggetauto',
            'inc/aiautotool-prompt-cpt.php' => 'AIautotool_Prompt_CPT',
            'inc/AI_AutoTool_Prompt_Customer.php' => 'AI_AutoTool_Prompt_Customer',
            'inc/aiautotool_bot_scrurity.php' => 'AIAutoToolSecurityBot',
            'inc/scan.php' => 'AIAutotool_Image404Scanner',
            'inc/Aiautotool_footer_base.php' => 'Aiautotool_Footer_Base',
            'inc/images.php' => 'AIAutoTool_Images',
            'inc/webstories.php' => 'AIautotool_Web_Stories',
            'inc/aiautotool_create_random_profileuser.php' => 'aiautotool_AutoGenerateUsername',
            'inc/cron-status-widget.php' => 'AIAutoTool_CronStatusWidget',
        );
        
        $needs_fixing = array();
        
        foreach ($files_to_check as $file => $class_name) {
            $file_path = $plugin_dir . $file;
            if (self::needs_fixing($file_path, $class_name)) {
                $needs_fixing[$file] = $class_name;
            }
        }
        
        return $needs_fixing;
    }
}

// Auto-fix on load if in admin
if (is_admin() && !wp_doing_ajax()) {
    add_action('admin_init', function() {
        $files_needing_fix = AIAutoTool_ClassDuplicateFixer::get_files_needing_fix();
        if (!empty($files_needing_fix)) {
            AIAutoTool_ClassDuplicateFixer::fix_all_files();
        }
    }, 1);
}
?>
