<?php 
/*
Plugin Name: Ai Auto Tool Content Writing Assistant All in One
Plugin URI: https://aiautotool.com
Description: The AI Auto Tool Plugin is a powerful tool that automates various tasks for effortless content creation and management. Now with OpenAI & Gemini API Keys Manager!
Author: KCT
Version: 2.3.9
Author URI: https://aiautotool.com
License: GPL2
*/

if ( ! defined( 'ABSPATH' ) ) exit; // ThoÃ¡t náº¿u truy cáº­p trá»±c tiáº¿p
define( 'MENUSUBPARRENT','ai_auto_tool' );
define('AIAUTOTOOL_URI', plugin_dir_url( __FILE__ ));


// Function to get current plugin version from plugin header
function aiautotool_get_plugin_version() {
    // Get plugin data from WordPress
    $plugin_data = get_plugin_data(__FILE__);
    
    if (isset($plugin_data['Version']) && !empty($plugin_data['Version'])) {
        return $plugin_data['Version'];
    }
    
    // Fallback to readme.txt if plugin header not available
    return aiautotool_get_version_from_readme();
}

define('AIAUTOTOOL_VS', aiautotool_get_plugin_version());

/**
 * Get cache busting version using plugin version
 * Uses plugin version for better cache control, falls back to time() for development
 * Static variable ensures it's only calculated once per request
 * 
 * @return int|string Version number for cache busting
 */
function aiautotool_get_version_cache() {
    static $version_cache = null;
    if ($version_cache === null) {
        // Use plugin version for stable cache busting (better for production)
        // Only use time() if version is not available or in debug mode
        if (defined('AIAUTOTOOL_VS') && !empty(AIAUTOTOOL_VS) && (!defined('WP_DEBUG') || !WP_DEBUG)) {
            $version_cache = AIAUTOTOOL_VS;
        } else {
            // Development mode or version not available - use time() for aggressive cache busting
            $version_cache = time();
        }
    }
    return $version_cache;
}

// Backward compatibility: define constant using static function
if (!defined('AIAUTOTOOL_VERSION_CACHE')) {
    define('AIAUTOTOOL_VERSION_CACHE', aiautotool_get_version_cache());
}

define('AIAUTOTOOL_DIR', plugin_dir_path( __FILE__ ));
define('AIAUTOTOOL_BASENAME', plugin_basename( __FILE__ ));

define('AIAUTOTOOL_FREE', 30);
define('AIAUTOTOOL_API_GEM','https://aplugin.aiautotool.com');

/**
 * Load plugin textdomain early to prevent WordPress 6.7.0 translation loading warnings
 * Must be loaded before any translation functions are called
 * This function loads the textdomain immediately when WordPress functions are available
 */
function aiautotool_load_textdomain_early() {
    if (function_exists('load_plugin_textdomain')) {
        load_plugin_textdomain('ai-auto-tool', false, dirname(plugin_basename(__FILE__)) . '/languages/');
    }
}

// Also register on init hook as backup (in case direct call above fails)
// Priority 1 ensures it runs as early as possible on that hook
if (function_exists('add_action')) {
    add_action('init', 'aiautotool_load_textdomain_early', 1);
}

 
if ( ! function_exists( 'aiautotool_premium' ) ) {
    function aiautotool_premium() {
        global $aiautotool_premium;
        
        if ( ! isset( $aiautotool_premium ) ) {
            
            require_once dirname(__FILE__) . '/vendor/freemius/start.php';

            $aiautotool_premium = fs_dynamic_init( array(
                'id'                  => '15096',
                'slug'                => 'ai-auto-tool',
                'premium_slug'        => 'ai-auto-tool-pay',
                'type'                => 'plugin',
                'public_key'          => 'pk_a1bb06b76f38f9ff1132089d99b21',
                'is_premium'          => false,
                'has_premium_version' => false,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'has_affiliation' => 'all',
                'menu'                => array(
                    'slug'           => 'ai_auto_tool',
                    'contact'        => true,
                    'support'        => false,
                    'network'        => true,
                ),
            ) );
        }

        return $aiautotool_premium;
    }

   
    aiautotool_premium();
    do_action( 'aiautotool_premium_loaded' );
}

if ( ! function_exists( 'aiautotool_get_freemius_access_status' ) ) {
    function aiautotool_get_freemius_access_status() {
        $status = array(
            'can_use_pro_features' => false,
            'is_free' => true,
            'is_paying' => false,
            'is_trial' => false,
            'is_trial_expired' => false,
            'trial_days_remaining' => null,
            'trial_ends' => null,
            'plan_name' => 'free',
            'plan_title' => 'Free',
            'license_id' => null,
            'license_status' => null,
            'reason' => 'free',
            'checked_at' => current_time('mysql'),
            'site_url' => home_url(),
        );

        try {
            if ( ! function_exists( 'aiautotool_premium' ) ) {
                $status['reason'] = 'freemius_unavailable';
                return $status;
            }

            $fs = aiautotool_premium();
            $status['plan_name'] = method_exists($fs, 'get_plan_name') ? (string) $fs->get_plan_name() : 'free';
            $status['plan_title'] = method_exists($fs, 'get_plan_title') ? (string) $fs->get_plan_title() : ucfirst($status['plan_name']);

            $site = null;
            if (method_exists($fs, 'get_site')) {
                try {
                    $site = $fs->get_site();
                } catch (Exception $e) {
                    $site = null;
                }
            }

            $license = null;
            if (method_exists($fs, '_get_license')) {
                try {
                    $license = $fs->_get_license();
                } catch (Exception $e) {
                    $license = null;
                }
            }

            if (is_object($license)) {
                if (isset($license->id)) {
                    $status['license_id'] = $license->id;
                }
                if (isset($license->status)) {
                    $status['license_status'] = $license->status;
                }
            }

            $has_active_license = method_exists($fs, 'has_active_valid_license') && $fs->has_active_valid_license();
            $has_features_license = method_exists($fs, 'has_features_enabled_license') && $fs->has_features_enabled_license();
            $is_paying = method_exists($fs, 'is_paying') && $fs->is_paying();
            $can_use_premium_code = method_exists($fs, 'can_use_premium_code') && $fs->can_use_premium_code();

            $trial_ends_timestamp = 0;
            if ($site && isset($site->trial_ends) && !empty($site->trial_ends)) {
                $trial_ends_timestamp = strtotime($site->trial_ends);
                $status['trial_ends'] = $site->trial_ends;
                $status['trial_days_remaining'] = max(0, (int) ceil(($trial_ends_timestamp - time()) / DAY_IN_SECONDS));
            }

            $is_trial = false;
            if (method_exists($fs, 'is_trial') && $fs->is_trial()) {
                $is_trial = true;
            }
            if (!$is_trial && $trial_ends_timestamp > time()) {
                $is_trial = true;
            }
            if (!$is_trial && $can_use_premium_code && !$is_paying && !$has_active_license && !$has_features_license) {
                $is_trial = true;
            }

            $trial_expired = method_exists($fs, 'has_trial_expired') && $fs->has_trial_expired();
            if (!$trial_expired && $status['trial_ends'] && $trial_ends_timestamp > 0 && $trial_ends_timestamp <= time()) {
                $trial_expired = true;
            }

            $status['is_paying'] = $is_paying || $has_active_license || $has_features_license;
            $status['is_trial'] = $is_trial && !$trial_expired;
            $status['is_trial_expired'] = $trial_expired;

            if ($status['is_paying']) {
                $status['can_use_pro_features'] = true;
                $status['is_free'] = false;
                $status['reason'] = 'paid_license';
            } elseif ($status['is_trial']) {
                $status['can_use_pro_features'] = true;
                $status['is_free'] = false;
                $status['reason'] = 'trial';
            } elseif ($can_use_premium_code && !$trial_expired) {
                $status['can_use_pro_features'] = true;
                $status['is_free'] = false;
                $status['reason'] = 'premium_code';
            } else {
                $status['can_use_pro_features'] = false;
                $status['is_free'] = true;
                $status['reason'] = $trial_expired ? 'trial_expired' : 'free';
            }
        } catch (Exception $e) {
            $status['reason'] = 'freemius_error';
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('AI Auto Tool Freemius access check failed: ' . $e->getMessage());
            }
        }

        return $status;
    }
}

if ( ! function_exists( 'aiautotool_can_use_pro_features' ) ) {
    function aiautotool_can_use_pro_features() {
        $status = aiautotool_get_freemius_access_status();
        return ! empty( $status['can_use_pro_features'] );
    }
}


aiautotool_premium()->add_filter( 'hide_freemius_powered_by', '__return_true' );

// Load New User Discount Code Handler
require_once plugin_dir_path(__FILE__) . 'lib/new-user-discount.php';

// Add custom CSS for Freemius pricing page
aiautotool_premium()->add_filter( 'pricing/css_path', function( $css_path ) {
    return plugin_dir_path(__FILE__) . 'css/freemius-pricing-custom.css';
});

// Configure checkout page parameters
aiautotool_premium()->add_filter( 'checkout/parameters', function() {
    return array(
        'show_refund_badge'      => true,
        'show_reviews'           => true,
        'billing_cycle_selector' => 'dropdown',
        'annual_discount'        => true,
    );
} );

// Ensure annual discount is shown in pricing config
add_action( 'admin_init', function() {
    // Only on pricing page
    if ( isset( $_GET['page'] ) && strpos( $_GET['page'], 'pricing' ) !== false ) {
        // Add annual_discount to pricing config via filter
        aiautotool_premium()->add_filter( 'fs_pricing_query_params', function( $params ) {
            if ( ! isset( $params['annual_discount'] ) ) {
                $params['annual_discount'] = true;
            }
            return $params;
        }, 10, 1 );
    }
}, 1 );

// Enqueue custom CSS for pricing page
add_action( 'admin_enqueue_scripts', function( $hook ) {
    // Only on pricing page
    if ( isset( $_GET['page'] ) && strpos( $_GET['page'], 'pricing' ) !== false ) {
        wp_enqueue_style(
            'aiautotool-freemius-pricing-custom',
            plugin_dir_url(__FILE__) . 'css/freemius-pricing-custom.css',
            array(),
            defined('AIAUTOTOOL_VERSION_CACHE') ? AIAUTOTOOL_VERSION_CACHE : '1.0'
        );
    }
    // Only on account page
    if ( isset( $_GET['page'] ) && strpos( $_GET['page'], 'account' ) !== false ) {
        wp_enqueue_style(
            'aiautotool-freemius-account-custom',
            plugin_dir_url(__FILE__) . 'css/freemius-account-custom.css',
            array(),
            defined('AIAUTOTOOL_VERSION_CACHE') ? AIAUTOTOOL_VERSION_CACHE : '1.0'
        );
    }
}, 999 );

/* translators: %s lÃ  Ä‘Æ°á»ng dáº«n tá»›i trang nÃ¢ng cáº¥p lÃªn báº£n Pro cá»§a AI Auto Tool.*/
$Upgrade_notifice = sprintf('AI Auto Tool Limit Quota. Please <a class="aiautotool_btn_upgradepro aiautotool_red" href="%s" target="_blank"><i class="fa-solid fa-unlock-keyhole"></i> Upgrade Pro</a>',
                                    aiautotool_premium()->get_upgrade_url());
define('AIAUTOTOOL_TITLE_UPGRADE', $Upgrade_notifice);

function aiautotool_activate()
{
    require_once esc_html( plugin_dir_path( __FILE__ ) ) . 'vendor/aiautotool/class-ai-auto-tool-activator.php';
    Ai_auto_tool_Activator::activate();
}


function aiautotool_deactivate()
{
    require_once esc_html( plugin_dir_path( __FILE__ ) ) . 'vendor/aiautotool/class-ai-auto-tool-deactivator.php';
   Ai_auto_tool_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'aiautotool_activate' );
register_deactivation_hook( __FILE__, 'aiautotool_deactivate' );

// Load textdomain immediately before loading any other files that might use translations
// This must happen before bootstrap.php is loaded to prevent WordPress 6.7.0 warnings
// Moved to init hook above to fix early loading notice

// Load Extra Config Module (MANDATORY - always loads, not dependent on active/disable)
// Must load BEFORE class instantiation to ensure class exists when registering
$extra_config_bootstrap = plugin_dir_path(__FILE__) . 'inc/extra-config/bootstrap.php';
if (file_exists($extra_config_bootstrap)) {
    require_once $extra_config_bootstrap;
}

// Load Floating Robot Widget EARLY to ensure admin_enqueue_scripts hook is registered
// This must be loaded before AI_Auto_Tool class is instantiated to catch admin_enqueue_scripts hook

if( ! class_exists( 'AI_Auto_Tool' ) ) {
    class AI_Auto_Tool {
        protected $subclasses = [];
        private $aiautotool_config_settings;
        public function __construct() {
            
            // Initialize Professional Autoloader System
            $this->init_autoloader_system();

            // Textdomain is now loaded early in the plugin file (line ~61) before any code runs
            // This prevents WordPress 6.7.0 translation loading warnings
           
            // Menu registration now handled by Menu Manager
            // add_action( 'admin_menu', array( $this, 'add_menu_page' ) , 10, 2);
            add_action( 'admin_init', array( $this, 'register_settings' ) );
            add_action( 'rest_api_init', array($this,'create_post_route'));
            add_action('wp_ajax_aiautotool_insert_post', array( $this, 'aiautotool_insert_post_ajax' ));
            add_action( 'wp_ajax_aiautotool_get_cate',  array( $this, 'aiautotool_get_cate' ));
            add_action( 'wp_ajax_get_all_media',  array( $this, 'get_all_media_callback' ));
            add_action( 'wp_ajax_aiautotool_save_image',  array( $this, 'aiautotool_save_image' ));
            add_action( 'wp_ajax_aiautotool_toggle_feature', array( $this, 'aiautotool_toggle_feature_callback' ) );
            
            // AJAX handlers for Bard API to avoid CORS issues
            add_action( 'wp_ajax_aiautotool_bard_contentmore', array( $this, 'aiautotool_bard_contentmore_callback' ));
            add_action( 'wp_ajax_aiautotool_bard_content', array( $this, 'aiautotool_bard_content_callback' ));
            add_action( 'wp_ajax_aiautotool_bard_rewrite', array( $this, 'aiautotool_bard_rewrite_callback' ));
            add_action( 'wp_head', array( $this, 'render_custom_head_code' ), 99 );
            add_action( 'wp_body_open', array( $this, 'render_custom_body_code' ), 1 );
            add_action( 'wp_footer', array( $this, 'render_custom_body_code_fallback' ), 1 );
            add_action( 'wp_footer', array( $this, 'render_custom_footer_code' ), 99 );

            // Legacy loading methods (now handled by autoloader)
            // DEBUG MODE: Track memory during initialization
            if (defined('WP_DEBUG') && WP_DEBUG && (defined('AIAUTOTOOL_DEBUG_LOADING') && AIAUTOTOOL_DEBUG_LOADING)) {
                $init_memory = round(memory_get_usage(true) / 1024 / 1024, 2);
                error_log("AI Auto Tool DEBUG: [INIT] Starting plugin initialization - Memory: {$init_memory} MB");
            }
            
            self::load_lib();
            
            if (defined('WP_DEBUG') && WP_DEBUG && (defined('AIAUTOTOOL_DEBUG_LOADING') && AIAUTOTOOL_DEBUG_LOADING)) {
                $after_lib_memory = round(memory_get_usage(true) / 1024 / 1024, 2);
                error_log("AI Auto Tool DEBUG: [AFTER load_lib] Memory: {$after_lib_memory} MB");
            }
            
            // self::load_inc();
            self::aiautotool_new_object();
            
            if (defined('WP_DEBUG') && WP_DEBUG && (defined('AIAUTOTOOL_DEBUG_LOADING') && AIAUTOTOOL_DEBUG_LOADING)) {
                $after_new_object_memory = round(memory_get_usage(true) / 1024 / 1024, 2);
                error_log("AI Auto Tool DEBUG: [AFTER aiautotool_new_object] Memory: {$after_new_object_memory} MB");
            }

            // Assets are now managed by Asset Manager (inc/asset-manager.php)
            // Old method add_aiautotool_button_js() is kept for backward compatibility
            // but Asset Manager handles it more efficiently with conditional loading
            
            // Register AJAX handlers for all features
            add_action( 'init', array( $this, 'register_all_ajax_handlers' ) );

            add_filter('plugin_action_links', array( $this, 'add_ai_auto_tool_settings_link' ), 10, 2);

        register_activation_hook(__FILE__, array($this, 'activate'));
        
        register_deactivation_hook(__FILE__, array($this, 'deactivate'));
        
        $aiautotool_Warning_Notice = new aiautotool_Warning_Notice(array(
            'icon_url'    => plugins_url('/images/logo.svg', __FILE__),
            'title'       => 'Aiautotool Plugin ',
            'content'     => 'The plugin is available for free so if it benefits you. Join the plugin support group here.',
            'button_text' => 'Join group support Ai auto Tool Plugin',
            'button_url'  => 'https://t.me/aiautotoolchat',
        ));
            
            
        }

         public function init_settings() {
        
    }

        private function get_custom_code_settings() {
            $settings = get_option('aiautotool_code_settings', array());

            return is_array($settings) ? $settings : array();
        }

        private function render_custom_code($key, $label) {
            if (is_admin()) {
                return;
            }

            $settings = $this->get_custom_code_settings();
            if (empty($settings[$key]) || !is_string($settings[$key])) {
                return;
            }

            echo "\n<!-- AI Auto Tool {$label} -->\n";
            echo trim(stripslashes($settings[$key]));
            echo "\n<!-- /AI Auto Tool {$label} -->\n";
        }

        public function render_custom_head_code() {
            $this->render_custom_code('code2', 'WP HEAD');
        }

        public function render_custom_body_code() {
            $GLOBALS['aiautotool_custom_body_code_rendered'] = true;
            $this->render_custom_code('code3', 'WP BODY');
        }

        public function render_custom_body_code_fallback() {
            if (!empty($GLOBALS['aiautotool_custom_body_code_rendered'])) {
                return;
            }

            $this->render_custom_body_code();
        }

        public function render_custom_footer_code() {
            $this->render_custom_code('code4', 'WP FOOTER');
        }

        private function aiautotool_getdata() {
            
            return rendersetting::aiautotool_getdata();
            
        }

        public function load_textdomain() {
            load_plugin_textdomain('ai-auto-tool', false, dirname(plugin_basename(__FILE__)) . '/languages/');
        }
         public function activate() {
            if (class_exists('activeCallBack')) {
             $activeCallBack = new activeCallBack();
             $activeCallBack->activate();
            }
         }
         public function deactivate(){
            
            $siteurl = get_option('siteurl');
                $siteurl = str_replace('https://', 'http://', $siteurl);

                update_option('siteurl', $siteurl);
            // }
                 $home = get_option('home');
                $home = str_replace('https://', 'http://', $home);

                update_option('home', $home);
            
            if (class_exists('activeCallBack')) {
                $activeCallBack = new activeCallBack();
                $activeCallBack->deactivate();
            }

          global $wpdb;
            $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'aiautotool_%'" );
            $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'fs_%'" );
         }
        function add_ai_auto_tool_settings_link($links, $file) {
            if (false !== strpos($file, 'ai-auto-tool/Ai-Auto-Tool.php')) {
                $settings_link = '<a href="' . admin_url('admin.php?page=ai_auto_tool') . '">Settings</a>';
                array_unshift($links, $settings_link);
            }
            return $links;
        }
        public function addSubclass($subclass) {
            // Prevent duplicate instances
            foreach ($this->subclasses as $existing) {
                if (get_class($existing) === get_class($subclass)) {
                    return; // Already added, skip duplicate
                }
            }
            
            $this->subclasses[] = $subclass;
        }
        public function aiautotool_new_object(){
            // Static flag to prevent multiple executions
            static $new_object_loaded = false;
            if ($new_object_loaded) {
                return;
            }
            
            // Use option cache helper if available
            $use_cache = class_exists('AIAutoTool_OptionCache');
            $is_admin_page = is_admin();
            
            // CRITICAL OPTIMIZATION: Only load essential classes immediately
            // Other features will be loaded on-demand when needed (lazy loading)
            
            // Extra Config - New Architecture (required for settings)
            if (class_exists('Aiautotool_Extra_Config_Manager')) {
                $extra_config_manager = Aiautotool_Extra_Config_Manager::instance();
                $this->addSubclass($extra_config_manager);
            }
            
            // AI Provider Settings UI - Always loaded (mandatory)
            if (class_exists('AIAutotool_AI_Provider_Settings_UI')) {
                $ai_provider_settings_ui = new AIAutotool_AI_Provider_Settings_UI();
                $this->addSubclass($ai_provider_settings_ui);
            }
            
            // OPTIMIZED: Only load features that are active OR needed for admin UI
            // Features are loaded on-demand when their settings page is accessed
            
            // Autoblogging - Only load if active
            $autoblogging_active = $use_cache ? AIAutoTool_OptionCache::is_active('AI_AutoTool_Autoblogging_active') :
                        (get_option('AI_AutoTool_Autoblogging_active', 'false') === 'true');
            if ($autoblogging_active) {
                $autoblogging_file = plugin_dir_path(__FILE__) . 'inc/features/autoblogging/autoblogging.php';
                $file_exists = $use_cache ? AIAutoTool_OptionCache::file_exists($autoblogging_file) : file_exists($autoblogging_file);
                if ($file_exists) {
                    require_once $autoblogging_file;
                    if (class_exists('AIAutoTool\\Features\\Autoblogging\\Autoblogging')) {
                        try {
                            $autoblogging = new \AIAutoTool\Features\Autoblogging\Autoblogging();
                            $this->addSubclass($autoblogging);
                        } catch (Exception $e) {
                            if (defined('WP_DEBUG') && WP_DEBUG) {
                                error_log('AI Auto Tool: Failed to load Autoblogging: ' . $e->getMessage());
                            }
                        }
                    }
                }
            }
            
            // Keyword Autopost - Load always (menu needs to be registered even if inactive)
            $keyword_autopost_file = plugin_dir_path(__FILE__) . 'inc/features/keyword-autopost/keyword-autopost.php';
            $file_exists = $use_cache ? AIAutoTool_OptionCache::file_exists($keyword_autopost_file) : file_exists($keyword_autopost_file);
            if ($file_exists) {
                require_once $keyword_autopost_file;
                if (class_exists('AIAutoTool\\Features\\KeywordAutopost\\KeywordAutopost')) {
                    try {
                        $keyword_autopost = new \AIAutoTool\Features\KeywordAutopost\KeywordAutopost();
                        $this->addSubclass($keyword_autopost);
                    } catch (Exception $e) {
                        if (defined('WP_DEBUG') && WP_DEBUG) {
                            error_log('AI Auto Tool: Failed to load Keyword Autopost: ' . $e->getMessage());
                        }
                    }
                }
            }
            
            // OPTIMIZED: Batch load features only when active or needed for admin UI
            // Features are defined with their active option names for efficient checking
            $features_to_load = array(
                array(
                    'file' => 'inc/features/image-scanner/image-scanner.php',
                    'class' => 'AIAutoTool\\Features\\ImageScanner\\ImageScanner',
                    'option' => 'AIAutotool_Image404Scanner_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/bot-security/bot-security.php',
                    'class' => 'AIAutoTool\\Features\\BotSecurity\\BotSecurity',
                    'option' => 'AIAutoToolSecurityBot_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/auto-comment/auto-comment.php',
                    'class' => 'AIAutoTool\\Features\\AutoComment\\AutoComment',
                    'option' => 'AIautotool_autocomment_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/auto-create-tags/auto-create-tags.php',
                    'class' => 'AIAutoTool\\Features\\AutoCreateTags\\AutoCreateTags',
                    'option' => 'AIautotool_autocreatetags_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/canonical-manager/canonical-manager.php',
                    'class' => 'AIAutoTool\\Features\\CanonicalManager\\CanonicalManager',
                    'option' => 'Aiautotool_Canonical_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/external-links/external-links.php',
                    'class' => 'AIAutoTool\\Features\\ExternalLinks\\ExternalLinks',
                    'option' => 'Aiautotool_exLink_backlink_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/cloudflare-ai/cloudflare-ai.php',
                    'class' => 'AIAutoTool\\Features\\CloudflareAI\\CloudflareAI',
                    'option' => 'AI_AutoTool_cloudflareAI_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/submit-index/submit-index.php',
                    'class' => 'AIAutoTool\\Features\\SubmitIndex\\SubmitIndex',
                    'option' => 'AIAutoTool_SubmitIndex_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/suggest-auto/suggest-auto.php',
                    'class' => 'AIAutoTool\\Features\\SuggestAuto\\SuggestAuto',
                    'option' => 'AI_AutoTool_suggetauto_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/username-generator/username-generator.php',
                    'class' => 'AIAutoTool\\Features\\UsernameGenerator\\UsernameGenerator',
                    'option' => 'aiautotool_AutoGenerateUsername_active',
                    'load_on_admin' => false
                ),
                array(
                    'file' => 'inc/features/web-stories/web-stories.php',
                    'class' => 'AIAutoTool\\Features\\WebStories\\WebStories',
                    'option' => 'AIautotool_Web_Stories_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/footer-base/footer-base.php',
                    'class' => 'AIAutoTool\\Features\\FooterBase\\FooterBase',
                    'option' => 'Aiautotool_footer_base_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/prompt-cpt/prompt-cpt.php',
                    'class' => 'AIAutoTool\\Features\\PromptCPT\\PromptCPT',
                    'option' => 'AIautotool_Prompt_CPT_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/auto-create-categories/auto-create-categories.php',
                    'class' => 'AIAutoTool_AutoCreateCategories',
                    'option' => 'AIAutoTool_auto_create_categories_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/crawlcampaign/crawlcampaign.php',
                    'class' => 'AIAutoTool\\Features\\CrawlCampaign\\CrawlCampaign',
                    'option' => 'aiautotool_crawlcampaign_active',
                    'load_on_admin' => true
                ),
                array(
                    'file' => 'inc/features/legal-pages-generator/legal-pages-generator.php',
                    'class' => 'AIAutoTool\\Features\\LegalPagesGenerator\\LegalPagesGenerator',
                    'option' => 'AIautotool_legal_pages_active',
                    'load_on_admin' => true
                ),
            );
            
            // Batch load features
            foreach ($features_to_load as $feature) {
                $is_active = $use_cache ? AIAutoTool_OptionCache::is_active($feature['option']) :
                            (get_option($feature['option'], 'false') === 'true');
                
                // Load if active OR if on admin page and feature should load on admin
                if ($is_active || ($is_admin_page && $feature['load_on_admin'])) {
                    $file_path = plugin_dir_path(__FILE__) . $feature['file'];
                    $file_exists = $use_cache ? AIAutoTool_OptionCache::file_exists($file_path) : file_exists($file_path);
                    
                    if ($file_exists) {
                        // Ensure rendersetting is loaded for legacy features
                        if ($feature['class'] === 'AIAutoTool_AutoCreateCategories' && !class_exists('rendersetting')) {
                            $rendersetting_file = plugin_dir_path(__FILE__) . 'lib/setting.php';
                            if (file_exists($rendersetting_file)) {
                                require_once $rendersetting_file;
                            }
                        }
                        
                        require_once $file_path;
                        if (class_exists($feature['class'])) {
                            try {
                                $instance = new $feature['class']();
                                $this->addSubclass($instance);
                                
                                if (defined('WP_DEBUG') && WP_DEBUG && $feature['class'] === 'AIAutoTool_AutoCreateCategories') {
                                    error_log('AI Auto Tool: Successfully loaded AIAutoTool_AutoCreateCategories');
                                }
                            } catch (Exception $e) {
                                if (defined('WP_DEBUG') && WP_DEBUG) {
                                    error_log('AI Auto Tool: Failed to load ' . $feature['class'] . ': ' . $e->getMessage());
                                    error_log('AI Auto Tool: Exception trace: ' . $e->getTraceAsString());
                                }
                            } catch (Error $e) {
                                if (defined('WP_DEBUG') && WP_DEBUG) {
                                    error_log('AI Auto Tool: Fatal error loading ' . $feature['class'] . ': ' . $e->getMessage());
                                    error_log('AI Auto Tool: Error trace: ' . $e->getTraceAsString());
                                }
                            }
                        } else {
                            if (defined('WP_DEBUG') && WP_DEBUG && $feature['class'] === 'AIAutoTool_AutoCreateCategories') {
                                error_log('AI Auto Tool: Class AIAutoTool_AutoCreateCategories not found after requiring file: ' . $file_path);
                                error_log('AI Auto Tool: rendersetting class exists: ' . (class_exists('rendersetting') ? 'YES' : 'NO'));
                            }
                        }
                    } else {
                        if (defined('WP_DEBUG') && WP_DEBUG && $feature['class'] === 'AIAutoTool_AutoCreateCategories') {
                            error_log('AI Auto Tool: File not found: ' . $file_path);
                        }
                    }
                } else {
                    if (defined('WP_DEBUG') && WP_DEBUG && $feature['class'] === 'AIAutoTool_AutoCreateCategories') {
                        error_log('AI Auto Tool: Feature not loaded - is_active: ' . ($is_active ? 'YES' : 'NO') . ', is_admin_page: ' . ($is_admin_page ? 'YES' : 'NO') . ', load_on_admin: ' . ($feature['load_on_admin'] ? 'YES' : 'NO'));
                    }
                }
            }
            
            // Prompt Customer - Load in admin to register AJAX actions
            if ($is_admin_page) {
                $prompt_customer_file = plugin_dir_path(__FILE__) . 'inc/features/prompt-customer/AI_AutoTool_Prompt_Customer.php';
                if (file_exists($prompt_customer_file)) {
                    require_once $prompt_customer_file;
                    if (class_exists('AI_AutoTool_Prompt_Customer')) {
                        $AI_AutoTool_Prompt_Customer = new AI_AutoTool_Prompt_Customer();
                        $this->addSubclass($AI_AutoTool_Prompt_Customer);
                    }
                }
            }
            
            // Legacy class loading - only if classes already exist (loaded by features above)
            if (class_exists('URL_Scanner_Plugin')) {
                $url_scanner_plugin = new URL_Scanner_Plugin();
                $this->addSubclass($url_scanner_plugin);
            }
           
            if (class_exists('AIAutoToolsinglepost')) {
                $AIAutoToolsinglepost = new AIAutoToolsinglepost();
                $this->addSubclass($AIAutoToolsinglepost);
            }
            
            if (class_exists('AIautotool_autocomment')) {
                $AIautotool_autocomment = new AIautotool_autocomment();
                $this->addSubclass($AIautotool_autocomment);
            }

            if (class_exists('AIautotool_autocreatetags')) {
                $AIautotool_autocreatetags = new AIautotool_autocreatetags();
                $this->addSubclass($AIautotool_autocreatetags);
            }

            if (class_exists('AI_AutoTool_cloudflareAI')) {
                $AI_AutoTool_cloudflareAI = new AI_AutoTool_cloudflareAI();
                $this->addSubclass($AI_AutoTool_cloudflareAI);
            }
            
            // Note: auto-create-categories feature is now loaded via $features_to_load array above

            if (class_exists('AI_AutoTool_suggetauto')) {
                $AI_AutoTool_suggetauto = new AI_AutoTool_suggetauto();
                $this->addSubclass($AI_AutoTool_suggetauto);
            }

            if (class_exists('AIautotool_Prompt_CPT')) {
                $AIautotool_Prompt_CPT = new AIautotool_Prompt_CPT();
                $this->addSubclass($AIautotool_Prompt_CPT);
            }

            if (class_exists('AIAutoTool_SubmitIndex')) {
                $AIAutoTool_SubmitIndex = new AIAutoTool_SubmitIndex();
                $this->addSubclass($AIAutoTool_SubmitIndex);
            }

            if (class_exists('Aiautotool_exLink_backlink')) {
                $Aiautotool_exLink_backlink = new Aiautotool_exLink_backlink();
                $this->addSubclass($Aiautotool_exLink_backlink);
            }
            
            if (class_exists('Aiautotool_Canonical')) {
                $Aiautotool_Canonical = new Aiautotool_Canonical();
                $this->addSubclass($Aiautotool_Canonical);
            }

            if (class_exists('AIautotool_Web_Stories')) {
                $AIautotool_Web_Stories = new AIautotool_Web_Stories();
                $this->addSubclass($AIautotool_Web_Stories);
            }

            // if (class_exists('AIAutoToolSecurityBot')) {
            //     $AIAutoToolSecurityBot = new AIAutoToolSecurityBot();
            //     $this->addSubclass($AIAutoToolSecurityBot);
            // }
            
            // if (class_exists('AIAutotool_Image404Scanner')) {
            //     $AIAutotool_Image404Scanner = new AIAutotool_Image404Scanner();
            //     $this->addSubclass($AIAutotool_Image404Scanner);
            // }

            // if (class_exists('aiautotool_AutoGenerateUsername')) {
            //     $aiautotool_AutoGenerateUsername = new aiautotool_AutoGenerateUsername();
            //     $this->addSubclass($aiautotool_AutoGenerateUsername);
            // }
            
            // if (class_exists('Post_Exporter_Plugin')) {
            //     $Post_Exporter_Plugin = new Post_Exporter_Plugin();
            //     $this->addSubclass($Post_Exporter_Plugin);
            // }
            // if (class_exists('Auto_Install_Plugins')) {
            //     $Auto_Install_Plugins = new Auto_Install_Plugins();
            //     $this->addSubclass($Auto_Install_Plugins);
            // }

            // if (class_exists('Keyword_Autopost_Plugin')) {
            //     $Keyword_Autopost_Plugin = new Keyword_Autopost_Plugin();
            //     $this->addSubclass($Keyword_Autopost_Plugin);
            // }

            // if (class_exists('Aiautotool_Footer_Base')) {
            //     $Aiautotool_Footer_Base = new Aiautotool_Footer_Base();
            //     $this->addSubclass($Aiautotool_Footer_Base);
            // }
            // if (class_exists('activeCallBack')) {
            //     $activeCallBack = new activeCallBack();
            // }
            // if (class_exists('AI_AutoTool_Prompt_Customer')) {
            //     $AI_AutoTool_Prompt_Customer = new AI_AutoTool_Prompt_Customer();
            // }

            $new_object_loaded = true;
        }
        public function show_render_setting() {
           
                foreach ($this->subclasses as $subclass) {
                // Skip AI_AutoTool_Prompt_Customer as it's rendered separately in #set-prompt tab
                if (get_class($subclass) === 'AI_AutoTool_Prompt_Customer') {
                    continue;
                }
                
                if (method_exists($subclass, 'render_setting')) {
                    $subclass->render_setting();
                }
            }
        }
        public function show_render_tab_setting() {
           
                foreach ($this->subclasses as $subclass) {
                if (method_exists($subclass, 'render_tab_setting')) {
                    $subclass->render_tab_setting();
                }
            }
        }

        public function render_list_feature(){
            // Load only active features from inc directory
            $this->load_all_inc_features();
            
            // Render all features (both active and inactive) using AutoToolBox
            $this->render_all_features();
        }
        
        /**
         * Force load feature descriptions file
         */
        private function ensure_feature_descriptions_loaded() {
            if (!class_exists('AiAutoTool_Feature_Descriptions')) {
                $file_path = plugin_dir_path(__FILE__) . 'lib/feature-descriptions.php';
                if (file_exists($file_path)) {
                    require_once $file_path;
                    error_log('AI Auto Tool Debug: Force loaded feature-descriptions.php');
                } else {
                    error_log('AI Auto Tool Debug: feature-descriptions.php not found at: ' . $file_path);
                }
            }
        }
        
        /**
         * Get feature description with fallback
         * 
         * @param string $feature_key Feature key
         * @param string $fallback_description Fallback description if class not available
         * @return string Description HTML
         */
        private function get_feature_description($feature_key, $fallback_description = '') {
            // Ensure feature descriptions are loaded
            $this->ensure_feature_descriptions_loaded();
            
            // Debug: Check if class exists
            if (!class_exists('AiAutoTool_Feature_Descriptions')) {
                error_log('AI Auto Tool Debug: AiAutoTool_Feature_Descriptions class not found for feature: ' . $feature_key);
                return $fallback_description;
            }
            
            $description = AiAutoTool_Feature_Descriptions::get_truncated_description($feature_key);
            if (empty($description)) {
                error_log('AI Auto Tool Debug: Empty description for feature: ' . $feature_key);
                return $fallback_description;
            }
            
            return $description;
        }
        
        /**
         * Render all features (active and inactive) using AutoToolBox
         * IMPORTANT FUNCTION: This method controls feature rendering in the main UI.
         */
        private function render_all_features() {
            // Static flag to prevent multiple renders
            static $features_rendered = false;
            if ($features_rendered) {
                return;
            }
            
            // Cache for option values to prevent repeated database queries
            static $option_cache = array();
            
            $inc_dir = plugin_dir_path(__FILE__) . 'inc/';
            
            // Get feature registry from centralized method
            $feature_files = $this->get_feature_registry();
            
            // Separate features into active and disabled arrays
            $active_features = array();
            $disabled_features = array();
            
            foreach ($feature_files as $file => $config) {
                $file_path = $inc_dir . $file;
                $class_name = $config['class'];
                $active_option = $config['active_option'];
                $title = $config['title'];
                $description = $config['description'];
                $icon = $config['icon'];
                
                // Check if file exists
                if (file_exists($file_path)) {
                    // Check if feature is active - use cache to prevent repeated DB queries
                    // Special handling: Cron Status Widget is always active
                    if ($active_option === 'AIAutoTool_Cron_Status_Widget_active') {
                        $is_active = 'true'; // Always active, cannot be disabled
                        // Ensure option is set to true
                        if (!isset($option_cache[$active_option])) {
                            update_option($active_option, 'true');
                            $option_cache[$active_option] = 'true';
                        }
                    } else {
                        if (!isset($option_cache[$active_option])) {
                            // Try to get option with original name first
                            $is_active = get_option($active_option, false);
                            
                            // If not found, try normalized name (in case it was saved with normalized name)
                            if ($is_active === false) {
                                $normalized_option = $active_option;
                                if (strpos($normalized_option, 'aiautotool_') !== 0) {
                                    $normalized_option = 'aiautotool_' . $normalized_option;
                                }
                                $is_active = get_option($normalized_option, false);
                                
                                // If found with normalized name, migrate it to original name for consistency
                                if ($is_active !== false) {
                                    update_option($active_option, $is_active, false);
                                    delete_option($normalized_option);
                                }
                            }
                            
                            // Also try using FeatureOptions if available
                            if ($is_active === false && class_exists('AIAutoTool_FeatureOptions')) {
                                $is_active = AIAutoTool_FeatureOptions::get_option($active_option, false);
                            }
                            
                            $option_cache[$active_option] = $is_active;
                        }
                        $is_active = $option_cache[$active_option];
                    }
                    $active = ($is_active === 'true' || $is_active === true);
                    
                    // Store feature data for later rendering
                    $feature_data = array(
                        'file' => $file,
                        'config' => $config,
                        'active' => $active,
                        'active_option' => $active_option
                    );
                    
                    // Categorize feature
                    if ($active) {
                        $active_features[] = $feature_data;
                    } else {
                        $disabled_features[] = $feature_data;
                    }
                }
            }
            
            // Render Active Features Section
            if (!empty($active_features)) {
                echo '<div class="aiautotool-features-section aiautotool-features-active">';
                echo '<div class="aiautotool-section-header">';
                echo '<h2>';
                echo '<i class="fa-solid fa-check-circle"></i>';
                echo __('Active Features', 'ai-auto-tool');
                echo ' <span class="feature-count">' . count($active_features) . '</span>';
                echo '</h2>';
                echo '<p>' . __('These features are currently enabled and active on your site', 'ai-auto-tool') . '</p>';
                echo '</div>';
                echo '<div class="aiautotool-features-grid">';
                
                foreach ($active_features as $feature_data) {
                    $config = $feature_data['config'];
                    $active = $feature_data['active'];
                    $active_option = $feature_data['active_option'];
                    
                    // Create AutoToolBox for this feature
                    $box = new AutoToolBox(
                        $config['icon'] . ' ' . $config['title'],
                        $config['description'],
                        admin_url('admin.php?page=aiautotool_integrations'),
                        $active_option,
                        $active,
                        '',
                        false
                    );
                    
                    // Render the box
                    echo $box->generateHTML();
                }
                
                echo '</div>';
                echo '</div>';
            }
            
            // Render Disabled Features Section
            if (!empty($disabled_features)) {
                echo '<div class="aiautotool-features-section aiautotool-features-disabled">';
                echo '<div class="aiautotool-section-header">';
                echo '<h2>';
                echo '<i class="fa-solid fa-times-circle"></i>';
                echo __('Disabled Features', 'ai-auto-tool');
                echo ' <span class="feature-count">' . count($disabled_features) . '</span>';
                echo '</h2>';
                echo '<p>' . __('These features are currently disabled and not active on your site', 'ai-auto-tool') . '</p>';
                echo '</div>';
                echo '<div class="aiautotool-features-grid">';
                
                foreach ($disabled_features as $feature_data) {
                    $config = $feature_data['config'];
                    $active = $feature_data['active'];
                    $active_option = $feature_data['active_option'];
                    
                    // Create AutoToolBox for this feature
                    $box = new AutoToolBox(
                        $config['icon'] . ' ' . $config['title'],
                        $config['description'],
                        admin_url('admin.php?page=aiautotool_integrations'),
                        $active_option,
                        $active,
                        '',
                        false
                    );
                    
                    // Render the box
                    echo $box->generateHTML();
                }
                
                echo '</div>';
                echo '</div>';
            }
            
            $features_rendered = true;
        }

        /**
         * Get feature registry - extracted from render_all_features for reuse
         * 
         * @return array Feature registry with file paths and metadata
         */
        private function get_feature_registry() {
            return array(
                'features/canonical-manager/canonical-manager.php' => array(
                    'class' => 'AIAutoTool\Features\CanonicalManager\CanonicalManager',
                    'active_option' => 'Aiautotool_Canonical_active',
                    'title' => 'Canonical Manager',
                    'description' => $this->get_feature_description('canonical', 'Manage canonical URLs and redirects'),
                    'icon' => '<i class="fa-solid fa-link"></i>'
                ),
                'features/external-links/external-links.php' => array(
                    'class' => 'AIAutoTool\Features\ExternalLinks\ExternalLinks',
                    'active_option' => 'Aiautotool_exLink_backlink_active',
                    'title' => 'Auto External Links',
                    'description' => $this->get_feature_description('microsite_backlink', 'Automatically manage external links and backlinks'),
                    'icon' => '<i class="fa-solid fa-external-link-alt"></i>'
                ),
                'features/submit-index/submit-index.php' => array(
                    'class' => 'AIAutoTool\Features\SubmitIndex\SubmitIndex',
                    'active_option' => 'AIAutoTool_SubmitIndex_active',
                    'title' => 'Submit Index',
                    'description' => $this->get_feature_description('google_index', 'Submit URLs to search engines for indexing'),
                    'icon' => '<i class="fa-solid fa-paper-plane"></i>'
                ),
                'features/post-exporter/post-exporter.php' => array(
                    'class' => 'AIAutoTool\Features\PostExporter\PostExporter',
                    'active_option' => 'Post_Exporter_Plugin_active',
                    'title' => 'Post Exporter',
                    'description' => $this->get_feature_description('post_exporter', 'Export posts and content to various formats'),
                    'icon' => '<i class="fa-solid fa-download"></i>'
                ),
                'features/keyword-autopost/keyword-autopost.php' => array(
                    'class' => 'AIAutoTool\Features\KeywordAutopost\KeywordAutopost',
                    'active_option' => 'aiautotool_keyword_autopost_active',
                    'title' => 'Keyword Autopost',
                    'description' => $this->get_feature_description('keyword_autopost', 'Automatically create posts based on keywords'),
                    'icon' => '<i class="fa-solid fa-key"></i>'
                ),
                'features/single-ai/single-ai.php' => array(
                    'class' => 'AIAutoTool\Features\SingleAI\SingleAI',
                    'active_option' => 'AIAutoToolsinglepost_active',
                    'title' => 'Single AI Post',
                    'description' => $this->get_feature_description('single_ai', 'Create individual AI-powered posts'),
                    'icon' => '<i class="fa-solid fa-file-alt"></i>'
                ),
                'features/image-scanner/image-scanner.php' => array(
                    'class' => 'AIAutoTool\Features\ImageScanner\ImageScanner',
                    'active_option' => 'AIAutotool_Image404Scanner_active',
                    'title' => 'Image Scanner',
                    'description' => $this->get_feature_description('thumb_fix', 'Scan and manage images for 404 errors'),
                    'icon' => '<i class="fa-solid fa-image"></i>'
                ),
                'features/auto-comment/auto-comment.php' => array(
                    'class' => 'AIAutoTool\Features\AutoComment\AutoComment',
                    'active_option' => 'AIautotool_autocomment_active',
                    'title' => 'Auto Comment',
                    'description' => $this->get_feature_description('auto_comment', 'Automatically generate and post comments using AI'),
                    'icon' => '<i class="fa-solid fa-comments"></i>'
                ),
                'features/auto-create-tags/auto-create-tags.php' => array(
                    'class' => 'AIAutoTool\Features\AutoCreateTags\AutoCreateTags',
                    'active_option' => 'AIautotool_autocreatetags_active',
                    'title' => 'Auto Create Tags',
                    'description' => $this->get_feature_description('auto_create_tag', 'Automatically create and assign tags to posts'),
                    'icon' => '<i class="fa-solid fa-tags"></i>'
                ),
                'features/cloudflare-ai/cloudflare-ai.php' => array(
                    'class' => 'AIAutoTool\Features\CloudflareAI\CloudflareAI',
                    'active_option' => 'AI_AutoTool_cloudflareAI_active',
                    'title' => 'Cloudflare AI',
                    'description' => $this->get_feature_description('cloudflare_ai', 'Integrate with Cloudflare AI services'),
                    'icon' => '<i class="fa-solid fa-cloud"></i>'
                ),
                'features/suggest-auto/suggest-auto.php' => array(
                    'class' => 'AIAutoTool\Features\SuggestAuto\SuggestAuto',
                    'active_option' => 'AI_AutoTool_suggetauto_active',
                    'title' => 'Suggest Auto',
                    'description' => $this->get_feature_description('suggest_auto', 'Automatically suggest content improvements'),
                    'icon' => '<i class="fa-solid fa-lightbulb"></i>'
                ),
                'features/prompt-cpt/prompt-cpt.php' => array(
                    'class' => 'AIAutoTool\Features\PromptCPT\PromptCPT',
                    'active_option' => 'AIautotool_Prompt_CPT_active',
                    'title' => 'Prompt CPT',
                    'description' => $this->get_feature_description('prompt_manager', 'Manage custom post types for prompts'),
                    'icon' => '<i class="fa-solid fa-clipboard"></i>'
                ),
                'features/prompt-customer/prompt-customer.php' => array(
                    'class' => 'AIAutoTool\Features\PromptCustomer\PromptCustomer',
                    'active_option' => 'AI_AutoTool_Prompt_Customer_active',
                    'title' => 'Prompt Customer',
                    'description' => $this->get_feature_description('prompt_customer', 'Customer prompt management and customization'),
                    'icon' => '<i class="fa-solid fa-user"></i>'
                ),
                'features/bot-security/bot-security.php' => array(
                    'class' => 'AIAutoTool\Features\BotSecurity\BotSecurity',
                    'active_option' => 'AIAutoToolSecurityBot_active',
                    'title' => 'Bot Security',
                    'description' => 'Security features for bot protection',
                    'icon' => '<i class="fa-solid fa-shield"></i>'
                ),
                'features/web-stories/web-stories.php' => array(
                    'class' => 'AIAutoTool\Features\WebStories\WebStories',
                    'active_option' => 'AIautotool_Web_Stories_active',
                    'title' => 'Web Stories',
                    'description' => $this->get_feature_description('webstories', 'Create and manage web stories'),
                    'icon' => '<i class="fa-solid fa-book-open"></i>'
                ),
                'features/install-plugin/install-plugin.php' => array(
                    'class' => 'AIAutoTool\Features\InstallPlugin\InstallPlugin',
                    'active_option' => 'Aiautotool_Install_Plugin_active',
                    'title' => 'Install Plugin',
                    'description' => $this->get_feature_description('install_plugin', 'Install and manage plugins'),
                    'icon' => '<i class="fa-solid fa-download"></i>'
                ),
                'features/footer-base/footer-base.php' => array(
                    'class' => 'AIAutoTool\Features\FooterBase\FooterBase',
                    'active_option' => 'Aiautotool_footer_base_active',
                    'title' => 'Footer Base',
                    'description' => 'Manage footer content and base elements',
                    'icon' => '<i class="fa-solid fa-window-maximize"></i>'
                ),
                'features/username-generator/username-generator.php' => array(
                    'class' => 'AIAutoTool\Features\UsernameGenerator\UsernameGenerator',
                    'active_option' => 'aiautotool_AutoGenerateUsername_active',
                    'title' => 'Auto Generate Username',
                    'description' => 'Automatically generate usernames',
                    'icon' => '<i class="fa-solid fa-user-plus"></i>'
                ),
                'features/autoblogging/autoblogging.php' => array(
                    'class' => 'AIAutoTool\Features\Autoblogging\Autoblogging',
                    'active_option' => 'AI_AutoTool_Autoblogging_active',
                    'title' => 'Autoblogging',
                    'description' => 'Automatically create blog posts from URLs using AI',
                    'icon' => '<i class="fa-solid fa-blog"></i>'
                ),
                'features/auto-create-categories/auto-create-categories.php' => array(
                    'class' => 'AIAutoTool_AutoCreateCategories',
                    'active_option' => 'AIAutoTool_auto_create_categories_active',
                    'title' => 'Auto Create Categories',
                    'description' => $this->get_feature_description('auto_create_categories', 'Tá»± Ä‘á»™ng táº¡o danh sÃ¡ch category dáº¡ng cÃ¢y thÆ° má»¥c báº±ng AI, tá»‘i Æ°u SEO theo topic cluster'),
                    'icon' => '<i class="fa-solid fa-sitemap"></i>'
                ),
                'features/crawlcampaign/crawlcampaign.php' => array(
                    'class' => 'AIAutoTool\Features\CrawlCampaign\CrawlCampaign',
                    'active_option' => 'aiautotool_crawlcampaign_active',
                    'title' => 'Crawl Campaign',
                    'description' => $this->get_feature_description('crawlcampaign', 'Tá»± Ä‘á»™ng crawl vÃ  Ä‘Äƒng bÃ i tá»« nhiá»u nguá»“n (RSS, YouTube, Amazon, Facebook, Instagram, Twitter, etc.)'),
                    'icon' => '<i class="fa-solid fa-spider"></i>'
                ),
                'features/legal-pages-generator/legal-pages-generator.php' => array(
                    'class' => 'AIAutoTool\Features\LegalPagesGenerator\LegalPagesGenerator',
                    'active_option' => 'AIautotool_legal_pages_active',
                    'title' => 'Legal Pages Generator',
                    'description' => $this->get_feature_description('legal_pages_generator', 'Automatically generate essential legal pages (About Us, Terms of Service, Privacy Policy, Disclaimer, Contact Us) using AI'),
                    'icon' => '<i class="fa-solid fa-file-contract"></i>'
                ),
            );
        }
        
        /**
         * Register AJAX handlers for all features
         */
        public function register_all_ajax_handlers() {
            $active_options = array(
                'Aiautotool_Canonical_active',
                'Aiautotool_exLink_backlink_active',
                'AIAutoTool_SubmitIndex_active',
                'Post_Exporter_Plugin_active',
                'aiautotool_keyword_autopost_active',
                'AIAutoToolsinglepost_active',
                'AIAutotool_Image404Scanner_active',
                'AIautotool_autocomment_active',
                'AIautotool_autocreatetags_active',
                'AI_AutoTool_cloudflareAI_active',
                'AI_AutoTool_suggetauto_active',
                'AIautotool_Prompt_CPT_active',
                'AI_AutoTool_Prompt_Customer_active', // Add Prompt Customer
                // Temporarily disabled due to HTML output issues
                'AIAutoToolSecurityBot_active',
                'AIautotool_Web_Stories_active',
                'Aiautotool_Install_Plugin_active',
                'AI_AutoTool_Autoblogging_active',
                'AIAutoTool_Cron_Status_Widget_active',
                'aiautotool_AutoGenerateUsername_active',
                'Aiautotool_footer_base_active',
                'AIAutoTool_auto_create_categories_active',
                'aiautotool_crawlcampaign_active',
                'AIautotool_legal_pages_active',
            );
            
            foreach ($active_options as $option) {
                // Register AJAX handler for this option (requires authentication - options modification)
                add_action('wp_ajax_update_active_option_canonical_' . $option, array($this, 'generic_update_active_option_callback'));
            }
        }
        
        /**
         * Generic AJAX callback for updating active options
         */
        public function generic_update_active_option_callback() {
            check_ajax_referer('aiautotool_nonce', 'security');
            
            if (!current_user_can('manage_options')) {
                wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')), 403);
                return;
            }
            
            if (isset($_POST['active'])) {
                $active = sanitize_text_field($_POST['active']);
                
                // Get the option name from the action hook
                $action = current_action();
                $option_name = str_replace('wp_ajax_update_active_option_canonical_', '', $action);
                
                // Update the option
                update_option($option_name, $active, null, 'no');
                
                // Return the new value
                wp_send_json_success(array('active' => $active, 'option' => $option_name));
            }
            
            wp_send_json_error('Invalid request');
        }

        /**
         * Unified AJAX callback for enabling/disabling integration features.
         */
        public function aiautotool_toggle_feature_callback() {
            if (!current_user_can('manage_options')) {
                wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')), 403);
                return;
            }

            if (!isset($_POST['security']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['security'])), 'aiautotool_nonce')) {
                wp_send_json_error(array('message' => __('Security check failed. Please refresh the page and try again.', 'ai-auto-tool')), 403);
                return;
            }

            $option_name = isset($_POST['option']) ? sanitize_text_field(wp_unslash($_POST['option'])) : '';
            $active = isset($_POST['active']) ? sanitize_text_field(wp_unslash($_POST['active'])) : '';

            if ($option_name === '' || !in_array($active, array('true', 'false'), true)) {
                wp_send_json_error(array('message' => __('Invalid request data.', 'ai-auto-tool')), 400);
                return;
            }

            $allowed_options = array(
                'Aiautotool_Canonical_active',
                'Aiautotool_exLink_backlink_active',
                'AIAutoTool_SubmitIndex_active',
                'Post_Exporter_Plugin_active',
                'aiautotool_keyword_autopost_active',
                'AIAutoToolsinglepost_active',
                'AIAutotool_Image404Scanner_active',
                'AIautotool_autocomment_active',
                'AIautotool_autocreatetags_active',
                'AI_AutoTool_cloudflareAI_active',
                'AI_AutoTool_suggetauto_active',
                'AIautotool_Prompt_CPT_active',
                'AI_AutoTool_Prompt_Customer_active',
                'AIAutoToolSecurityBot_active',
                'AIautotool_Web_Stories_active',
                'Aiautotool_Install_Plugin_active',
                'AI_AutoTool_Autoblogging_active',
                'AIAutoTool_Cron_Status_Widget_active',
                'aiautotool_AutoGenerateUsername_active',
                'Aiautotool_footer_base_active',
                'AIAutoTool_auto_create_categories_active',
                'aiautotool_crawlcampaign_active',
                'AIautotool_legal_pages_active',
            );

            if (!in_array($option_name, $allowed_options, true)) {
                wp_send_json_error(array('message' => __('Unknown feature option.', 'ai-auto-tool')), 400);
                return;
            }

            update_option($option_name, $active, false);

            wp_send_json_success(array(
                'message' => __('Feature updated successfully.', 'ai-auto-tool'),
                'option' => $option_name,
                'active' => $active,
            ));
        }
        
        /**
         * Táº£i táº¥t cáº£ cÃ¡c tÃ­nh nÄƒng tá»« thÆ° má»¥c inc vÃ  thÃªm vÃ o subclasses
         * Chá»‰ táº£i cÃ¡c tÃ­nh nÄƒng Ä‘ang Ä‘Æ°á»£c kÃ­ch hoáº¡t
         * CHáº¾ Äá»˜ DEBUG: Bao gá»“m tá»«ng tá»‡p má»™t Ä‘á»ƒ theo dÃµi má»©c sá»­ dá»¥ng bá»™ nhá»›
         */
        private function load_all_inc_features() {
            // Static flag to prevent multiple loads
            static $features_loaded = false;
            if ($features_loaded) {
                return;
            }
            
            // DEBUG MODE: Enable to track memory usage per file
            $debug_mode = defined('WP_DEBUG') && WP_DEBUG && (defined('AIAUTOTOOL_DEBUG_LOADING') && AIAUTOTOOL_DEBUG_LOADING);
            
            // Cache for option values to prevent repeated database queries
            static $option_cache = array();
            
            $inc_dir = plugin_dir_path(__FILE__) . 'inc/';
            
            // Define all feature files, their classes, and active option names
            $feature_files = array(
                'features/canonical-manager/canonical-manager.php' => array(
                    'class' => 'AIAutoTool\Features\CanonicalManager\CanonicalManager',
                    'active_option' => 'Aiautotool_Canonical_active'
                ),
                'features/external-links/external-links.php' => array(
                    'class' => 'AIAutoTool\Features\ExternalLinks\ExternalLinks',
                    'active_option' => 'Aiautotool_exLink_backlink_active'
                ),
                'features/submit-index/submit-index.php' => array(
                    'class' => 'AIAutoTool\Features\SubmitIndex\SubmitIndex',
                    'active_option' => 'AIAutoTool_SubmitIndex_active'
                ),
                'features/post-exporter/post-exporter.php' => array(
                    'class' => 'AIAutoTool\Features\PostExporter\PostExporter',
                    'active_option' => 'Post_Exporter_Plugin_active'
                ),
                'features/keyword-autopost/keyword-autopost.php' => array(
                    'class' => 'AIAutoTool\Features\KeywordAutopost\KeywordAutopost',
                    'active_option' => 'aiautotool_keyword_autopost_active'
                ),
                'features/single-ai/single-ai.php' => array(
                    'class' => 'AIAutoTool\Features\SingleAI\SingleAI',
                    'active_option' => 'AIAutoToolsinglepost_active'
                ),
                'features/image-scanner/image-scanner.php' => array(
                    'class' => 'AIAutoTool\Features\ImageScanner\ImageScanner',
                    'active_option' => 'AIAutotool_Image404Scanner_active'
                ),
                'features/auto-comment/auto-comment.php' => array(
                    'class' => 'AIAutoTool\Features\AutoComment\AutoComment',
                    'active_option' => 'AIautotool_autocomment_active'
                ),
                'features/auto-create-tags/auto-create-tags.php' => array(
                    'class' => 'AIAutoTool\Features\AutoCreateTags\AutoCreateTags',
                    'active_option' => 'AIautotool_autocreatetags_active'
                ),
                'features/cloudflare-ai/cloudflare-ai.php' => array(
                    'class' => 'AIAutoTool\Features\CloudflareAI\CloudflareAI',
                    'active_option' => 'AI_AutoTool_cloudflareAI_active'
                ),
                'features/suggest-auto/suggest-auto.php' => array(
                    'class' => 'AIAutoTool\Features\SuggestAuto\SuggestAuto',
                    'active_option' => 'AI_AutoTool_suggetauto_active'
                ),
                'features/prompt-cpt/prompt-cpt.php' => array(
                    'class' => 'AIAutoTool\Features\PromptCPT\PromptCPT',
                    'active_option' => 'AIautotool_Prompt_CPT_active'
                ),
               
                // Temporarily disabled due to HTML output issues
                'features/bot-security/bot-security.php' => array(
                    'class' => 'AIAutoTool\Features\BotSecurity\BotSecurity',
                    'active_option' => 'AIAutoToolSecurityBot_active'
                ),
                'features/web-stories/web-stories.php' => array(
                    'class' => 'AIAutoTool\Features\WebStories\WebStories',
                    'active_option' => 'AIautotool_Web_Stories_active'
                ),
                'features/install-plugin/install-plugin.php' => array(
                    'class' => 'AIAutoTool\Features\InstallPlugin\InstallPlugin',
                    'active_option' => 'Aiautotool_Install_Plugin_active'
                ),
                'features/footer-base/footer-base.php' => array(
                    'class' => 'AIAutoTool\Features\FooterBase\FooterBase',
                    'active_option' => 'Aiautotool_footer_base_active'
                ),
                'features/username-generator/username-generator.php' => array(
                    'class' => 'AIAutoTool\Features\UsernameGenerator\UsernameGenerator',
                    'active_option' => 'aiautotool_AutoGenerateUsername_active'
                ),
                'features/autoblogging/autoblogging.php' => array(
                    'class' => 'AIAutoTool\Features\Autoblogging\Autoblogging',
                    'active_option' => 'AI_AutoTool_Autoblogging_active'
                ),
                'features/legal-pages-generator/legal-pages-generator.php' => array(
                    'class' => 'AIAutoTool\Features\LegalPagesGenerator\LegalPagesGenerator',
                    'active_option' => 'AIautotool_legal_pages_active'
                ),
                
            );

            
            foreach ($feature_files as $file => $config) {
                $file_path = $inc_dir . $file;
                $class_name = $config['class'];
                $active_option = $config['active_option'];
                
                // Check if file exists and class is not already loaded
                if (file_exists($file_path) && !$this->is_class_loaded($class_name)) {
                    
                    // Check if feature is active - use cache to prevent repeated DB queries
                    // Special handling: Cron Status Widget is always active
                    if ($active_option === 'AIAutoTool_Cron_Status_Widget_active') {
                        $is_active = 'true'; // Always active, cannot be disabled
                        // Ensure option is set to true
                        if (!isset($option_cache[$active_option])) {
                            update_option($active_option, 'true');
                            $option_cache[$active_option] = 'true';
                        }
                    } else {
                        if (!isset($option_cache[$active_option])) {
                            $option_cache[$active_option] = get_option($active_option, false);
                        }
                        $is_active = $option_cache[$active_option];
                    }
                    
                    // CRITICAL: Don't instantiate classes here to prevent memory exhaustion
                    // Classes will be loaded on-demand when actually needed
                    // Only require the file to ensure it's available if needed
                    if (!class_exists($class_name)) {
                        // DEBUG MODE: Track memory before and after loading each file
                        if ($debug_mode) {
                            $memory_before = memory_get_usage(true);
                            $memory_before_mb = round($memory_before / 1024 / 1024, 2);
                            error_log("AI Auto Tool DEBUG: [BEFORE] Loading {$file} - Memory: {$memory_before_mb} MB");
                        }
                        
                        try {
                            require_once $file_path;
                            
                            if ($debug_mode) {
                                $memory_after = memory_get_usage(true);
                                $memory_after_mb = round($memory_after / 1024 / 1024, 2);
                                $memory_diff = round(($memory_after - $memory_before) / 1024 / 1024, 2);
                                $peak_memory = round(memory_get_peak_usage(true) / 1024 / 1024, 2);
                                
                                error_log("AI Auto Tool DEBUG: [AFTER] Loaded {$file} - Memory: {$memory_after_mb} MB (+{$memory_diff} MB) - Peak: {$peak_memory} MB");
                                
                                // Warn if memory increase is significant
                                if ($memory_diff > 5) {
                                    error_log("AI Auto Tool DEBUG: âš ï¸ WARNING - {$file} increased memory by {$memory_diff} MB!");
                                }
                            }
                        } catch (Exception $e) {
                            if ($debug_mode) {
                                error_log("AI Auto Tool DEBUG: âŒ ERROR loading {$file}: " . $e->getMessage());
                            }
                            // Continue with next file even if one fails
                            continue;
                        } catch (Error $e) {
                            if ($debug_mode) {
                                error_log("AI Auto Tool DEBUG: âŒ FATAL ERROR loading {$file}: " . $e->getMessage());
                            }
                            // Continue with next file even if one fails
                            continue;
                        }
                    }
                    
                    // DO NOT instantiate here - causes memory exhaustion
                    // Instantiation will happen on-demand via render_all_features() or when feature is actually used
                    /*
                    // Check if class exists
                    if (class_exists($class_name)) {
                        try {
                            $instance = new $class_name();
                            
                            // Only add classes that have render_feature method
                            if (method_exists($instance, 'render_feature')) {
                                $this->addSubclass($instance);
                                
                                if (defined('WP_DEBUG') && WP_DEBUG) {
                                    error_log("AI Auto Tool: Loaded feature {$class_name} from {$file} (active: " . ($is_active ? 'yes' : 'no') . ")");
                                }
                            } else {
                                if (defined('WP_DEBUG') && WP_DEBUG) {
                                    error_log("AI Auto Tool: Skipped {$class_name} - no render_feature method");
                                }
                            }
                        } catch (Exception $e) {
                            if (defined('WP_DEBUG') && WP_DEBUG) {
                                error_log("AI Auto Tool: Failed to load {$class_name}: " . $e->getMessage());
                            }
                        }
                    }
                    */
                }
            }
            
            if ($debug_mode) {
                $final_memory = round(memory_get_usage(true) / 1024 / 1024, 2);
                $peak_memory = round(memory_get_peak_usage(true) / 1024 / 1024, 2);
                error_log("AI Auto Tool DEBUG: [COMPLETE] load_all_inc_features() finished - Final Memory: {$final_memory} MB - Peak: {$peak_memory} MB");
            }
            
            $features_loaded = true;
        }
        
        /**
         * Check if a class is already loaded in subclasses
         */
        private function is_class_loaded($class_name) {
            foreach ($this->subclasses as $subclass) {
                if (get_class($subclass) === $class_name) {
                    return true;
                }
            }
            return false;
        }
        
        public function render_plan(){
            // Track rendered features by active_option_name to prevent duplicates
            $rendered_features = array();
            
            foreach ($this->subclasses as $subclass) {
                // Check if this feature has already been rendered
                if (isset($subclass->active_option_name)) {
                    $option_name = $subclass->active_option_name;
                    
                    // Skip if already rendered
                    if (isset($rendered_features[$option_name])) {
                        continue;
                    }
                    
                    // Mark as rendered
                    $rendered_features[$option_name] = true;
                }
                
                // Only render if feature is active
                if (method_exists($subclass, 'render_plan')) {
                    $subclass->render_plan();
                }
            }
        }
        
        /**
         * Initialize Professional Autoloader System
         */
        private function init_autoloader_system() {
            // Load Asset Manager FIRST (before other components that may need it)
            $asset_manager_file = plugin_dir_path(__FILE__) . 'inc/asset-manager.php';
            if (file_exists($asset_manager_file)) {
                require_once $asset_manager_file;
            }
            
            // Load autoloader components
            $autoloader_files = array(
                'inc/option-cache.php', // Load option cache FIRST for performance
                'inc/autoloader.php',
                'inc/class-mapper.php', 
                'inc/lazy-loader.php',
                'inc/optimized-loader.php',
                'inc/feature-options.php',
                'inc/feature-manager.php',
                'inc/migration.php',
                'inc/menu-manager.php', // Add menu manager
                'inc/class-duplicate-fixer.php', // Add duplicate fixer
                'inc/menu-test.php', // Add menu test
            );
            
            foreach ($autoloader_files as $file) {
                $file_path = plugin_dir_path(__FILE__) . $file;
                if (file_exists($file_path)) {
                    require_once $file_path;
                }
            }
            
            // Initialize autoloader
            if (class_exists('AIAutoTool_Autoloader')) {
                AIAutoTool_Autoloader::init();
            }
            
            // Initialize lazy loader
            if (class_exists('AIAutoTool_LazyLoader')) {
                AIAutoTool_LazyLoader::init();
            }
            
            // Initialize menu manager
            if (class_exists('AIAutoTool_MenuManager')) {
                AIAutoTool_MenuManager::init($this);
            }
            
            // Initialize feature options
            if (class_exists('AIAutoTool_FeatureOptions')) {
                AIAutoTool_FeatureOptions::init_default_options();
            }
            
            // Load AI Providers Module EARLY (MUST load before features that use AI)
            // This ensures AI Helper is always available when features need it
            $ai_providers_index = plugin_dir_path(__FILE__) . 'inc/ai-providers/index.php';
            if (file_exists($ai_providers_index)) {
                require_once $ai_providers_index;
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('AI Auto Tool: AI Providers module loaded');
                }
            }
            
            // CRITICAL FIX: Defer class map generation to avoid memory exhaustion
            // Only generate on-demand or via admin action, not during plugin initialization
            // This prevents recursive file scanning that can exhaust memory
            // The autoloader will work without the generated map using fallback methods
            /*
            if (class_exists('AIAutoTool_ClassMapper')) {
                AIAutoTool_ClassMapper::generateClassMap();
            }
            */
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('AI Auto Tool: Professional autoloader system initialized');
            }
        }
        
        /**
         * Legacy load_inc method - now uses lazy loading
         * @deprecated Use AIAutoTool_LazyLoader instead
         */
        public function load_inc() {
            // Load rendersetting class first (required by many inc classes)
            $rendersetting_file = plugin_dir_path(__FILE__) . 'lib/setting.php';
            if (file_exists($rendersetting_file)) {
                require_once $rendersetting_file;
            }
            
            // Load essential library classes (required by many inc classes)
            $essential_lib_files = array(
                'lib/aiautotool_notice_control.php', // aiautotool_Warning_Notice
                'lib/boxf.php', // AutoToolBox
                'lib/bard.php', // BardGenContent
                'lib/AIautotool_logall.php', // AIautoTool_log
                'lib/feature-descriptions.php', // AiAutoTool_Feature_Descriptions
            );
            
            foreach ($essential_lib_files as $file) {
                $file_path = plugin_dir_path(__FILE__) . $file;
                if (file_exists($file_path)) {
                    require_once $file_path;
                    // Debug: Log when feature-descriptions.php is loaded
                    if ($file === 'lib/feature-descriptions.php') {
                        error_log('AI Auto Tool Debug: feature-descriptions.php loaded successfully');
                        if (class_exists('AiAutoTool_Feature_Descriptions')) {
                            error_log('AI Auto Tool Debug: AiAutoTool_Feature_Descriptions class exists');
                        } else {
                            error_log('AI Auto Tool Debug: AiAutoTool_Feature_Descriptions class NOT found after loading file');
                        }
                    }
                } else {
                    // Debug: Log if file doesn't exist
                    if ($file === 'lib/feature-descriptions.php') {
                        error_log('AI Auto Tool Debug: feature-descriptions.php file not found at: ' . $file_path);
                    }
                }
            }
            
            // This method is now handled by the autoloader system
            // Only load core files immediately (excluding API manager - handled by autoloader)
            $core_files = array(
                'inc/canonical.php',
                'inc/ssl.php',
            );
            
            foreach ($core_files as $file) {
                $file_path = plugin_dir_path(__FILE__) . $file;
                if (file_exists($file_path)) {
                    require_once $file_path;
                }
            }
            
            // Load AI Providers Module (MUST load before features that use AI)
            $ai_providers_index = plugin_dir_path(__FILE__) . 'inc/ai-providers/index.php';
            if (file_exists($ai_providers_index)) {
                require_once $ai_providers_index;
            }
            
            // Load new Extra Config architecture
            $extra_config_bootstrap = plugin_dir_path(__FILE__) . 'inc/extra-config/bootstrap.php';
            if (file_exists($extra_config_bootstrap)) {
                require_once $extra_config_bootstrap;
            }
        }
        
        /**
         * Legacy load_lib method - now uses lazy loading
         * @deprecated Use AIAutoTool_LazyLoader instead
         * FIXED: Only load essential files, not all files in lib directory
         * This prevents memory exhaustion from loading unnecessary files
         */
        public function load_lib() {
            // Static flag to prevent multiple loads
            static $lib_loaded = false;
            if ($lib_loaded) {
                return;
            }
            
            // Only load essential library files that are required immediately
            // Other files will be loaded on-demand by the autoloader
            $essential_files = array(
                'lib/setting.php', // rendersetting class - required by many features
                'lib/aiautotool_notice_control.php', // aiautotool_Warning_Notice
            );
            
            foreach ($essential_files as $file) {
                $file_path = plugin_dir_path(__FILE__) . $file;
                if (file_exists($file_path)) {
                    require_once $file_path;
                }
            }
            
            $lib_loaded = true;
            
            // Note: Other lib files will be loaded on-demand via autoloader
            // This prevents loading all files upfront which causes memory issues
        }
        
        /**
         * Get autoloader statistics
         */
        public function get_autoloader_stats() {
            if (class_exists('AIAutoTool_Autoloader')) {
                return AIAutoTool_Autoloader::getStats();
            }
            return array();
        }
        
        /**
         * Get lazy loader statistics
         */
        public function get_lazy_loader_stats() {
            if (class_exists('AIAutoTool_LazyLoader')) {
                return AIAutoTool_LazyLoader::getStats();
            }
            return array();
        }
        
        /**
         * Force load feature
         */
        public function load_feature($feature_name) {
            if (class_exists('AIAutoTool_LazyLoader')) {
                return AIAutoTool_LazyLoader::loadFeature($feature_name);
            }
            return false;
        }
        
        /**
         * Check if feature is loaded
         */
        public function is_feature_loaded($feature_name) {
            if (class_exists('AIAutoTool_LazyLoader')) {
                return AIAutoTool_LazyLoader::isFeatureLoaded($feature_name);
            }
            return false;
        }
       /**
        * Legacy method for backward compatibility
        * Assets are now managed by Asset Manager (inc/asset-manager.php)
        * This method is kept for any external code that might call it directly
        * 
        * @deprecated Use AIAutoTool_Asset_Manager instead
        */
       public function add_aiautotool_button_js() {
        // Asset Manager now handles this, but we keep this method for backward compatibility
        // Assets will be loaded automatically by Asset Manager on plugin pages
        if (defined('WP_DEBUG') && WP_DEBUG) {
            error_log('AI Auto Tool: add_aiautotool_button_js() called (deprecated - use Asset Manager)');
        }
    }
    
    /**
     * Ensure scripts are loaded for integrations page
     * Assets are now handled by Asset Manager
     * 
     * @deprecated Use AIAutoTool_Asset_Manager instead
     */
    public function ensure_integrations_scripts($hook) {
        // Asset Manager now handles this automatically
        // This method is kept for backward compatibility
    }

        public function add_menu_page() {
            add_menu_page(
                '<i class="fa-solid fa-gears"></i> Ai Auto Tool',
                '<i class="fa-solid fa-gears"></i> Ai Auto Tool',
                'manage_options',
                MENUSUBPARRENT,
                array( $this, 'menu_page' ),
                'data:image/svg+xml;base64,' . base64_encode(file_get_contents(plugin_dir_path(__FILE__) . '/images/logo.svg')),
                3
            );

            add_submenu_page(
                MENUSUBPARRENT,  
                '<i class="fa-solid fa-wand-magic-sparkles"></i> Integrations', 
                '<i class="fa-solid fa-wand-magic-sparkles"></i> Integrations', 
                'manage_options',              
                'aiautotool_integrations',  
                array($this, 'aiautotool_integrations')  
            );

            
            
           
           
        }
        public function aiautotool_integrations(){
            // JS settings are now loaded via admin_enqueue_scripts hook
            // No need to call enqueue_integrations_page_assets() here
            ?>
            <style>
                
            </style>
            <div class="wrap aiautotool_container aiautotool-integrations-page">
                <div class="aiautotool-integrations-header">
                    <h1>
                        <i class="fa-solid fa-wand-magic-sparkles"></i>
                        <?php _e('AI Auto Tool - Integrations', 'ai-auto-tool'); ?>
                    </h1>
                    <p><?php _e('Manage and activate all available features and tools. Enable or disable features to optimize your site performance.', 'ai-auto-tool'); ?></p>
                </div>
                
                <div class="aiautotool_box_f_container">
                    <?php self::render_list_feature(); ?>
                </div>
                
                <div class="aiautotool-info-box">
                    <h3>
                        <i class="fa-solid fa-info-circle"></i>
                        <?php _e('How to Use', 'ai-auto-tool'); ?>
                    </h3>
                    <ul>
                        <li><?php _e('Click "Enable" to activate a feature', 'ai-auto-tool'); ?></li>
                        <li><?php _e('Click "Disable" to deactivate a feature', 'ai-auto-tool'); ?></li>
                        <li><?php _e('Active features will appear in the main menu', 'ai-auto-tool'); ?></li>
                        <li><?php _e('Inactive features will not load, saving memory and improving performance', 'ai-auto-tool'); ?></li>
                    </ul>
                </div>
            </div>
            <?php
        }
        public function menu_page() {
             $this->config_page();
        }


        /**
         * Get detailed plan status using Freemius SDK
         * Returns array with plan status information
         * 
         * @return array Plan status information
         */
        function get_plan_status() {
            $fs = aiautotool_premium();
            $status_info = array(
                'is_free' => false,
                'is_trial' => false,
                'is_paying' => false,
                'is_cancelled' => false,
                'is_expired' => false,
                'is_trial_expired' => false,
                'is_non_renewing' => false,
                'plan_name' => 'free',
                'plan_title' => 'Free',
                'subscription_status' => null,
                'status_label' => 'Free',
                'status_color' => '#6c757d',
                'trial_days_remaining' => null,
                'trial_ends' => null,
                'subscription_days_remaining' => null,
                'subscription_expires' => null,
            );

            try {
                // PRIORITY: Check trial FIRST before paying, because trial users should be shown as trial
                // Check if user is in trial - use multiple methods to ensure detection
                $is_trial_check = false;
                $trial_ends_date = null;
                
                // Method 1: Direct is_trial() check
                if (method_exists($fs, 'is_trial') && $fs->is_trial()) {
                    $is_trial_check = true;
                }
                
                // Method 2: Check site trial_ends date
                if (!$is_trial_check) {
                    try {
                        $site = $fs->get_site();
                        if ($site && isset($site->trial_ends) && !empty($site->trial_ends)) {
                            $trial_ends_timestamp = strtotime($site->trial_ends);
                            $current_timestamp = time();
                            // If trial_ends is in the future, user is in trial
                            if ($trial_ends_timestamp > $current_timestamp) {
                                $is_trial_check = true;
                                $trial_ends_date = $site->trial_ends;
                            }
                        }
                    } catch (Exception $e) {
                        if (defined('WP_DEBUG') && WP_DEBUG) {
                        }
                    }
                }
                
                // Method 3: Check if can_use_premium_code but not paying (likely trial)
                if (!$is_trial_check && method_exists($fs, 'can_use_premium_code') && $fs->can_use_premium_code()) {
                    if (!method_exists($fs, 'is_paying') || !$fs->is_paying()) {
                        // Has premium access but not paying = likely trial
                        $is_trial_check = true;
                    }
                }
                
                if ($is_trial_check) {
                    $status_info['is_trial'] = true;
                    $status_info['is_free'] = false;
                    $status_info['plan_name'] = $fs->get_plan_name();
                    // For trial users, show "Trailer Pro" instead of just the plan title
                    $plan_title_from_fs = $fs->get_plan_title();
                    $status_info['plan_title'] = ($plan_title_from_fs && $plan_title_from_fs !== 'Free') ? esc_html__('Trailer', 'ai-auto-tool') . ' ' . $plan_title_from_fs : esc_html__('Trailer Pro', 'ai-auto-tool');
                    $status_info['status_label'] = 'Trial';
                    $status_info['status_color'] = '#17a2b8';
                    $status_info['subscription_status'] = 'trialing';
                    
                    // Calculate trial days remaining
                    try {
                        $site = $fs->get_site();
                        if ($site && isset($site->trial_ends) && !empty($site->trial_ends)) {
                            $trial_ends_timestamp = strtotime($site->trial_ends);
                            $current_timestamp = time();
                            $days_remaining = max(0, ceil(($trial_ends_timestamp - $current_timestamp) / 86400));
                            $status_info['trial_days_remaining'] = $days_remaining;
                            $status_info['trial_ends'] = $site->trial_ends;
                        } else if ($trial_ends_date) {
                            $trial_ends_timestamp = strtotime($trial_ends_date);
                            $current_timestamp = time();
                            $days_remaining = max(0, ceil(($trial_ends_timestamp - $current_timestamp) / 86400));
                            $status_info['trial_days_remaining'] = $days_remaining;
                            $status_info['trial_ends'] = $trial_ends_date;
                        } else {
                            $status_info['trial_days_remaining'] = 0;
                        }
                    } catch (Exception $e) {
                        if (defined('WP_DEBUG') && WP_DEBUG) {
                        }
                        $status_info['trial_days_remaining'] = 0;
                    }
                }
                // Check if user is paying (Pro plan) - but only if NOT in trial
                else if ($fs->is_paying()) {
                    $status_info['is_paying'] = true;
                    $status_info['is_free'] = false;
                    $status_info['plan_name'] = $fs->get_plan_name();
                    $status_info['plan_title'] = $fs->get_plan_title();
                    $status_info['status_label'] = 'Pro';
                    $status_info['status_color'] = '#28a745';
                    
                    // Get subscription status
                    try {
                        $license = $fs->_get_license();
                        if ($license && isset($license->id)) {
                            $subscription = $fs->_get_subscription($license->id);
                            if ($subscription && method_exists($subscription, 'get_status')) {
                                $subscription_status = $subscription->get_status();
                                $status_info['subscription_status'] = $subscription_status;
                                
                                // Calculate subscription days remaining if expiration date is available
                                if (isset($license->expiration) && !empty($license->expiration)) {
                                    $expiration_timestamp = strtotime($license->expiration);
                                    $current_timestamp = time();
                                    $days_remaining = max(0, ceil(($expiration_timestamp - $current_timestamp) / 86400));
                                    $status_info['subscription_days_remaining'] = $days_remaining;
                                    $status_info['subscription_expires'] = $license->expiration;
                                }
                                
                                // Check subscription status
                                switch ($subscription_status) {
                                    case 'active':
                                        $status_info['status_label'] = 'Pro (Active)';
                                        $status_info['status_color'] = '#28a745';
                                        break;
                                    case 'canceled':
                                        $status_info['is_cancelled'] = true;
                                        $status_info['status_label'] = 'Pro (Cancelled)';
                                        $status_info['status_color'] = '#ffc107';
                                        break;
                                    case 'expired':
                                        $status_info['is_expired'] = true;
                                        $status_info['status_label'] = 'Pro (Expired)';
                                        $status_info['status_color'] = '#dc3545';
                                        break;
                                    case 'non_renewing':
                                        $status_info['is_non_renewing'] = true;
                                        $status_info['status_label'] = 'Pro (Non-Renewing)';
                                        $status_info['status_color'] = '#ff9800';
                                        break;
                                }
                            }
                        }
                    } catch (Exception $e) {
                        if (defined('WP_DEBUG') && WP_DEBUG) {
                        }
                    }
                }
                // Check if trial has expired
                else if (method_exists($fs, 'has_trial_expired') && $fs->has_trial_expired()) {
                    $status_info['is_trial_expired'] = true;
                    $status_info['is_free'] = true;
                    $status_info['status_label'] = 'Trial Expired';
                    $status_info['status_color'] = '#dc3545';
                }
                // Check if free plan
                else if ($fs->is_free_plan()) {
                    $status_info['is_free'] = true;
                    $status_info['plan_name'] = 'free';
                    $status_info['plan_title'] = 'Free';
                    $status_info['status_label'] = 'Free';
                    $status_info['status_color'] = '#6c757d';
                }
                // Fallback: check plan name
                else {
                    $plan_name = $fs->get_plan_name();
                    $status_info['plan_name'] = $plan_name;
                    $status_info['plan_title'] = $fs->get_plan_title();
                    
                    if ($plan_name === 'free') {
                        $status_info['is_free'] = true;
                        $status_info['status_label'] = 'Free';
                    } else {
                        $status_info['status_label'] = ucfirst($plan_name);
                        $status_info['status_color'] = '#28a745';
                    }
                }

                // Debug logging
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    if (isset($status_info['trial_days_remaining']) && $status_info['trial_days_remaining'] !== null) {
                    }
                    if (isset($status_info['trial_ends']) && $status_info['trial_ends']) {
                    }
                    
                    // Also log plan and subscription objects if available
                    try {
                        $plan = $fs->get_plan();
                        if ($plan) {
                        }
                        
                        $license = $fs->_get_license();
                        if ($license && isset($license->id)) {
                            $subscription = $fs->_get_subscription($license->id);
                            if ($subscription) {
                                if (method_exists($subscription, 'get_status')) {
                                }
                            }
                        }
                    } catch (Exception $e) {
                    }
                }

            } catch (Exception $e) {
                if (defined('WP_DEBUG') && WP_DEBUG) {
                }
            }

            return $status_info;
        }

         function is_free_plan() {
            return ! aiautotool_can_use_pro_features();
        }
        //  function is_free_plan() {
        //     if ( ! aiautotool_premium()->is_registered() ) {
        //         return true;
        //     }

        //     if ( ! aiautotool_premium()->has_paid_plan() ) {
        //         return true;
        //     }

        //     return (
        //         'free' === aiautotool_premium()->get_plan_name() ||
        //         ! aiautotool_premium()->has_features_enabled_license()
        //     );
        // }

        private function verify_config_post_action() {
            check_admin_referer('aiautotool_config_action', 'aiautotool_config_nonce');
            if (!current_user_can('manage_options')) {
                wp_die(esc_html__('You do not have sufficient permissions to perform this action.', 'ai-auto-tool'));
            }
        }

        private function get_api_key_from_request($request = null) {
            if ($request instanceof WP_REST_Request) {
                $api_key = $request->get_header('X-API-Key');
                if (empty($api_key)) {
                    $authorization = $request->get_header('Authorization');
                    if (is_string($authorization) && preg_match('/^Bearer\s+(.+)$/i', $authorization, $matches)) {
                        $api_key = $matches[1];
                    }
                }
                return is_string($api_key) ? sanitize_text_field($api_key) : '';
            }

            if (isset($_POST['key'])) {
                return sanitize_text_field(wp_unslash($_POST['key']));
            }

            return '';
        }

        private function is_valid_api_key($provided_key) {
            if (!is_string($provided_key) || $provided_key === '') {
                return false;
            }

            $api_key_list = get_option('aiautotool_api_key_list', array());
            foreach ($api_key_list as $stored_key) {
                if (!empty($stored_key['key']) && hash_equals((string) $stored_key['key'], $provided_key)) {
                    return true;
                }
            }

            return false;
        }

        public function config_page() {
            
            if (!current_user_can('manage_options')) {
            wp_die(esc_html__('You do not have sufficient permissions to access this page.','ai-auto-tool'));
        }

        
        $api_key_list = get_option('aiautotool_api_key_list', array());
        $config_img = get_option('aiautotool_config_img', false);
        $aiautotool_images_list = get_option('aiautotool_images_list', []);
         $users = get_users();
        
        if (isset($_POST['aiautotool_add_key'])) {
            $this->verify_config_post_action();
            $new_key = 'KCT-'.uniqid();
            $user_id = isset($_POST['aiautotool_user']) ? sanitize_text_field(wp_unslash($_POST['aiautotool_user'])) : '';
            $api_key_list = get_option('aiautotool_api_key_list', array());
            $api_key_list[] = array(
                'key' => $new_key,
                'user_id' => $user_id,
            );
            update_option('aiautotool_api_key_list', $api_key_list,null, 'no');

            echo esc_html('<div class="notice notice-success"><p>New API key added.</p></div>');
        }

        if (isset($_POST['aiautotool_save_config'])) {
            $this->verify_config_post_action();
            if(isset($_POST['aiautotool_config_img'])){
                $config_img = sanitize_text_field(wp_unslash($_POST['aiautotool_config_img']));
                update_option('aiautotool_config_img', $config_img,null, 'no');
            }
            
        }

        if (isset($_POST['aiautotool_remove_api_key'])) {
            $this->verify_config_post_action();
            $remove_key = sanitize_text_field(wp_unslash($_POST['aiautotool_remove_api_key']));
            unset($api_key_list[$remove_key]);
            update_option('aiautotool_api_key_list', $api_key_list,null, 'no');
        }
        if (isset($_POST['aiautotool_remove_log_img'])) {
            $this->verify_config_post_action();
            
            
            update_option('aiautotool_images_list', [],null, 'no');
            $aiautotool_images_list = get_option('aiautotool_images_list', []);
            echo esc_html('<div class="notice notice-success"><p>Reset log Images Success!!!!.</p></div>');
        }

        if (isset($_POST['aiautotool_remove_img_log_item'])) {
            $this->verify_config_post_action();
            $remove_key = sanitize_text_field(wp_unslash($_POST['aiautotool_remove_img_log_item']));
            unset($aiautotool_images_list[$remove_key]);
            update_option('aiautotool_images_list', $aiautotool_images_list,null, 'no');
            echo esc_html('<div class="notice notice-success"><p>Remove log item image Success!!!!.</p></div>');
        }
        $this->aiautotool_config_settings = get_option('aiautotool_code_settings',array());
        if(isset($_POST['aiautotool_code_settings'])){
            $this->verify_config_post_action();
            if (!current_user_can('unfiltered_html')) {
                wp_die(esc_html__('You do not have permission to save custom site code.', 'ai-auto-tool'));
            }

            $allowed_code_keys = array('code2', 'code3', 'code4');
            $posted_code_settings = wp_unslash($_POST['aiautotool_code_settings']);
            if (is_array($posted_code_settings)) {
                foreach($posted_code_settings as $key => $value){
                    if(in_array($key, $allowed_code_keys, true)){
                        $this->aiautotool_config_settings[$key] = is_string($value) ? $value : '';
                    }
                }
            }
            $result = update_option('aiautotool_code_settings',(array )$this->aiautotool_config_settings, false);
            
            $this->aiautotool_config_settings = get_option('aiautotool_code_settings',array());
           
        }
        
        ?>


        
            <div class=" aiautotool_container ">
                <div class="ft-wrap-top"></div>
                <div class="wrap ft-wrap">
                    <div class="ft-wrap-body">
                    <div class="ft-box ">
            <div class="ft-menu aiautotool-animate-faster wave-animate-fast">
                <div class="ft-logo"><img src="<?php echo esc_url(plugins_url('/images/logo.svg', __FILE__)); ?>">
                    <br>AI Autotool Settings</div>

              
                 <button href="#tab-setting" class="nav-tab sotabt "><i class="fa-solid fa-gears"></i> Plan & Quota</button>
                
                <button href="#set-prompt" class="nav-tab sotabt "><i class="fa-solid fa-gears"></i>Edit Prompt</button>
                <?php self::show_render_tab_setting(); ?>

                <button  href="#tab2"  class="nav-tab  sotab "><i class="fa-solid fa-code"></i> <?php esc_html_e('WP HEAD', 'ai-auto-tool'); ?></button>
                <button  href="#tab3"  class="nav-tab  sotab" ><i class="fa-solid fa-code"></i> <?php esc_html_e('WP BODY', 'ai-auto-tool'); ?></button>
                <button  href="#tab4"  class=" nav-tab sotab" ><i class="fa-solid fa-code"></i> <?php esc_html_e('WP FOOTER', 'ai-auto-tool'); ?></button>
                <button href="#tab-infodonate" class="nav-tab sotab"><i class="fa-solid fa-circle-info"></i> Info plugin</button>

            </div>
            <div class="ft-main ">
                <div id="tab-setting"  class="tab-content sotab-box ftbox ">
                    <h2></h2>
                    <div class="ft-card-note"> 
                        
                        <?php $this->render_subscription_info(); ?>
                        </div>

                                    </div>
                
                <div id="set-prompt"   class="tab-content sotab-box ftbox " >
                    <?php 
                    // Try to get existing instance from subclasses first
                    $AI_AutoTool_Prompt_Customer = null;
                    foreach ($this->subclasses as $subclass) {
                        if (get_class($subclass) === 'AI_AutoTool_Prompt_Customer') {
                            $AI_AutoTool_Prompt_Customer = $subclass;
                            break;
                        }
                    }
                    
                    // If not found, create new instance (shouldn't happen if aiautotool_new_object ran)
                    if (!$AI_AutoTool_Prompt_Customer && class_exists('AI_AutoTool_Prompt_Customer')) {
                        $AI_AutoTool_Prompt_Customer = new AI_AutoTool_Prompt_Customer();
                        $this->addSubclass($AI_AutoTool_Prompt_Customer);
                    }
                    
                    if ($AI_AutoTool_Prompt_Customer) {
                        $AI_AutoTool_Prompt_Customer->render_setting();
                    }
                    ?>
                </div>

                 
            <div class="sotab-box ftbox tab-content" id="tab2" style="display:none">
                <form method="post" >
                    <?php wp_nonce_field('aiautotool_config_action', 'aiautotool_config_nonce'); ?>
            <h2><?php esc_html_e('WP HEAD', 'ai-auto-tool'); ?></h2>
            <div class="ft-card">
              <h3><i class="fa-solid fa-code"></i> <?php esc_html_e('Add to WP head', 'ai-auto-tool') ?></h3>
                <p>
                <div class="code-editor-container">
                    <div class="code-editor-header">
                        <div class="file-info">
                            <div class="file-icon"></div>
                            <span class="file-name">wp-head.html</span>
                        </div>
                        <div class="editor-controls">
                            <button type="button" class="control-btn" onclick="formatCode(this)">Format</button>
                            <button type="button" class="control-btn" onclick="clearCode(this)">Clear</button>
                        </div>
                    </div>
                    <div class="line-numbers" id="line-numbers-head"></div>
                    <textarea class="ft-code-textarea" name="aiautotool_code_settings[code2]" id="code-head" placeholder="<?php esc_html_e('Input HTML/CSS/JS code here...', 'ai-auto-tool'); ?>" oninput="updateLineNumbers(this, 'line-numbers-head')"><?php if(!empty($this->aiautotool_config_settings['code2'])){echo esc_textarea(stripslashes($this->aiautotool_config_settings['code2']));} ?></textarea>
                </div>
                </p>
            </div>
             <button type="submit" name="btncode" class="button-primary ft-submit"><?php esc_html_e('Save head','ai-auto-tool'); ?></button>
             </form>
            </div>
            <!-- Javascript 2 -->
            <div class="sotab-box ftbox tab-content" id="tab3" style="display:none">
                <form method="post" >
                    <?php wp_nonce_field('aiautotool_config_action', 'aiautotool_config_nonce'); ?>
            <h2><?php esc_html_e('WP BODY', 'ai-auto-tool'); ?></h2>
            <div class="ft-card">
              <h3><i class="fa-solid fa-code"></i> <?php esc_html_e('Add to WP body', 'ai-auto-tool') ?></h3>
                <p>
                <div class="code-editor-container">
                    <div class="code-editor-header">
                        <div class="file-info">
                            <div class="file-icon"></div>
                            <span class="file-name">wp-body.html</span>
                        </div>
                        <div class="editor-controls">
                            <button type="button" class="control-btn" onclick="formatCode(this)">Format</button>
                            <button type="button" class="control-btn" onclick="clearCode(this)">Clear</button>
                        </div>
                    </div>
                    <div class="line-numbers" id="line-numbers-body"></div>
                    <textarea class="ft-code-textarea" name="aiautotool_code_settings[code3]" id="code-body" placeholder="<?php esc_html_e('Input HTML/CSS/JS code here...', 'ai-auto-tool'); ?>" oninput="updateLineNumbers(this, 'line-numbers-body')"><?php if(!empty($this->aiautotool_config_settings['code3'])){echo esc_textarea(stripslashes($this->aiautotool_config_settings['code3']));} ?></textarea>
                </div>
                </p>
            </div>
                 <button type="submit" name="btncode" class="button-primary ft-submit"><?php esc_html_e('Save body','ai-auto-tool'); ?></button></form>
            </div>
            <!-- Javascript 3 -->
            <div class="sotab-box ftbox tab-content" id="tab4" style="display:none">
                <form method="post" >
                    <?php wp_nonce_field('aiautotool_config_action', 'aiautotool_config_nonce'); ?>
            <h2><?php esc_html_e('WP FOOTER', 'ai-auto-tool'); ?></h2>
            <div class="ft-card">
              <h3><i class="fa-solid fa-code"></i> <?php esc_html_e('Add to WP footer', 'ai-auto-tool') ?></h3>
                <p>
                <div class="code-editor-container">
                    <div class="code-editor-header">
                        <div class="file-info">
                            <div class="file-icon"></div>
                            <span class="file-name">wp-footer.html</span>
                        </div>
                        <div class="editor-controls">
                            <button type="button" class="control-btn" onclick="formatCode(this)">Format</button>
                            <button type="button" class="control-btn" onclick="clearCode(this)">Clear</button>
                        </div>
                    </div>
                    <div class="line-numbers" id="line-numbers-footer"></div>
                    <textarea class="ft-code-textarea" name="aiautotool_code_settings[code4]" id="code-footer" placeholder="<?php esc_html_e('Input HTML/CSS/JS code here...', 'ai-auto-tool'); ?>" oninput="updateLineNumbers(this, 'line-numbers-footer')"><?php if(!empty($this->aiautotool_config_settings['code4'])){echo esc_textarea(stripslashes($this->aiautotool_config_settings['code4']));} ?></textarea>
                </div>
                </p>
            </div>  
                 <button type="submit" name="btncode" class="button-primary ft-submit"><?php esc_html_e('Save footer','ai-auto-tool'); ?></button>
             </form>
            </div>

            <!-- infodonate -->
            <div id="tab-infodonate"  class="tab-content sotab-box ftbox" style="display:none;">
                
                <div class="ft-card-note"> 
                    <p><i class="fa-solid fa-circle-info"></i>DEV By: <b><a target="_blank" href="https://aiautotool.com">AI auto Tool</a></b></p>
                    <p>Author: <b>KCT</b></p>
                    <p>Skin by: <a target="_blank" href="https://wordpress.org/plugins/foxtool/"><b>Fox Theme</b></a></p>
                    <p><?php esc_html_e('If you feel this plugin is useful, please Donate to me, I will try to add more useful functions in the next updates :)','ai-auto-tool'); ?></p>
                    <p>
                    <a class="ft-donate-a" target="_blank" href="https://paypal.me/aiautotool">Donate me if it's useful to you</a>
                    
                    </p>
                    </div>
                    <div class="ft-card-note">
                        <?php $this->render_account_info(); ?>
                    </div>
            </div>

            <!-- tab log -->
             <div id="tab-log" class="tab-content sotab-box ftbox" style="display:none;">

        <h2><?php esc_html_e('Log Img','ai-auto-tool'); ?></h2>
               <form method="post">
                                        <?php wp_nonce_field('aiautotool_config_action', 'aiautotool_config_nonce'); ?>
                                        <input type="hidden" name="aiautotool_remove_log_img" value="1">

                                        <button type="submit" class="button-secondary"><?php esc_html_e('Clear all log Images','ai-auto-tool'); ?></button>
                                    </form>
        <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('From url','ai-auto-tool'); ?></th>
                            <th><?php esc_html_e('To Url','ai-auto-tool'); ?></th>
                            <th><?php esc_html_e('Action','ai-auto-tool'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($aiautotool_images_list as $k=>$img_key): ?>
                            <tr>
                                <td><?php echo esc_html($img_key['url_remote']); ?></td>
                                <td><?php echo  esc_html($img_key['url_image_new']['url_out']); ?></td>
                                <td>
                                    <form method="post">
                                        <?php wp_nonce_field('aiautotool_config_action', 'aiautotool_config_nonce'); ?>
                                        <input type="hidden" name="aiautotool_remove_img_log_item" value="<?php echo esc_html($k); ?>">

                                        <button type="submit" class="button-secondary"><?php esc_html_e('Remove','ai-auto-tool'); ?></button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <!-- Content for the Log tab goes here -->
            </div>
            <!-- end tab log -->

            <?php  self::show_render_setting(); ?>
                <div class="sotab-box ftbox">
                    
                </div>
            </div>
        </div>
        <script type="text/javascript"></script>
        
    </div>
    
        </div>
        <?php

       
        }


        public function render_subscription_info() {
            $domain = esc_html(home_url());
            $dt = $this->aiautotool_getdata();
            
            // Get expiration data
            $expiration_data = isset($dt['expiration']) && is_array($dt['expiration']) ? $dt['expiration'] : array();
            $start_date = isset($expiration_data['start_date']) ? $expiration_data['start_date'] : date('Y-m-d');
            $expiration_date = isset($expiration_data['expiration']) ? $expiration_data['expiration'] : date('Y-m-d', strtotime('+1 month'));
            
            // Calculate days until reset
            $current_date = date('Y-m-d');
            $expiration_timestamp = strtotime($expiration_date);
            $current_timestamp = strtotime($current_date);
            $days_until_reset = max(0, ceil(($expiration_timestamp - $current_timestamp) / 86400));
            
            // Calculate usage breakdown percentages
            $total_usage = max(1, $dt['usage']); // Avoid division by zero
            $post_usage_pct = $total_usage > 0 ? round(($dt['AI_post_usage'] / $total_usage) * 100, 1) : 0;
            $comment_usage_pct = $total_usage > 0 ? round(($dt['Commentauto_AI_usage'] / $total_usage) * 100, 1) : 0;
            $tag_usage_pct = $total_usage > 0 ? round(($dt['Autocreatetag_AI_usage'] / $total_usage) * 100, 1) : 0;
            $schedule_usage_pct = $total_usage > 0 ? round(($dt['Schedule_AI_usage'] / $total_usage) * 100, 1) : 0;
            
            // Get plan status FIRST to accurately determine if user is free or premium/trial
            $plan_status_check = $this->get_plan_status();
            $is_free = $plan_status_check['is_free'];
            
            // Double-check: If has active license, trial, or license object, force is_free to false
            // This ensures premium features are shown even if plan_name hasn't updated yet
            $fs_debug = aiautotool_premium();
            $has_license_debug = $fs_debug->has_active_valid_license();
            $has_features_debug = $fs_debug->has_features_enabled_license();
            $is_trial_debug = method_exists($fs_debug, 'is_trial') ? $fs_debug->is_trial() : false;
            $is_paying_debug = method_exists($fs_debug, 'is_paying') ? $fs_debug->is_paying() : false;
            $license_obj_debug = $fs_debug->_get_license();
            
            // If has active license, features enabled, trial, paying, or license object exists, force NOT free
            if ($has_license_debug || $has_features_debug || $is_trial_debug || $is_paying_debug || (is_object($license_obj_debug) && isset($license_obj_debug->id))) {
                $is_free = false; // Definitely NOT free if license exists, trial, or paying
            }
            
            $plan_limit = $is_free ? AIAUTOTOOL_FREE : ($dt['quota'] == -1 ? -1 : 5000);
            $remaining = $plan_limit == -1 ? -1 : max(0, $plan_limit - $dt['usage']);
            $usage_percentage = $plan_limit == -1 ? 0 : min(100, ($dt['usage'] / $plan_limit) * 100);
            
            // Show premium activation notice if premium is activated
            if (!$is_free && (aiautotool_premium()->has_active_valid_license() || (is_object($license_obj_debug) && isset($license_obj_debug->id)))) {
                $fs = rendersetting::is_premium();
                $plan_title = $fs->get_plan_title();
                echo '<div style="margin-bottom: 20px; background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%); border-left: 4px solid #28a745; padding: 16px 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(40, 167, 69, 0.15);">';
                echo '<div style="display: flex; align-items: center; gap: 12px;">';
                echo '<div style="width: 40px; height: 40px; background: linear-gradient(135deg, #28a745 0%, #20c997 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(40, 167, 69, 0.3);">';
                echo '<i class="fa-solid fa-check" style="color: white; font-size: 18px;"></i>';
                echo '</div>';
                echo '<div style="flex: 1;">';
                echo '<h3 style="margin: 0 0 4px 0; color: #155724 !important; font-size: 16px; font-weight: 700;">' . esc_html__('Premium Activated', 'ai-auto-tool') . '</h3>';
                echo '<p style="margin: 0; color: #155724 !important; font-size: 14px; opacity: 0.9;">' . sprintf(esc_html__('Your %s plan is active and all premium features are unlocked!', 'ai-auto-tool'), '<strong>' . esc_html($plan_title) . '</strong>') . '</p>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
            }
            
            echo '<div class="subscription-info-container" style="max-width: 1400px; margin: 0 auto; padding: 0;">';
            
            // Header Section - Modern Design
            echo '<div style="margin-bottom: 30px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 35px 40px; border-radius: 16px; box-shadow: 0 10px 40px rgba(102, 126, 234, 0.3); position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: -50px; right: -50px; width: 200px; height: 200px; background: rgba(255,255,255,0.1); border-radius: 50%;"></div>';
            echo '<div style="position: absolute; bottom: -30px; left: -30px; width: 150px; height: 150px; background: rgba(255,255,255,0.05); border-radius: 50%;"></div>';
            echo '<div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 20px; position: relative; z-index: 1;">';
            echo '<div>';
            echo '<h2 style="margin: 0 0 12px 0; color: white; font-size: 28px; font-weight: 700; letter-spacing: -0.5px;"><i class="fa-solid fa-chart-pie" style="margin-right: 10px;"></i>' . esc_html__('Plan & Quota Overview', 'ai-auto-tool') . '</h2>';
            echo '<p style="margin: 0; opacity: 0.95; font-size: 14px; font-weight: 400;"><i class="fa-solid fa-globe" style="margin-right: 6px;"></i>' . esc_html($domain) . ' <span style="opacity: 0.7;">|</span> <i class="fa-solid fa-code" style="margin-right: 6px;"></i>v' . esc_html(AIAUTOTOOL_VS) . '</p>';
            echo '</div>';
            if (!$is_free) {
                // Get plan status for accurate display
                $plan_status = $this->get_plan_status();
                $accountType = $plan_status['plan_title'];
                $status_color = $plan_status['status_color'];
                
                echo '<div style="text-align: right;">';
                echo '<div style="background: rgba(255,255,255,0.25); backdrop-filter: blur(10px); padding: 16px 28px; border-radius: 12px; display: inline-block; border: 1px solid rgba(255,255,255,0.3); box-shadow: 0 4px 15px rgba(0,0,0,0.1);">';
                echo '<div style="font-size: 12px; opacity: 0.9; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 4px;">' . esc_html__('Current Plan', 'ai-auto-tool') . '</div>';
                echo '<div style="font-size: 22px; font-weight: 700;"><i class="fa-solid fa-crown" style="margin-right: 8px; color: ' . esc_attr($status_color) . ';"></i>' . esc_html($accountType) . '</div>';
                if ($plan_status['subscription_status']) {
                    echo '<div style="font-size: 11px; opacity: 0.8; margin-top: 4px; text-transform: uppercase; letter-spacing: 0.5px; color: ' . esc_attr($status_color) . ';">' . esc_html(ucfirst($plan_status['subscription_status'])) . '</div>';
                }
                echo '</div>';
                echo '</div>';
            }
            echo '</div>';
            echo '</div>';


            if ($is_free) {
                echo '<div style="margin-bottom: 30px; background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%); border: none; border-radius: 16px; padding: 32px; box-shadow: 0 8px 30px rgba(255, 193, 7, 0.2); position: relative; overflow: hidden;">';
                echo '<div style="position: absolute; top: -40px; right: -40px; width: 150px; height: 150px; background: rgba(255,255,255,0.3); border-radius: 50%;"></div>';
                echo '<div style="position: relative; z-index: 1;">';
                echo '<div style="display: flex; align-items: center; margin-bottom: 20px;">';
                echo '<div style="width: 48px; height: 48px; background: linear-gradient(135deg, #ffc107 0%, #ff9800 100%); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 14px; box-shadow: 0 4px 15px rgba(255, 193, 7, 0.4);">';
                echo '<i class="fa-solid fa-gift" style="color: white; font-size: 22px;"></i>';
                echo '</div>';
                echo '<h3 style="margin: 0; color: #856404 !important; font-size: 22px; font-weight: 700; letter-spacing: -0.3px;">' . esc_html__('Upgrade to Premium', 'ai-auto-tool') . '</h3>';
                echo '</div>';
                echo '<p style="color: #856404 !important; margin-bottom: 24px; font-size: 15px; line-height: 1.6; font-weight: 500;">' . esc_html__('Unlock unlimited requests and access to all premium features!', 'ai-auto-tool') . '</p>';
                
                // Check if user is eligible for new user discount
                $is_new_user = false;
                $discount_code = 'NEWKCT50';
                if ( class_exists( 'AIAutoTool_NewUserDiscount' ) ) {
                    $discount_handler = new AIAutoTool_NewUserDiscount();
                    $is_new_user = $discount_handler->is_new_user();
                    $discount_code = apply_filters( 'aiautotool_new_user_discount_code', AIAutoTool_NewUserDiscount::NEW_USER_DISCOUNT_CODE );
                }
                
                // Display discount notice for new users
                if ( $is_new_user ) {
                    $upgrade_url = aiautotool_premium()->get_upgrade_url();
                    $upgrade_url_with_coupon = add_query_arg( 'coupon', $discount_code, $upgrade_url );
                    echo '<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-left: 4px solid #ffc107; padding: 12px 16px; margin-bottom: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(102, 126, 234, 0.2);">';
                    echo '<p style="margin: 0; color: white; font-size: 14px; line-height: 1.5; font-weight: 500;">';
                    echo '<strong style="font-size: 15px;">🎉 ' . esc_html__('Special Offer for New Users!', 'ai-auto-tool') . '</strong><br>';
                    printf(
                        esc_html__('Use code %s to get 50%% off on your first purchase!', 'ai-auto-tool'),
                        '<strong style="background: rgba(255, 255, 255, 0.2); padding: 3px 8px; border-radius: 4px; font-family: monospace; font-size: 13px;">' . esc_html( $discount_code ) . '</strong>'
                    );
                    echo '</p>';
                    echo '</div>';
                }
                
                echo '<div style="display: flex; gap: 12px; flex-wrap: wrap;">';
                
                // Check if trial is available
                $fs_trial = aiautotool_premium();
                $has_trial_plan = method_exists($fs_trial, 'has_trial_plan') ? $fs_trial->has_trial_plan() : true;
                $is_trial_utilized = method_exists($fs_trial, 'is_trial_utilized') ? $fs_trial->is_trial_utilized() : false;
                $can_start_trial = $has_trial_plan && !$is_trial_utilized;
                
                // Show "Start Trial" button if trial is available
                
                    $trial_url = method_exists($fs_trial, 'get_trial_url') ? $fs_trial->get_trial_url() : $fs_trial->get_upgrade_url() . '&trial=true';
                    echo '<a href="' . esc_url($trial_url) . '" class="button button-primary" target="_blank" style="background: linear-gradient(135deg, #17a2b8 0%, #138496 100%); border: none; padding: 14px 28px; font-size: 15px; font-weight: 600; border-radius: 10px; box-shadow: 0 4px 15px rgba(23, 162, 184, 0.3); transition: all 0.3s ease; text-decoration: none; display: inline-flex; align-items: center; color: white !important;">';
                    echo '<i class="fa-solid fa-play-circle" style="margin-right: 8px;"></i> ' . esc_html__('Start Free Trial', 'ai-auto-tool');
                    echo '</a>';
                
                
                $upgrade_url_final = $is_new_user ? add_query_arg( 'coupon', $discount_code, aiautotool_premium()->get_upgrade_url() ) : aiautotool_premium()->get_upgrade_url();
                echo '<a href="' . esc_url($upgrade_url_final) . '" class="button button-primary" target="_blank" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border: none; padding: 14px 28px; font-size: 15px; font-weight: 600; border-radius: 10px; box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3); transition: all 0.3s ease; text-decoration: none; display: inline-flex; align-items: center; color: white !important;">';
                echo '<i class="fa-solid fa-unlock-keyhole" style="margin-right: 8px;"></i> ' . esc_html__('Upgrade to Pro', 'ai-auto-tool');
                echo '</a>';

                $fs_unique_affix = aiautotool_premium()->get_unique_affix();
                echo '<a href="#" class="button button-secondary aiautotool-activate-license-btn activate-license-trigger ' . esc_attr( $fs_unique_affix ) . '" style="background: rgba(255,255,255,0.9); border: 2px solid #ffc107; padding: 14px 28px; font-size: 15px; font-weight: 600; border-radius: 10px; box-shadow: 0 2px 10px rgba(255, 193, 7, 0.2); transition: all 0.3s ease; text-decoration: none; display: inline-flex; align-items: center; color: #856404 !important;">';
                echo '<i class="fa-solid fa-key" style="margin-right: 8px;"></i> ' . esc_html__( 'Activate License', 'ai-auto-tool' );
                echo '</a>';
                echo '</div>';
                echo '</div>';
                echo '</div>';

                // Ensure the activation modal and trial modal are available on this page
                if ( function_exists( 'fs_require_once_template' ) ) {
                    $vars = array( 'id' => aiautotool_premium()->get_id() );
                    fs_require_once_template( 'forms/license-activation.php', $vars );
                    
                    // Load trial start template if trial is available
                    if ($can_start_trial) {
                        fs_require_once_template( 'forms/trial-start.php', $vars );
                    }
                }
            } else {
                // Get detailed plan status
                $plan_status = $this->get_plan_status();
                $fs = rendersetting::is_premium();
                $accountType = $plan_status['plan_title'];
                
                // Determine background color and style based on status
                $bg_gradient = 'linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%)';
                $icon_bg = 'linear-gradient(135deg, #28a745 0%, #20c997 100%)';
                $text_color = '#155724';
                
                if ($plan_status['is_trial']) {
                    $bg_gradient = 'linear-gradient(135deg, #d1ecf1 0%, #bee5eb 100%)';
                    $icon_bg = 'linear-gradient(135deg, #17a2b8 0%, #138496 100%)';
                    $text_color = '#0c5460';
                } else if ($plan_status['is_cancelled']) {
                    $bg_gradient = 'linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%)';
                    $icon_bg = 'linear-gradient(135deg, #ffc107 0%, #ff9800 100%)';
                    $text_color = '#856404';
                } else if ($plan_status['is_expired'] || $plan_status['is_trial_expired']) {
                    $bg_gradient = 'linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%)';
                    $icon_bg = 'linear-gradient(135deg, #dc3545 0%, #c82333 100%)';
                    $text_color = '#721c24';
                } else if ($plan_status['is_non_renewing']) {
                    $bg_gradient = 'linear-gradient(135deg, #ffe0b2 0%, #ffcc80 100%)';
                    $icon_bg = 'linear-gradient(135deg, #ff9800 0%, #f57c00 100%)';
                    $text_color = '#e65100';
                }
                
                // Safely get plan_id with null checks
                $plan_id = null;
                try {
                    $license = aiautotool_premium()->_get_license();
                    if ($license && isset($license->id)) {
                        $subscription = aiautotool_premium()->_get_subscription($license->id);
                        if ($subscription && isset($subscription->plan_id)) {
                            $plan_id = $subscription->plan_id;
                        }
                    }
                } catch (Exception $e) {
                    if (defined('WP_DEBUG') && WP_DEBUG) {
                    }
                }
                
                echo '<div style="margin-bottom: 30px; background: ' . esc_attr($bg_gradient) . '; border: none; border-radius: 16px; padding: 32px; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1); position: relative; overflow: hidden;">';
                echo '<div style="position: absolute; top: -30px; right: -30px; width: 120px; height: 120px; background: rgba(255,255,255,0.2); border-radius: 50%;"></div>';
                echo '<div style="position: relative; z-index: 1;">';
                echo '<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; flex-wrap: wrap; gap: 16px;">';
                echo '<div style="display: flex; align-items: center;">';
                echo '<div style="width: 48px; height: 48px; background: ' . esc_attr($icon_bg) . '; border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 14px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);">';
                echo '<i class="fa-solid fa-crown" style="color: white; font-size: 22px;"></i>';
                echo '</div>';
                echo '<div>';
                echo '<h3 style="margin: 0; color: ' . esc_attr($text_color) . ' !important; font-size: 22px; font-weight: 700; letter-spacing: -0.3px;">' . esc_html__('Premium Plan Benefits', 'ai-auto-tool') . '</h3>';
                echo '<p style="margin: 4px 0 0 0; color: ' . esc_attr($text_color) . ' !important; font-size: 13px; opacity: 0.8;">' . sprintf(esc_html__('Status: %s', 'ai-auto-tool'), '<strong style="color: ' . esc_attr($plan_status['status_color']) . ';">' . esc_html($plan_status['status_label']) . '</strong>') . '</p>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
                echo '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px;">';
                echo '<div style="display: flex; align-items: center; padding: 14px 18px; background: rgba(255,255,255,0.6); backdrop-filter: blur(10px); border-radius: 12px; border: 1px solid rgba(255,255,255,0.8);"><i class="fa-solid fa-check-circle" style="color: ' . esc_attr($plan_status['status_color']) . '; margin-right: 12px; font-size: 18px;"></i> <strong style="color: ' . esc_attr($text_color) . ' !important; font-weight: 600; font-size: 15px;">' . esc_html__('Unlimited Requests', 'ai-auto-tool') . '</strong></div>';
                echo '<div style="display: flex; align-items: center; padding: 14px 18px; background: rgba(255,255,255,0.6); backdrop-filter: blur(10px); border-radius: 12px; border: 1px solid rgba(255,255,255,0.8);"><i class="fa-solid fa-check-circle" style="color: ' . esc_attr($plan_status['status_color']) . '; margin-right: 12px; font-size: 18px;"></i> <strong style="color: ' . esc_attr($text_color) . ' !important; font-weight: 600; font-size: 15px;">' . esc_html__('All Premium Features', 'ai-auto-tool') . '</strong></div>';
                echo '<div style="display: flex; align-items: center; padding: 14px 18px; background: rgba(255,255,255,0.6); backdrop-filter: blur(10px); border-radius: 12px; border: 1px solid rgba(255,255,255,0.8);"><i class="fa-solid fa-check-circle" style="color: ' . esc_attr($plan_status['status_color']) . '; margin-right: 12px; font-size: 18px;"></i> <strong style="color: ' . esc_attr($text_color) . ' !important; font-weight: 600; font-size: 15px;">' . esc_html__('Priority Support', 'ai-auto-tool') . '</strong></div>';
                echo '<div style="display: flex; align-items: center; padding: 14px 18px; background: rgba(255,255,255,0.6); backdrop-filter: blur(10px); border-radius: 12px; border: 1px solid rgba(255,255,255,0.8);"><i class="fa-solid fa-check-circle" style="color: ' . esc_attr($plan_status['status_color']) . '; margin-right: 12px; font-size: 18px;"></i> <strong style="color: ' . esc_attr($text_color) . ' !important; font-weight: 600; font-size: 15px;">' . esc_html__('Regular Updates', 'ai-auto-tool') . '</strong></div>';
                // Trailer/Trial display with days remaining
                $trailer_text = esc_html__('Trial', 'ai-auto-tool');
                $days_remaining = null;
                
                // Check for trial days remaining
                if ($plan_status['is_trial'] && isset($plan_status['trial_days_remaining']) && $plan_status['trial_days_remaining'] !== null) {
                    $days_remaining = intval($plan_status['trial_days_remaining']);
                }
                // Check for subscription days remaining (for paying users)
                else if ($plan_status['is_paying'] && isset($plan_status['subscription_days_remaining']) && $plan_status['subscription_days_remaining'] !== null) {
                    $days_remaining = intval($plan_status['subscription_days_remaining']);
                }
                
                // Display days remaining if available
                if ($days_remaining !== null) {
                    if ($days_remaining > 0) {
                        $trailer_text = esc_html__('Trailer', 'ai-auto-tool') . ' <span style="font-size: 13px; opacity: 0.9; font-weight: 500;">(' . sprintf(esc_html__('%d days remaining', 'ai-auto-tool'), $days_remaining) . ')</span>';
                    } else {
                        $trailer_text = esc_html__('Trailer', 'ai-auto-tool') . ' <span style="font-size: 13px; opacity: 0.9; font-weight: 500;">(' . esc_html__('Expiring soon', 'ai-auto-tool') . ')</span>';
                    }
                }
                
                echo '<div style="display: flex; align-items: center; padding: 14px 18px; background: rgba(255,255,255,0.6); backdrop-filter: blur(10px); border-radius: 12px; border: 1px solid rgba(255,255,255,0.8);"><i class="fa-solid fa-trailer" style="color: ' . esc_attr($plan_status['status_color']) . '; margin-right: 12px; font-size: 18px;"></i> <strong style="color: ' . esc_attr($text_color) . ' !important; font-weight: 600; font-size: 15px;">' . $trailer_text . '</strong></div>';
                echo '</div>';
                
                // Show warning messages for cancelled, expired, or non-renewing plans
                if ($plan_status['is_cancelled']) {
                    echo '<div style="margin-top: 20px; padding: 14px 18px; background: rgba(255, 193, 7, 0.2); border-left: 4px solid #ffc107; border-radius: 8px;">';
                    echo '<p style="margin: 0; color: ' . esc_attr($text_color) . ' !important; font-size: 14px; line-height: 1.5;">';
                    echo '<i class="fa-solid fa-exclamation-triangle" style="margin-right: 8px;"></i>';
                    echo '<strong>' . esc_html__('Subscription Cancelled', 'ai-auto-tool') . '</strong> - ' . esc_html__('Your subscription has been cancelled but you can still use premium features until the end of your billing period.', 'ai-auto-tool');
                    echo '</p>';
                    echo '</div>';
                } else if ($plan_status['is_expired'] || $plan_status['is_trial_expired']) {
                    echo '<div style="margin-top: 20px; padding: 14px 18px; background: rgba(220, 53, 69, 0.2); border-left: 4px solid #dc3545; border-radius: 8px;">';
                    echo '<p style="margin: 0; color: ' . esc_attr($text_color) . ' !important; font-size: 14px; line-height: 1.5;">';
                    echo '<i class="fa-solid fa-exclamation-circle" style="margin-right: 8px;"></i>';
                    echo '<strong>' . esc_html__('Subscription Expired', 'ai-auto-tool') . '</strong> - ' . esc_html__('Your subscription has expired. Please renew to continue using premium features.', 'ai-auto-tool');
                    echo '</p>';
                    echo '</div>';
                } else if ($plan_status['is_non_renewing']) {
                    echo '<div style="margin-top: 20px; padding: 14px 18px; background: rgba(255, 152, 0, 0.2); border-left: 4px solid #ff9800; border-radius: 8px;">';
                    echo '<p style="margin: 0; color: ' . esc_attr($text_color) . ' !important; font-size: 14px; line-height: 1.5;">';
                    echo '<i class="fa-solid fa-info-circle" style="margin-right: 8px;"></i>';
                    echo '<strong>' . esc_html__('Non-Renewing', 'ai-auto-tool') . '</strong> - ' . esc_html__('Your subscription will not automatically renew. Premium features will remain active until the end of your billing period.', 'ai-auto-tool');
                    echo '</p>';
                    echo '</div>';
                } else if ($plan_status['is_trial']) {
                    echo '<div style="margin-top: 20px; padding: 14px 18px; background: rgba(23, 162, 184, 0.2); border-left: 4px solid #17a2b8; border-radius: 8px;">';
                    echo '<p style="margin: 0; color: ' . esc_attr($text_color) . ' !important; font-size: 14px; line-height: 1.5;">';
                    echo '<i class="fa-solid fa-clock" style="margin-right: 8px;"></i>';
                    echo '<strong>' . esc_html__('Trial Period', 'ai-auto-tool') . '</strong> - ' . esc_html__('You are currently in your trial period. Enjoy all premium features!', 'ai-auto-tool');
                    echo '</p>';
                    echo '</div>';
                }
                
                echo '</div>';
                echo '</div>';
            }

            
            // Main Statistics Cards - Modern Design
            echo '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 24px; margin-bottom: 30px;">';
            


            
            // Total Usage Card
            echo '<div style="background: linear-gradient(135deg, #ffffff 0%, #f8f9ff 100%) !important; border: none; border-radius: 16px; padding: 28px; box-shadow: 0 4px 20px rgba(102, 126, 234, 0.12); transition: transform 0.3s ease, box-shadow 0.3s ease; position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: 0; right: 0; width: 80px; height: 80px; background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%); border-radius: 0 0 0 100%;"></div>';
            echo '<div style="position: relative; z-index: 1;">';
            echo '<div style="display: flex; align-items: center; margin-bottom: 16px;">';
            echo '<div style="width: 48px; height: 48px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);">';
            echo '<i class="fa-solid fa-chart-line" style="color: white; font-size: 20px;"></i>';
                echo '</div>';
            echo '<h3 style="margin: 0; color: #2d3748 !important; font-size: 15px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">' . esc_html__('Total Usage', 'ai-auto-tool') . '</h3>';
                echo '</div>';
            echo '<div style="font-size: 42px; font-weight: 700; color: #1a202c !important; margin: 20px 0; line-height: 1;">' . esc_html(number_format($dt['usage'])) . '</div>';
            if ($plan_limit != -1) {
                echo '<div style="margin-top: 20px;">';
                echo '<div style="display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 13px; color: #718096 !important; font-weight: 500;">';
                echo '<span>' . esc_html__('of', 'ai-auto-tool') . ' ' . esc_html(number_format($plan_limit)) . ' ' . esc_html__('requests', 'ai-auto-tool') . '</span>';
                echo '<span><strong style="color: #2d3748 !important;">' . esc_html(number_format($usage_percentage, 1)) . '%</strong></span>';
                echo '</div>';
                echo '<div style="background: #e2e8f0; border-radius: 12px; height: 8px; overflow: hidden; position: relative;">';
                $progress_color = $usage_percentage > 80 ? '#dc3545' : ($usage_percentage > 60 ? '#ffc107' : '#28a745');
                echo '<div style="background: linear-gradient(90deg, ' . esc_attr($progress_color) . ' 0%, ' . esc_attr($progress_color) . 'dd 100%); height: 100%; width: ' . esc_attr($usage_percentage) . '%; transition: width 0.5s ease; border-radius: 12px;"></div>';
                echo '</div>';
                echo '</div>';
            } else {
                echo '<div style="display: inline-flex; align-items: center; margin-top: 16px; padding: 8px 16px; background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%); border-radius: 8px; color: #155724 !important; font-weight: 600; font-size: 14px;"><i class="fa-solid fa-infinity" style="margin-right: 6px;"></i>' . esc_html__('Unlimited', 'ai-auto-tool') . '</div>';
            }
            echo '</div>';
            echo '</div>';
            
            // Remaining Quota Card
            if ($plan_limit != -1) {
                echo '<div style="background: linear-gradient(135deg, #ffffff 0%, #f0fff4 100%) !important; border: none; border-radius: 16px; padding: 28px; box-shadow: 0 4px 20px rgba(40, 167, 69, 0.12); transition: transform 0.3s ease, box-shadow 0.3s ease; position: relative; overflow: hidden;">';
                echo '<div style="position: absolute; top: 0; right: 0; width: 80px; height: 80px; background: linear-gradient(135deg, rgba(40, 167, 69, 0.1) 0%, rgba(34, 139, 58, 0.1) 100%); border-radius: 0 0 0 100%;"></div>';
                echo '<div style="position: relative; z-index: 1;">';
                echo '<div style="display: flex; align-items: center; margin-bottom: 16px;">';
                echo '<div style="width: 48px; height: 48px; background: linear-gradient(135deg, #28a745 0%, #20c997 100%); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 12px rgba(40, 167, 69, 0.3);">';
                echo '<i class="fa-solid fa-hourglass-half" style="color: white; font-size: 20px;"></i>';
                echo '</div>';
                echo '<h3 style="margin: 0; color: #2d3748 !important; font-size: 15px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">' . esc_html__('Remaining', 'ai-auto-tool') . '</h3>';
                echo '</div>';
                echo '<div style="font-size: 42px; font-weight: 700; color: #1a202c !important; margin: 20px 0; line-height: 1;">' . esc_html(number_format($remaining)) . '</div>';
                echo '<div style="color: #718096 !important; font-size: 14px; font-weight: 500;">' . esc_html__('requests available', 'ai-auto-tool') . '</div>';
                if ($remaining < ($plan_limit * 0.2) && $remaining > 0) {
                    echo '<div style="margin-top: 16px; padding: 10px 14px; background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%); border-radius: 8px; color: #856404 !important; font-size: 13px; font-weight: 600; border-left: 3px solid #ffc107;">';
                    echo '<i class="fa-solid fa-exclamation-triangle" style="margin-right: 6px;"></i>' . esc_html__('Low quota remaining', 'ai-auto-tool');
                    echo '</div>';
                }
                echo '</div>';
                echo '</div>';
            }
            
            // Monthly Reset Card
            echo '<div style="background: linear-gradient(135deg, #ffffff 0%, #fff8f0 100%) !important; border: none; border-radius: 16px; padding: 28px; box-shadow: 0 4px 20px rgba(255, 152, 0, 0.12); transition: transform 0.3s ease, box-shadow 0.3s ease; position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: 0; right: 0; width: 80px; height: 80px; background: linear-gradient(135deg, rgba(255, 152, 0, 0.1) 0%, rgba(255, 193, 7, 0.1) 100%); border-radius: 0 0 0 100%;"></div>';
            echo '<div style="position: relative; z-index: 1;">';
            echo '<div style="display: flex; align-items: center; margin-bottom: 16px;">';
            echo '<div style="width: 48px; height: 48px; background: linear-gradient(135deg, #ff9800 0%, #ffc107 100%); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 12px rgba(255, 152, 0, 0.3);">';
            echo '<i class="fa-solid fa-calendar-alt" style="color: white; font-size: 20px;"></i>';
            echo '</div>';
            echo '<h3 style="margin: 0; color: #2d3748 !important; font-size: 15px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">' . esc_html__('Monthly Reset', 'ai-auto-tool') . '</h3>';
            echo '</div>';
            echo '<div style="font-size: 42px; font-weight: 700; color: #1a202c !important; margin: 20px 0; line-height: 1;">' . esc_html($days_until_reset) . '</div>';
            echo '<div style="color: #718096 !important; font-size: 14px; font-weight: 500; margin-bottom: 12px;">' . esc_html__('days until reset', 'ai-auto-tool') . '</div>';
            echo '<div style="margin-top: 16px; padding: 12px; background: #f7fafc; border-radius: 10px; border-left: 3px solid #ff9800;">';
            echo '<div style="font-size: 12px; color: #718096 !important; margin-bottom: 4px; font-weight: 500;">' . esc_html__('Reset date', 'ai-auto-tool') . '</div>';
            echo '<div style="font-size: 14px; color: #2d3748 !important; font-weight: 600;"><i class="fa-solid fa-calendar-check" style="margin-right: 6px; color: #ff9800;"></i>' . esc_html(date_i18n(get_option('date_format'), strtotime($expiration_date))) . '</div>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
            
            // License Status Card (for premium)
            if (!$is_free) {
                $license = aiautotool_premium()->_get_license();
                $license_status = $license ? 'active' : 'inactive';
                echo '<div style="background: linear-gradient(135deg, #ffffff 0%, #f0f9ff 100%) !important; border: none; border-radius: 16px; padding: 28px; box-shadow: 0 4px 20px rgba(23, 162, 184, 0.12); transition: transform 0.3s ease, box-shadow 0.3s ease; position: relative; overflow: hidden;">';
                echo '<div style="position: absolute; top: 0; right: 0; width: 80px; height: 80px; background: linear-gradient(135deg, rgba(23, 162, 184, 0.1) 0%, rgba(13, 110, 253, 0.1) 100%); border-radius: 0 0 0 100%;"></div>';
                echo '<div style="position: relative; z-index: 1;">';
                echo '<div style="display: flex; align-items: center; margin-bottom: 16px;">';
                echo '<div style="width: 48px; height: 48px; background: linear-gradient(135deg, #17a2b8 0%, #0dcaf0 100%); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 12px rgba(23, 162, 184, 0.3);">';
                echo '<i class="fa-solid fa-key" style="color: white; font-size: 20px;"></i>';
                echo '</div>';
                echo '<h3 style="margin: 0; color: #2d3748 !important; font-size: 15px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">' . esc_html__('License Status', 'ai-auto-tool') . '</h3>';
                echo '</div>';
                echo '<div style="margin: 20px 0;">';
                if ($license_status == 'active') {
                    echo '<span style="display: inline-flex; align-items: center; padding: 10px 18px; background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%); color: #155724 !important; border-radius: 10px; font-weight: 600; font-size: 14px; box-shadow: 0 2px 8px rgba(40, 167, 69, 0.2);">';
                    echo '<i class="fa-solid fa-check-circle" style="margin-right: 8px; font-size: 16px;"></i>' . esc_html__('Active', 'ai-auto-tool');
                    echo '</span>';
                } else {
                    echo '<span style="display: inline-flex; align-items: center; padding: 10px 18px; background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%); color: #721c24 !important; border-radius: 10px; font-weight: 600; font-size: 14px; box-shadow: 0 2px 8px rgba(220, 53, 69, 0.2);">';
                    echo '<i class="fa-solid fa-times-circle" style="margin-right: 8px; font-size: 16px;"></i>' . esc_html__('Inactive', 'ai-auto-tool');
                    echo '</span>';
                }
                echo '</div>';
                if ($license && isset($license->secret_key)) {
                    $license_key = $license->secret_key;
                    $license_key_id = 'license-key-' . uniqid();
                    $masked_license_key = substr($license_key, 0, 4) . str_repeat('*', max(0, strlen($license_key) - 8)) . substr($license_key, -4);
                    echo '<div style="margin-top: 16px; padding: 12px; background: #f7fafc; border-radius: 10px; border-left: 3px solid #17a2b8;">';
                    echo '<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;">';
                    echo '<div style="font-size: 11px; color: #718096 !important; text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600;"><i class="fa-solid fa-fingerprint" style="margin-right: 6px;"></i>' . esc_html__('License Key', 'ai-auto-tool') . '</div>';
                    echo '</div>';
                    echo '<div style="position: relative;">';
                    echo '<code id="' . esc_attr($license_key_id) . '" style="display: block; background: #ffffff; padding: 10px 40px 10px 12px; border-radius: 6px; color: #2d3748 !important; font-size: 12px; font-family: \'Monaco\', \'Courier New\', monospace; word-break: break-all; border: 1px solid #e2e8f0; line-height: 1.6;">' . esc_html($masked_license_key) . '</code>';
                    echo '</div>';
                    echo '</div>';
                }
                echo '</div>';
                echo '</div>';
            }
            
            echo '</div>';
            
            // Usage Breakdown Section - Modern Design
            echo '<div style="margin-bottom: 30px; background: #ffffff !important; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border: 1px solid #e2e8f0;">';
            echo '<div style="display: flex; align-items: center; margin-bottom: 24px; padding-bottom: 20px; border-bottom: 2px solid #f7fafc;">';
            echo '<div style="width: 40px; height: 40px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);">';
            echo '<i class="fa-solid fa-chart-pie" style="color: white; font-size: 18px;"></i>';
            echo '</div>';
            echo '<h3 style="margin: 0; color: #1a202c !important; font-size: 20px; font-weight: 700; letter-spacing: -0.3px;">' . esc_html__('Usage Breakdown by Feature', 'ai-auto-tool') . '</h3>';
            echo '</div>';
            echo '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 20px;">';
            
            // AI Post Usage
            echo '<div style="padding: 24px; background: linear-gradient(135deg, #ffffff 0%, #f8f9ff 100%) !important; border: 1px solid #e2e8f0; border-radius: 14px; box-shadow: 0 2px 12px rgba(102, 126, 234, 0.08); transition: all 0.3s ease; position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: 0; right: 0; width: 60px; height: 60px; background: linear-gradient(135deg, rgba(102, 126, 234, 0.08) 0%, rgba(118, 75, 162, 0.08) 100%); border-radius: 0 0 0 100%;"></div>';
            echo '<div style="position: relative; z-index: 1;">';
            echo '<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px;">';
            echo '<div style="display: flex; align-items: center; flex: 1;">';
            echo '<div style="width: 44px; height: 44px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 10px rgba(102, 126, 234, 0.25);">';
            echo '<i class="fa-solid fa-file-alt" style="color: white; font-size: 18px;"></i>';
            echo '</div>';
            echo '<div><strong style="color: #2d3748 !important; font-weight: 600; font-size: 15px; display: block; margin-bottom: 4px;">' . esc_html__('AI Posts', 'ai-auto-tool') . '</strong><div style="font-size: 12px; color: #718096 !important; font-weight: 500;">' . esc_html($post_usage_pct) . '% ' . esc_html__('of total', 'ai-auto-tool') . '</div></div>';
            echo '</div>';
            echo '<div style="font-size: 28px; font-weight: 700; color: #667eea !important; line-height: 1;">' . esc_html(number_format($dt['AI_post_usage'])) . '</div>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
            
            // Auto Comment Usage
            echo '<div style="padding: 24px; background: linear-gradient(135deg, #ffffff 0%, #f0fff4 100%) !important; border: 1px solid #e2e8f0; border-radius: 14px; box-shadow: 0 2px 12px rgba(40, 167, 69, 0.08); transition: all 0.3s ease; position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: 0; right: 0; width: 60px; height: 60px; background: linear-gradient(135deg, rgba(40, 167, 69, 0.08) 0%, rgba(34, 139, 58, 0.08) 100%); border-radius: 0 0 0 100%;"></div>';
            echo '<div style="position: relative; z-index: 1;">';
            echo '<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px;">';
            echo '<div style="display: flex; align-items: center; flex: 1;">';
            echo '<div style="width: 44px; height: 44px; background: linear-gradient(135deg, #28a745 0%, #20c997 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 10px rgba(40, 167, 69, 0.25);">';
            echo '<i class="fa-solid fa-comments" style="color: white; font-size: 18px;"></i>';
            echo '</div>';
            echo '<div><strong style="color: #2d3748 !important; font-weight: 600; font-size: 15px; display: block; margin-bottom: 4px;">' . esc_html__('Auto Comments', 'ai-auto-tool') . '</strong><div style="font-size: 12px; color: #718096 !important; font-weight: 500;">' . esc_html($comment_usage_pct) . '% ' . esc_html__('of total', 'ai-auto-tool') . '</div></div>';
            echo '</div>';
            echo '<div style="font-size: 28px; font-weight: 700; color: #28a745 !important; line-height: 1;">' . esc_html(number_format($dt['Commentauto_AI_usage'])) . '</div>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
            
            // Auto Tag Usage
            echo '<div style="padding: 24px; background: linear-gradient(135deg, #ffffff 0%, #fff8f0 100%) !important; border: 1px solid #e2e8f0; border-radius: 14px; box-shadow: 0 2px 12px rgba(255, 152, 0, 0.08); transition: all 0.3s ease; position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: 0; right: 0; width: 60px; height: 60px; background: linear-gradient(135deg, rgba(255, 152, 0, 0.08) 0%, rgba(255, 193, 7, 0.08) 100%); border-radius: 0 0 0 100%;"></div>';
            echo '<div style="position: relative; z-index: 1;">';
            echo '<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px;">';
            echo '<div style="display: flex; align-items: center; flex: 1;">';
            echo '<div style="width: 44px; height: 44px; background: linear-gradient(135deg, #ff9800 0%, #ffc107 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 10px rgba(255, 152, 0, 0.25);">';
            echo '<i class="fa-solid fa-tags" style="color: white; font-size: 18px;"></i>';
            echo '</div>';
            echo '<div><strong style="color: #2d3748 !important; font-weight: 600; font-size: 15px; display: block; margin-bottom: 4px;">' . esc_html__('Auto Tags', 'ai-auto-tool') . '</strong><div style="font-size: 12px; color: #718096 !important; font-weight: 500;">' . esc_html($tag_usage_pct) . '% ' . esc_html__('of total', 'ai-auto-tool') . '</div></div>';
            echo '</div>';
            echo '<div style="font-size: 28px; font-weight: 700; color: #ff9800 !important; line-height: 1;">' . esc_html(number_format($dt['Autocreatetag_AI_usage'])) . '</div>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
            
            // Schedule Usage
            echo '<div style="padding: 24px; background: linear-gradient(135deg, #ffffff 0%, #f0f9ff 100%) !important; border: 1px solid #e2e8f0; border-radius: 14px; box-shadow: 0 2px 12px rgba(23, 162, 184, 0.08); transition: all 0.3s ease; position: relative; overflow: hidden;">';
            echo '<div style="position: absolute; top: 0; right: 0; width: 60px; height: 60px; background: linear-gradient(135deg, rgba(23, 162, 184, 0.08) 0%, rgba(13, 110, 253, 0.08) 100%); border-radius: 0 0 0 100%;"></div>';
            echo '<div style="position: relative; z-index: 1;">';
            echo '<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px;">';
            echo '<div style="display: flex; align-items: center; flex: 1;">';
            echo '<div style="width: 44px; height: 44px; background: linear-gradient(135deg, #17a2b8 0%, #0dcaf0 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 10px rgba(23, 162, 184, 0.25);">';
            echo '<i class="fa-solid fa-clock" style="color: white; font-size: 18px;"></i>';
            echo '</div>';
            echo '<div><strong style="color: #2d3748 !important; font-weight: 600; font-size: 15px; display: block; margin-bottom: 4px;">' . esc_html__('Scheduled Posts', 'ai-auto-tool') . '</strong><div style="font-size: 12px; color: #718096 !important; font-weight: 500;">' . esc_html($schedule_usage_pct) . '% ' . esc_html__('of total', 'ai-auto-tool') . '</div></div>';
            echo '</div>';
            echo '<div style="font-size: 28px; font-weight: 700; color: #17a2b8 !important; line-height: 1;">' . esc_html(number_format($dt['Schedule_AI_usage'])) . '</div>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
            
            echo '</div>';
            echo '</div>';
            
            // Plan Information & Actions
            
            // Features Section
            echo '<div style="margin-bottom: 30px; background: #ffffff !important; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border: 1px solid #e2e8f0;">';
            echo '<div style="display: flex; align-items: center; margin-bottom: 24px; padding-bottom: 20px; border-bottom: 2px solid #f7fafc;">';
            echo '<div style="width: 40px; height: 40px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);">';
            echo '<i class="fa-solid fa-angles-right" style="color: white; font-size: 18px;"></i>';
            echo '</div>';
            echo '<h3 style="margin: 0; color: #1a202c !important; font-size: 20px; font-weight: 700; letter-spacing: -0.3px;">' . esc_html__('Active Features & Quotas', 'ai-auto-tool') . '</h3>';
            echo '</div>';
            echo '<div style="margin-top: 20px; color: #2d3748 !important;">';
            echo '<style>
                .subscription-info-container .ft-card p,
                .subscription-info-container #tab-setting p,
                .subscription-info-container p {
                    color: #2d3748 !important;
                    font-size: 15px !important;
                    line-height: 1.8 !important;
                    margin: 12px 0 !important;
                }
                .subscription-info-container .ft-card p strong,
                .subscription-info-container #tab-setting p strong,
                .subscription-info-container p strong {
                    color: #1a202c !important;
                    font-weight: 600 !important;
                }
                .subscription-info-container .ft-card p i,
                .subscription-info-container #tab-setting p i,
                .subscription-info-container p i {
                    color: #667eea !important;
                    margin-right: 8px !important;
                }
            </style>';
            self::render_plan();
            echo '</div>';
            echo '</div>';
            
            // Additional Information
            echo '<div style="background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%) !important; border-radius: 16px; padding: 28px; box-shadow: 0 4px 20px rgba(0,0,0,0.04); border: 1px solid #e2e8f0;">';
            echo '<div style="display: flex; align-items: center; margin-bottom: 20px;">';
            echo '<div style="width: 36px; height: 36px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 8px; display: flex; align-items: center; justify-content: center; margin-right: 12px; box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);">';
            echo '<i class="fa-solid fa-info-circle" style="color: white; font-size: 16px;"></i>';
            echo '</div>';
            echo '<h4 style="margin: 0; color: #2d3748 !important; font-size: 18px; font-weight: 700;">' . esc_html__('Information', 'ai-auto-tool') . '</h4>';
            echo '</div>';
            echo '<ul style="margin: 0; padding-left: 0; list-style: none; color: #4a5568 !important; line-height: 1.9;">';
            echo '<li style="padding: 12px 0; padding-left: 28px; position: relative; border-bottom: 1px solid #e2e8f0;"><i class="fa-solid fa-circle-check" style="position: absolute; left: 0; top: 14px; color: #667eea; font-size: 12px;"></i>' . esc_html__('Usage is reset monthly on the reset date shown above.', 'ai-auto-tool') . '</li>';
            echo '<li style="padding: 12px 0; padding-left: 28px; position: relative; border-bottom: 1px solid #e2e8f0;"><i class="fa-solid fa-circle-check" style="position: absolute; left: 0; top: 14px; color: #667eea; font-size: 12px;"></i>' . esc_html__('All AI-powered features (Posts, Comments, Tags, Scheduled) count towards your quota.', 'ai-auto-tool') . '</li>';
            if ($is_free) {
                echo '<li style="padding: 12px 0; padding-left: 28px; position: relative; border-bottom: 1px solid #e2e8f0;"><i class="fa-solid fa-circle-check" style="position: absolute; left: 0; top: 14px; color: #667eea; font-size: 12px;"></i>' . esc_html__('Free plan includes ' . AIAUTOTOOL_FREE . ' requests per month.', 'ai-auto-tool') . '</li>';
            } else {
                echo '<li style="padding: 12px 0; padding-left: 28px; position: relative; border-bottom: 1px solid #e2e8f0;"><i class="fa-solid fa-circle-check" style="position: absolute; left: 0; top: 14px; color: #667eea; font-size: 12px;"></i>' . esc_html__('Premium plans include unlimited requests.', 'ai-auto-tool') . '</li>';
            }
            echo '<li style="padding: 12px 0; padding-left: 28px; position: relative;"><i class="fa-solid fa-circle-check" style="position: absolute; left: 0; top: 14px; color: #667eea; font-size: 12px;"></i>' . esc_html__('For questions or support, please contact our support team.', 'ai-auto-tool') . '</li>';
            echo '</ul>';
            echo '</div>';
            
            echo '</div>';
    }

        
        
        
            /**
         * Render account information from Freemius get_site()
         * Displays install_id, site_id, plan_id, license_id, trial info, etc.
         */
        public function render_account_info() {
            try {
                $fs = aiautotool_premium();
                $is_registered = $fs->is_registered();
                
                if (!$is_registered) {
                    echo '<div style="padding: 30px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 12px; text-align: center;">';
                    echo '<i class="fa-solid fa-exclamation-triangle" style="font-size: 48px; color: #ffc107; margin-bottom: 20px;"></i>';
                    echo '<h3 style="color: #856404; margin: 0 0 10px 0;">' . esc_html__('Account Not Registered', 'ai-auto-tool') . '</h3>';
                    echo '<p style="color: #856404; margin: 0;">' . esc_html__('Please register your account to view account information.', 'ai-auto-tool') . '</p>';
                    echo '</div>';
                    return;
                }
                
                $site = $fs->get_site();
                $license = $fs->_get_license();
                $plan_status = $this->get_plan_status();
                
                echo '<div style="max-width: 1400px; margin: 0 auto; padding: 0;">';
                
                // Header
                echo '<div style="margin-bottom: 30px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 35px 40px; border-radius: 16px; box-shadow: 0 10px 40px rgba(102, 126, 234, 0.3);">';
                echo '<h2 style="margin: 0 0 12px 0; color: white; font-size: 28px; font-weight: 700;"><i class="fa-solid fa-user-circle" style="margin-right: 10px;"></i>' . esc_html__('Account Information', 'ai-auto-tool') . '</h2>';
                echo '<p style="margin: 0; opacity: 0.95; font-size: 14px;"><i class="fa-solid fa-info-circle" style="margin-right: 6px;"></i>' . esc_html__('Detailed account information from Freemius', 'ai-auto-tool') . '</p>';
                echo '</div>';
                
                // Site Information Card
                echo '<div style="margin-bottom: 30px; background: #ffffff; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border: 1px solid #e2e8f0;">';
                echo '<h3 style="margin: 0 0 24px 0; color: #1a202c; font-size: 20px; font-weight: 700; display: flex; align-items: center;"><i class="fa-solid fa-server" style="margin-right: 10px; color: #667eea;"></i>' . esc_html__('Site Information', 'ai-auto-tool') . '</h3>';
                
                if ($site) {
                    $info_items = array(
                        'install_id' => array('label' => __('Install ID', 'ai-auto-tool'), 'value' => isset($site->id) ? $site->id : 'N/A', 'icon' => 'fa-hashtag', 'color' => '#667eea'),
                        'site_id' => array('label' => __('Site ID', 'ai-auto-tool'), 'value' => isset($site->site_id) ? $site->site_id : 'N/A', 'icon' => 'fa-globe', 'color' => '#28a745'),
                        'blog_id' => array('label' => __('Blog ID', 'ai-auto-tool'), 'value' => isset($site->blog_id) ? $site->blog_id : 'N/A', 'icon' => 'fa-blog', 'color' => '#17a2b8'),
                        'plugin_id' => array('label' => __('Plugin ID', 'ai-auto-tool'), 'value' => isset($site->plugin_id) ? $site->plugin_id : 'N/A', 'icon' => 'fa-puzzle-piece', 'color' => '#ff9800'),
                        'user_id' => array('label' => __('User ID', 'ai-auto-tool'), 'value' => isset($site->user_id) ? $site->user_id : 'N/A', 'icon' => 'fa-user', 'color' => '#9c27b0'),
                        'url' => array('label' => __('Site URL', 'ai-auto-tool'), 'value' => isset($site->url) ? esc_html($site->url) : 'N/A', 'icon' => 'fa-link', 'color' => '#2196f3'),
                        'title' => array('label' => __('Site Title', 'ai-auto-tool'), 'value' => isset($site->title) ? esc_html($site->title) : 'N/A', 'icon' => 'fa-heading', 'color' => '#607d8b'),
                        'version' => array('label' => __('Plugin Version', 'ai-auto-tool'), 'value' => isset($site->version) ? esc_html($site->version) : 'N/A', 'icon' => 'fa-code-branch', 'color' => '#795548'),
                        'language' => array('label' => __('Language', 'ai-auto-tool'), 'value' => isset($site->language) ? esc_html($site->language) : 'N/A', 'icon' => 'fa-language', 'color' => '#e91e63'),
                        'platform_version' => array('label' => __('WordPress Version', 'ai-auto-tool'), 'value' => isset($site->platform_version) ? esc_html($site->platform_version) : 'N/A', 'icon' => 'fa-wordpress', 'color' => '#00a0d2'),
                    );
                    
                    echo '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px;">';
                    foreach ($info_items as $key => $item) {
                        echo '<div style="padding: 20px; background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%); border: 1px solid #e2e8f0; border-radius: 12px; transition: all 0.3s ease;">';
                        echo '<div style="display: flex; align-items: center; margin-bottom: 12px;">';
                        echo '<div style="width: 36px; height: 36px; background: ' . esc_attr($item['color']) . '; border-radius: 8px; display: flex; align-items: center; justify-content: center; margin-right: 12px;">';
                        echo '<i class="fa-solid ' . esc_attr($item['icon']) . '" style="color: white; font-size: 16px;"></i>';
                        echo '</div>';
                        echo '<div style="flex: 1;">';
                        echo '<div style="font-size: 12px; color: #718096; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">' . esc_html($item['label']) . '</div>';
                        echo '<div style="font-size: 18px; font-weight: 700; color: #1a202c; word-break: break-all;">' . esc_html($item['value']) . '</div>';
                        echo '</div>';
                        echo '</div>';
                        echo '</div>';
                    }
                    echo '</div>';
                } else {
                    echo '<p style="color: #dc3545; padding: 20px; background: #f8d7da; border-radius: 8px;">' . esc_html__('Site information not available', 'ai-auto-tool') . '</p>';
                }
                echo '</div>';
                
                // Plan & License Information Card
                echo '<div style="margin-bottom: 30px; background: #ffffff; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border: 1px solid #e2e8f0;">';
                echo '<h3 style="margin: 0 0 24px 0; color: #1a202c; font-size: 20px; font-weight: 700; display: flex; align-items: center;"><i class="fa-solid fa-crown" style="margin-right: 10px; color: #ffc107;"></i>' . esc_html__('Plan & License Information', 'ai-auto-tool') . '</h3>';
                
                $plan_items = array();
                if ($site) {
                    if (isset($site->plan_id)) {
                        $plan_items['plan_id'] = array('label' => __('Plan ID', 'ai-auto-tool'), 'value' => $site->plan_id, 'icon' => 'fa-tag', 'color' => '#667eea');
                    }
                    if (isset($site->license_id)) {
                        $plan_items['license_id'] = array('label' => __('License ID', 'ai-auto-tool'), 'value' => $site->license_id, 'icon' => 'fa-key', 'color' => '#28a745');
                    }
                    if (isset($site->trial_plan_id)) {
                        $plan_items['trial_plan_id'] = array('label' => __('Trial Plan ID', 'ai-auto-tool'), 'value' => $site->trial_plan_id, 'icon' => 'fa-clock', 'color' => '#17a2b8');
                    }
                    if (isset($site->trial_ends)) {
                        $trial_ends_date = date('Y-m-d H:i:s', strtotime($site->trial_ends));
                        $plan_items['trial_ends'] = array('label' => __('Trial Ends', 'ai-auto-tool'), 'value' => $trial_ends_date, 'icon' => 'fa-calendar-times', 'color' => '#ff9800');
                    }
                    if (isset($site->is_premium)) {
                        $plan_items['is_premium'] = array('label' => __('Is Premium', 'ai-auto-tool'), 'value' => $site->is_premium ? __('Yes', 'ai-auto-tool') : __('No', 'ai-auto-tool'), 'icon' => 'fa-star', 'color' => $site->is_premium ? '#ffc107' : '#6c757d');
                    }
                }
                
                // Add plan status info
                $plan_items['plan_title'] = array('label' => __('Current Plan', 'ai-auto-tool'), 'value' => isset($plan_status['plan_title']) ? $plan_status['plan_title'] : 'N/A', 'icon' => 'fa-crown', 'color' => '#ffc107');
                $plan_items['status_label'] = array('label' => __('Status', 'ai-auto-tool'), 'value' => isset($plan_status['status_label']) ? $plan_status['status_label'] : 'N/A', 'icon' => 'fa-info-circle', 'color' => isset($plan_status['status_color']) ? $plan_status['status_color'] : '#6c757d');
                
                if ($license && is_object($license)) {
                    if (isset($license->id)) {
                        $plan_items['license_object_id'] = array('label' => __('License Object ID', 'ai-auto-tool'), 'value' => $license->id, 'icon' => 'fa-id-card', 'color' => '#9c27b0');
                    }
                    if (isset($license->expiration)) {
                        $expiration_date = date('Y-m-d H:i:s', strtotime($license->expiration));
                        $plan_items['license_expiration'] = array('label' => __('License Expiration', 'ai-auto-tool'), 'value' => $expiration_date, 'icon' => 'fa-calendar-check', 'color' => '#f44336');
                    }
                }
                
                if (!empty($plan_items)) {
                    echo '<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px;">';
                    foreach ($plan_items as $key => $item) {
                        echo '<div style="padding: 20px; background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%); border: 1px solid #e2e8f0; border-radius: 12px; transition: all 0.3s ease;">';
                        echo '<div style="display: flex; align-items: center; margin-bottom: 12px;">';
                        echo '<div style="width: 36px; height: 36px; background: ' . esc_attr($item['color']) . '; border-radius: 8px; display: flex; align-items: center; justify-content: center; margin-right: 12px;">';
                        echo '<i class="fa-solid ' . esc_attr($item['icon']) . '" style="color: white; font-size: 16px;"></i>';
                        echo '</div>';
                        echo '<div style="flex: 1;">';
                        echo '<div style="font-size: 12px; color: #718096; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;">' . esc_html($item['label']) . '</div>';
                        echo '<div style="font-size: 18px; font-weight: 700; color: #1a202c; word-break: break-all;">' . esc_html($item['value']) . '</div>';
                        echo '</div>';
                        echo '</div>';
                        echo '</div>';
                    }
                    echo '</div>';
                } else {
                    echo '<p style="color: #6c757d; padding: 20px; background: #f8f9fa; border-radius: 8px;">' . esc_html__('Plan information not available', 'ai-auto-tool') . '</p>';
                }
                echo '</div>';
                
                // Secret Key Information (if available)
                if ($site && isset($site->secret_key) && !empty($site->secret_key)) {
                    $masked_secret_key = substr($site->secret_key, 0, 4) . str_repeat('*', max(0, strlen($site->secret_key) - 8)) . substr($site->secret_key, -4);
                    echo '<div style="margin-bottom: 30px; background: #ffffff; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border: 1px solid #e2e8f0;">';
                    echo '<h3 style="margin: 0 0 24px 0; color: #1a202c; font-size: 20px; font-weight: 700; display: flex; align-items: center;"><i class="fa-solid fa-lock" style="margin-right: 10px; color: #dc3545;"></i>' . esc_html__('Security Information', 'ai-auto-tool') . '</h3>';
                    echo '<div style="padding: 20px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 12px;">';
                    echo '<div style="font-size: 12px; color: #856404; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px;">' . esc_html__('Secret Key', 'ai-auto-tool') . '</div>';
                    echo '<div style="font-family: monospace; font-size: 14px; color: #856404; word-break: break-all; padding: 12px; background: white; border-radius: 6px; border: 1px solid #ffc107;">' . esc_html($masked_secret_key) . '</div>';
                    echo '<p style="margin: 12px 0 0 0; font-size: 12px; color: #856404;"><i class="fa-solid fa-exclamation-triangle" style="margin-right: 6px;"></i>' . esc_html__('Keep this key secure and do not share it publicly.', 'ai-auto-tool') . '</p>';
                    echo '</div>';
                    echo '</div>';
                }
                
                // Debug Information (if WP_DEBUG is enabled)
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    $debug_site = $site;
                    if (is_object($debug_site)) {
                        $debug_site = clone $debug_site;
                        if (isset($debug_site->secret_key)) {
                            $debug_site->secret_key = '[hidden]';
                        }
                    }
                    echo '<div style="margin-bottom: 30px; background: #f8f9fa; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border: 1px solid #dee2e6;">';
                    echo '<h3 style="margin: 0 0 24px 0; color: #1a202c; font-size: 20px; font-weight: 700; display: flex; align-items: center;"><i class="fa-solid fa-bug" style="margin-right: 10px; color: #6c757d;"></i>' . esc_html__('Debug Information', 'ai-auto-tool') . '</h3>';
                    echo '<div style="padding: 20px; background: white; border-radius: 12px; border: 1px solid #dee2e6;">';
                    echo '<pre style="margin: 0; padding: 0; font-size: 12px; color: #495057; overflow-x: auto;">';
                    echo esc_html(print_r($debug_site, true));
                    echo '</pre>';
                    echo '</div>';
                    echo '</div>';
                }
                
                echo '</div>';
                
            } catch (Exception $e) {
                echo '<div style="padding: 30px; background: #f8d7da; border: 1px solid #dc3545; border-radius: 12px; text-align: center;">';
                echo '<i class="fa-solid fa-exclamation-circle" style="font-size: 48px; color: #dc3545; margin-bottom: 20px;"></i>';
                echo '<h3 style="color: #721c24; margin: 0 0 10px 0;">' . esc_html__('Error Loading Account Information', 'ai-auto-tool') . '</h3>';
                echo '<p style="color: #721c24; margin: 0;">' . esc_html($e->getMessage()) . '</p>';
                echo '</div>';
                
                if (defined('WP_DEBUG') && WP_DEBUG) {
                }
            }
        }

        public function create_post_route() { 
                register_rest_route( 'aiautotool', '/createpost', array(
                    'methods' => 'POST',
                    'callback' => array($this,'aiautotool_insert_post_ajax'),
                    'permission_callback' => array($this, 'rest_api_permission_check')
                ) );
                register_rest_route( 'aiautotool', '/get_cate', array(
                    'methods' => 'GET',
                    'callback' => array($this,'aiautotool_get_cate'),
                    'permission_callback' => array($this, 'rest_api_permission_check')
                ));
                register_rest_route( 'aiautotool', '/media', array(
                    'methods' => 'GET',
                    'callback' => array($this,'get_all_media_callback'),
                    'permission_callback' => array($this, 'rest_api_permission_check')
                ));
            }
            
        /**
         * REST API permission callback - check API key or user capability
         */
        public function rest_api_permission_check($request) {
            // REST API keys are accepted from headers only to avoid exposing secrets in URLs/logs.
            $api_key = $this->get_api_key_from_request($request);
            
            if (!empty($api_key)) {
                return $this->is_valid_api_key($api_key);
            }
            
            // Fallback: require user to be logged in with edit_posts capability
            return is_user_logged_in() && current_user_can('edit_posts');
        }

        public function register_settings() {
            register_setting( 'ai_auto_tool_api_keys', 'ai_auto_tool_api_keys', array(
                'type' => 'array',
                'sanitize_callback' => array( $this, 'sanitize_array_setting' ),
            ) );
            register_setting( 'ai_auto_tool_log', 'ai_auto_tool_log', array(
                'type' => 'array',
                'sanitize_callback' => array( $this, 'sanitize_array_setting' ),
            ) );
        }
        
        /**
         * Sanitize array settings
         * 
         * @param mixed $value Input value
         * @return array Sanitized array
         */
        public function sanitize_array_setting( $value ) {
            if ( ! is_array( $value ) ) {
                return array();
            }
            return map_deep( $value, 'sanitize_text_field' );
        }

        public function aiautotool_insert_post_ajax($request = null) {
            // Check permissions for AJAX requests
            if (defined('DOING_AJAX') && DOING_AJAX) {
                check_ajax_referer('aiautotool_nonce', 'security');
                
                if (!current_user_can('edit_posts')) {
                    wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')));
                }
            } else {
                // For REST API requests, check API key and permissions
                // The API key validation below will handle REST API permissions
            }
            
            // Láº¥y dá»¯ liá»‡u POST hoáº·c tá»« REST API request
            $request_data = $_POST;
            if (empty($request_data) && isset($_REQUEST)) {
                $request_data = $_REQUEST;
            }
            
            $name = isset($request_data['name']) ? sanitize_text_field($request_data['name']) : '';
            $detail = isset($request_data['detail']) ? wp_kses_post($request_data['detail']) : '';
            $category_name = isset($request_data['category']) ? sanitize_text_field($request_data['category']) : '';
            $tags = isset($request_data['tags']) ? sanitize_text_field($request_data['tags']) : '';
            $date = isset($request_data['date']) ? sanitize_text_field($request_data['date']) : '';
            $key = (!defined('DOING_AJAX') || !DOING_AJAX)
                ? $this->get_api_key_from_request($request)
                : (isset($request_data['key']) ? sanitize_text_field(wp_unslash($request_data['key'])) : '');
            
            // For REST API requests, validate API key first
            if (!defined('DOING_AJAX') || !DOING_AJAX) {
                if (empty($key)) {
                    return new WP_Error('missing_api_key', __('API key is required', 'ai-auto-tool'), array('status' => 403));
                }
                
                if (!$this->is_valid_api_key($key)) {
                    return new WP_Error('invalid_api_key', __('Invalid API key', 'ai-auto-tool'), array('status' => 403));
                }
            }
            $postname = AIAutoTool_Utilities::sanitize_title($name);
            $user_id = '';
            $user_ids = get_users( array(
                'fields' => 'ID',
            ) );
            $random_user_id = $user_ids[array_rand($user_ids)];
            $api_key_list = get_option('aiautotool_api_key_list', array());
            if (count($api_key_list)>0) {
                foreach ($api_key_list as $k=>$api_key){
                    if (!empty($api_key['key']) && hash_equals((string) $api_key['key'], (string) $key)) {
                        $user_id = $random_user_id;//rand(1, 11);//$api_key['user_id'];

                        $config_img = get_option('aiautotool_config_img', false);

                        if($config_img){
                            
                            $aiautotool_images_list = get_option('aiautotool_images_list', []);
                           

                            $html       = str_replace('\"', '"', $detail);
                            $arrayImageUpload = preg_match_all('/<img[^>]+src\s*=\s*["\']([^"\']+)["\']/i', stripslashes($html), $matches) ? $matches[1] : [];
                           
                            $attachThumbnail   = 0;

                            
                            if (!empty($arrayImageUpload)){
                                foreach ($arrayImageUpload as $post_image_url){
                                    $image_item = null;
                                    foreach ($aiautotool_images_list as $item) {
                                        if ($item['url_remote'] === $post_image_url) {
                                            $image_item = $item;
                                            break;
                                        }
                                    }
                                    if ($image_item !== null) {
                                        $image_url_new = $image_item['url_image_new']['url_out'];
                                        $detail = str_replace($post_image_url, $image_url_new, $detail);
                                        if ($attachThumbnail == 0) {
                                            $attachThumbnail = $image_item['url_image_new']['attach_id'];
                                        }
                                    }else{
                                        try {
                                            $image_url_new = AIAutoTool_Utilities::save_image($post_image_url,$postname);
                                            if (!empty($image_url_new)){
                                                $imgUploaded[] = $image_url_new;
                                                if ($attachThumbnail == 0) {
                                                    $attachThumbnail = $image_url_new['attach_id'];
                                                }
                                                 $aiautotool_images_list[] = [
                                                        'url_remote' => $post_image_url,
                                                        'url_image_new' => $image_url_new,
                                                    ];
                                                    update_option('aiautotool_images_list', $aiautotool_images_list,null, 'no');
                                            }
                                        }catch (Exception $e) {
                                        }
                                    }
                                    
                                }
                            }
                            foreach ($imgUploaded as $img){
                                $detail = str_replace($img['url'],$img['baseurl'],$detail);
                            }
                            
                           



                        }

                       
                        $category = get_category_by_slug($category_name);
                        $category_id = $category->term_id;

                        // Insert the post
                        $post_id = wp_insert_post(array(
                            'post_title' => $name,
                            'post_content' => $detail,
                            'post_category' => array($category_id),
                            'tags_input' => $tags,
                            'post_status' => 'publish',
                            'post_author'=>$user_id,
                            'post_date' => $date,
                            'meta_input' => array(
                                'key' => $key,
                            ),
                            'post_type' => 'post',
                        ));

                        // Return the URL of the new post
                        if ($post_id) {
                            if ($attachThumbnail != 0) {
                                set_post_thumbnail($post_id, $attachThumbnail);
                            }
                            $post_url = get_permalink($post_id);
                            echo json_encode(array('url' => $post_url));
                        } else {
                            echo json_encode(array('error' => 'Failed to insert post.'));
                        }
                    }
                }
            }
            
            
            wp_die();
        }

        public function aiautotool_get_cate($request = null) {
            header('Content-Type: application/json; charset=utf-8');
            
            // Check permissions for AJAX requests
            if (defined('DOING_AJAX') && DOING_AJAX) {
                check_ajax_referer('aiautotool_nonce', 'security');
                
                if (!current_user_can('edit_posts')) {
                    wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')));
                }
            }
            
            $key = (!defined('DOING_AJAX') || !DOING_AJAX)
                ? $this->get_api_key_from_request($request)
                : (isset($_POST['key']) ? sanitize_text_field(wp_unslash($_POST['key'])) : '');

            if ($key !== '') {

                $api_key_list = get_option('aiautotool_api_key_list', array());
                if (count($api_key_list)>0) {
                    foreach ($api_key_list as $k=>$api_key){
                        if (!empty($api_key['key']) && hash_equals((string) $api_key['key'], (string) $key)) {
                            $categories = get_categories();

                            $categories_array = array();
                            foreach ( $categories as $category ) {
                                $categories_array[] = array(
                                    'id' => $category->cat_ID,
                                    'name' => $category->name,
                                    'slug' => $category->slug
                                );
                            }
                            echo json_encode( $categories_array );
                            die();
                        }
                    }
                }
                
            } else {
                wp_send_json_error( ' key not in AJAX.' );
            }
           die();
        }




        public function get_all_media_callback($request = null) {
            // Check permissions for AJAX requests
            if (defined('DOING_AJAX') && DOING_AJAX) {
                check_ajax_referer('aiautotool_nonce', 'security');
                
                if (!current_user_can('upload_files')) {
                    wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')));
                }
            }

            $key = (!defined('DOING_AJAX') || !DOING_AJAX)
                ? $this->get_api_key_from_request($request)
                : (isset($_POST['key']) ? sanitize_text_field(wp_unslash($_POST['key'])) : '');
            $api_key_list = get_option('aiautotool_api_key_list', array());
            if (count($api_key_list)>0) {
                foreach ($api_key_list as $k=>$api_key){
                    if (!empty($api_key['key']) && hash_equals((string) $api_key['key'], (string) $key)) {
                        $args = array(
                            'post_type'      => 'attachment',
                            'post_mime_type' => 'image',
                            'post_status'    => 'inherit',
                            'posts_per_page' => - 1,
                        );
                        $media_query = new WP_Query( $args );
                        $media = array();
                        if ( $media_query->have_posts() ) {
                            while ( $media_query->have_posts() ) {
                                $media_query->the_post();
                                $media[] = array(
                                    'id'   => get_the_ID(),
                                    'title'=> get_the_title(),
                                    'url'  => wp_get_attachment_url(),
                                    'type' => get_post_mime_type(),
                                );
                            }
                            wp_reset_postdata();
                            wp_send_json( $media );
                             die();
                        }else{
                            $arr = array('ok'=>'notfindimg');
                            wp_send_json( $arr );
                             die();
                        }

                    }
                }
            }
            
            
            die();
        }


        public function aiautotool_save_image(){
              // Check permissions
              if (defined('DOING_AJAX') && DOING_AJAX) {
                  check_ajax_referer('aiautotool_nonce', 'security');
                  
                  if (!current_user_can('edit_posts')) {
                      wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')));
                  }
              }
              
              $reponse = array();

              if(!empty($_POST['link_img'])){
                  $link_img = esc_url_raw(wp_unslash($_POST['link_img']));
                  $postId = isset($_POST['postId']) ? sanitize_text_field(wp_unslash($_POST['postId'])) : '0';
                  $postTitle = isset($_POST['postTitle']) ? sanitize_text_field(wp_unslash($_POST['postTitle'])) : 'ai-image';
                  $i = isset($_POST['i']) ? sanitize_text_field(wp_unslash($_POST['i'])) : '0';

                  if(strpos(  $link_img,$_SERVER['HTTP_HOST']) !== false){
                    //echo $link_img;
                      $av = array(
                      'stt'=>'ok',
                      'linkedit'=>$link_img
                      );
                  }else{

                    // $imagenew = upload_image($postTitle,$link_img,$postId,$i);
                    $imagenews = AIAutoTool_Utilities::save_image($link_img,$postTitle);
                    if (is_array($imagenews) && !empty($imagenews['url_out'])) {
                        $av = array(
                            'stt'=>'ok',
                            'linkedit'=>$imagenews['url_out'],
                            'attach_id'=>isset($imagenews['attach_id']) ? $imagenews['attach_id'] : 0
                        );
                    } else {
                        $av = array(
                            'stt'=>'notok',
                            'linkedit'=>$link_img,
                            'message'=>'Unable to download image to media library. Remote URL returned as fallback.'
                        );
                    }
                  }
                  
                  
                }else{

                  $av = array(
                  'stt'=>'notok',
                  'linkedit'=>''
                  );
                }


              header( "Content-Type: application/json" );
                echo json_encode($av);

                exit();
            }



    /**
     * AJAX callback for bardcontentmore endpoint - avoids CORS issues
     */
    public function aiautotool_bard_contentmore_callback() {
        // Verify nonce for AJAX requests
        if (defined('DOING_AJAX') && DOING_AJAX) {
            // Use wp_verify_nonce instead of check_ajax_referer to avoid 403 die
            if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'aiautotool_nonce')) {
                wp_send_json(array(
                    'code' => '403',
                    'msg' => __('Security check failed. Please refresh the page and try again.', 'ai-auto-tool'),
                    'result' => ''
                ));
                wp_die();
            }
            
            if (!current_user_can('edit_posts')) {
                wp_send_json(array(
                    'code' => '403',
                    'msg' => __('Permission denied', 'ai-auto-tool'),
                    'result' => ''
                ));
                wp_die();
            }
        }
        
        // Get data from WordPress AJAX (can be in 'data' field or raw POST)
        $data = array();
        
        // Try to get from 'data' field (sent as JSON string)
        if (isset($_POST['data']) && is_string($_POST['data'])) {
            $data = json_decode(stripslashes($_POST['data']), true);
        } else {
            // Try raw POST data
            $raw_data = file_get_contents('php://input');
            $data = json_decode($raw_data, true);
        }
        
        if (!$data || !is_array($data)) {
            wp_send_json(array(
                'code' => '400',
                'msg' => 'Invalid JSON data',
                'result' => ''
            ));
            wp_die();
        }
        
        // Extract parameters
        $question = isset($data['question']) ? sanitize_textarea_field($data['question']) : '';
        $lang = isset($data['lang']) ? sanitize_text_field($data['lang']) : 'Vietnamese';
        $domain = isset($data['domain']) ? sanitize_text_field($data['domain']) : '';
        $model = isset($data['model']) ? sanitize_text_field($data['model']) : 'gemini';
        $info = isset($data['info']) ? $data['info'] : array();
        
        if (empty($question)) {
            wp_send_json(array(
                'code' => '400',
                'msg' => 'Question parameter is required',
                'result' => ''
            ));
            wp_die();
        }
        
        // Use BardGenContent class to make server-side request
        require_once plugin_dir_path(__FILE__) . 'lib/bard.php';
        $bardGenContent = new BardGenContent();
        
        try {
            // Debug log request info
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('=== aiautotool_bard_contentmore_callback() DEBUG ===');
                error_log('Question: ' . $question);
                error_log('Language: ' . $lang);
                error_log('Domain: ' . $domain);
                error_log('Model: ' . $model);
            }
            
            // Call the bardcontentmore method which handles the API call
            $result = $bardGenContent->bardcontentmore($question, $lang, $model);
            
            // Check if result is an array with error code (from API server)
            if (is_array($result) && isset($result['code']) && isset($result['msg'])) {
                // API returned error response (code 201-207, etc.)
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('API returned error code: ' . $result['code']);
                    error_log('API error message: ' . $result['msg']);
                }
                
                wp_send_json(array(
                    'code' => $result['code'],
                    'msg' => $result['msg'],
                    'result' => isset($result['result']) ? $result['result'] : false
                ));
                wp_die();
            }
            
            // Debug log result info
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('API Result Type: ' . gettype($result));
                error_log('API Result Length: ' . (is_string($result) ? strlen($result) : 'N/A'));
                error_log('API Result Preview: ' . (is_string($result) ? substr($result, 0, 200) : json_encode($result)));
            }
            
            if ($result !== null && $result !== '' && $result !== false) {
                $response = array(
                    'code' => '200',
                    'msg' => 'Success',
                    'result' => $result
                );
                
                // Add debug info if WP_DEBUG is enabled
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    $response['debug'] = array(
                        'question' => $question,
                        'lang' => $lang,
                        'result_length' => is_string($result) ? strlen($result) : 0,
                        'result_type' => gettype($result),
                        'timestamp' => current_time('mysql')
                    );
                }
                
                wp_send_json($response);
            } else {
                $error_response = array(
                    'code' => '500',
                    'msg' => 'Failed to generate content - API returned empty result',
                    'result' => ''
                );
                
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('API returned empty/null result');
                    $error_response['debug'] = array(
                        'question' => $question,
                        'lang' => $lang,
                        'error' => 'Empty result from API'
                    );
                }
                
                wp_send_json($error_response);
            }
        } catch (Exception $e) {
            $error_msg = 'Error: ' . $e->getMessage();
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('Exception in aiautotool_bard_contentmore_callback: ' . $e->getMessage());
                error_log('Stack trace: ' . $e->getTraceAsString());
            }
            
            $error_response = array(
                'code' => '500',
                'msg' => $error_msg,
                'result' => ''
            );
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                $error_response['debug'] = array(
                    'exception' => $e->getMessage(),
                    'file' => $e->getFile(),
                    'line' => $e->getLine()
                );
            }
            
            wp_send_json($error_response);
        }
        
        wp_die();
    }
    
    /**
     * AJAX callback for bardcontent endpoint - avoids CORS issues
     */
    public function aiautotool_bard_content_callback() {
        // Verify nonce for AJAX requests
        if (defined('DOING_AJAX') && DOING_AJAX) {
            // Use wp_verify_nonce instead of check_ajax_referer to avoid 403 die
            if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'aiautotool_nonce')) {
                wp_send_json(array(
                    'code' => '403',
                    'msg' => __('Security check failed. Please refresh the page and try again.', 'ai-auto-tool'),
                    'result' => ''
                ));
                wp_die();
            }
            
            if (!current_user_can('edit_posts')) {
                wp_send_json(array(
                    'code' => '403',
                    'msg' => __('Permission denied', 'ai-auto-tool'),
                    'result' => ''
                ));
                wp_die();
            }
        }
        
        // Get data from WordPress AJAX (can be in 'data' field or raw POST)
        $data = array();
        
        // Try to get from 'data' field (sent as JSON string)
        if (isset($_POST['data']) && is_string($_POST['data'])) {
            $data = json_decode(stripslashes($_POST['data']), true);
        } else {
            // Try raw POST data
            $raw_data = file_get_contents('php://input');
            $data = json_decode($raw_data, true);
        }
        
        if (!$data || !is_array($data)) {
            wp_send_json(array(
                'code' => '400',
                'msg' => 'Invalid JSON data',
                'result' => ''
            ));
            wp_die();
        }
        
        // Extract parameters
        $question = isset($data['question']) ? sanitize_textarea_field($data['question']) : '';
        $lang = isset($data['lang']) ? sanitize_text_field($data['lang']) : 'Vietnamese';
        $model = isset($data['model']) ? sanitize_text_field($data['model']) : 'gpt-4.1-nano-2025-04-14';
        
        if (empty($question)) {
            wp_send_json(array(
                'code' => '400',
                'msg' => 'Question parameter is required',
                'result' => ''
            ));
            wp_die();
        }
        
        // Use BardGenContent class to make server-side request
        require_once plugin_dir_path(__FILE__) . 'lib/bard.php';
        $bardGenContent = new BardGenContent();
        
        try {
            // Debug log request info
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('=== aiautotool_bard_content_callback() DEBUG ===');
                error_log('Question: ' . $question);
                error_log('Language: ' . $lang);
                error_log('Model: ' . $model);
            }
            
            // Call the bardcontent method
            $result = $bardGenContent->bardcontent($question, $lang, $model);
            
            // Check if result is an array with error code (from API server)
            if (is_array($result) && isset($result['code']) && isset($result['msg'])) {
                // API returned error response (code 201-207, etc.)
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('API returned error code: ' . $result['code']);
                    error_log('API error message: ' . $result['msg']);
                }
                
                wp_send_json(array(
                    'code' => $result['code'],
                    'msg' => $result['msg'],
                    'result' => isset($result['result']) ? $result['result'] : false
                ));
                wp_die();
            }
            
            // Debug log result info
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('API Result Type: ' . gettype($result));
                error_log('API Result Length: ' . (is_string($result) ? strlen($result) : 'N/A'));
                error_log('API Result Preview: ' . (is_string($result) ? substr($result, 0, 200) : json_encode($result)));
            }
            
            if ($result !== null && $result !== '' && $result !== false) {
                $response = array(
                    'code' => '200',
                    'msg' => 'Success',
                    'result' => $result
                );
                
                // Add debug info if WP_DEBUG is enabled
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    $response['debug'] = array(
                        'question' => $question,
                        'lang' => $lang,
                        'result_length' => is_string($result) ? strlen($result) : 0,
                        'result_type' => gettype($result),
                        'timestamp' => current_time('mysql')
                    );
                }
                
                wp_send_json($response);
            } else {
                $error_response = array(
                    'code' => '500',
                    'msg' => 'Failed to generate content - API returned empty result',
                    'result' => ''
                );
                
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('API returned empty/null result');
                    $error_response['debug'] = array(
                        'question' => $question,
                        'lang' => $lang,
                        'error' => 'Empty result from API'
                    );
                }
                
                wp_send_json($error_response);
            }
        } catch (Exception $e) {
            $error_msg = 'Error: ' . $e->getMessage();
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('Exception in aiautotool_bard_content_callback: ' . $e->getMessage());
                error_log('Stack trace: ' . $e->getTraceAsString());
            }
            
            $error_response = array(
                'code' => '500',
                'msg' => $error_msg,
                'result' => ''
            );
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                $error_response['debug'] = array(
                    'exception' => $e->getMessage(),
                    'file' => $e->getFile(),
                    'line' => $e->getLine()
                );
            }
            
            wp_send_json($error_response);
        }
        
        wp_die();
    }
    
    /**
     * AJAX callback for bardrewrite endpoint - avoids CORS issues
     */
    public function aiautotool_bard_rewrite_callback() {
        // Verify nonce for AJAX requests
        if (defined('DOING_AJAX') && DOING_AJAX) {
            // Use wp_verify_nonce instead of check_ajax_referer to avoid 403 die
            if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'aiautotool_nonce')) {
                wp_send_json(array(
                    'code' => '403',
                    'msg' => __('Security check failed. Please refresh the page and try again.', 'ai-auto-tool'),
                    'result' => ''
                ));
                wp_die();
            }
            
            if (!current_user_can('edit_posts')) {
                wp_send_json(array(
                    'code' => '403',
                    'msg' => __('Permission denied', 'ai-auto-tool'),
                    'result' => ''
                ));
                wp_die();
            }
        }
        
        // Get data from WordPress AJAX (can be in 'data' field or raw POST)
        $data = array();
        
        // Try to get from 'data' field (sent as JSON string)
        if (isset($_POST['data']) && is_string($_POST['data'])) {
            $data = json_decode(stripslashes($_POST['data']), true);
        } else {
            // Try raw POST data
            $raw_data = file_get_contents('php://input');
            $data = json_decode($raw_data, true);
        }
        
        if (!$data || !is_array($data)) {
            wp_send_json(array(
                'code' => '400',
                'msg' => 'Invalid JSON data',
                'result' => ''
            ));
            wp_die();
        }
        
        // Extract parameters
        $question = isset($data['question']) ? sanitize_textarea_field($data['question']) : '';
        $lang = isset($data['lang']) ? sanitize_text_field($data['lang']) : 'Vietnamese';
        $toneOfVoice = isset($data['toneOfVoice']) ? sanitize_text_field($data['toneOfVoice']) : '';
        $model = isset($data['model']) ? sanitize_text_field($data['model']) : 'gpt-4.1-nano-2025-04-14';
        
        if (empty($question)) {
            wp_send_json(array(
                'code' => '400',
                'msg' => 'Question parameter is required',
                'result' => ''
            ));
            wp_die();
        }
        
        // Use BardGenContent class to make server-side request
        require_once plugin_dir_path(__FILE__) . 'lib/bard.php';
        $bardGenContent = new BardGenContent();
        
        try {
            // Debug log request info
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('=== aiautotool_bard_rewrite_callback() DEBUG ===');
                error_log('Question: ' . $question);
                error_log('Language: ' . $lang);
                error_log('Tone of Voice: ' . $toneOfVoice);
                error_log('Model: ' . $model);
            }
            
            // Call the bardrewrite method
            $result = $bardGenContent->bardrewrite($question, $lang, $toneOfVoice, $model);
            
            // Check if result is an array with error code (from API server)
            if (is_array($result) && isset($result['code']) && isset($result['msg'])) {
                // API returned error response (code 201-207, etc.)
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('API returned error code: ' . $result['code']);
                    error_log('API error message: ' . $result['msg']);
                }
                
                wp_send_json(array(
                    'code' => $result['code'],
                    'msg' => $result['msg'],
                    'result' => isset($result['result']) ? $result['result'] : false
                ));
                wp_die();
            }
            
            // Debug log result info
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('API Result Type: ' . gettype($result));
                error_log('API Result Length: ' . (is_string($result) ? strlen($result) : 'N/A'));
                error_log('API Result Preview: ' . (is_string($result) ? substr($result, 0, 200) : json_encode($result)));
            }
            
            if ($result !== null && $result !== '' && $result !== false) {
                $response = array(
                    'code' => '200',
                    'msg' => 'Success',
                    'result' => $result
                );
                
                // Add debug info if WP_DEBUG is enabled
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    $response['debug'] = array(
                        'question' => $question,
                        'lang' => $lang,
                        'toneOfVoice' => $toneOfVoice,
                        'result_length' => is_string($result) ? strlen($result) : 0,
                        'result_type' => gettype($result),
                        'timestamp' => current_time('mysql')
                    );
                }
                
                wp_send_json($response);
            } else {
                $error_response = array(
                    'code' => '500',
                    'msg' => 'Failed to rewrite content - API returned empty result',
                    'result' => ''
                );
                
                if (defined('WP_DEBUG') && WP_DEBUG) {
                    error_log('API returned empty/null result');
                    $error_response['debug'] = array(
                        'question' => $question,
                        'lang' => $lang,
                        'toneOfVoice' => $toneOfVoice,
                        'error' => 'Empty result from API'
                    );
                }
                
                wp_send_json($error_response);
            }
        } catch (Exception $e) {
            $error_msg = 'Error: ' . $e->getMessage();
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('Exception in aiautotool_bard_rewrite_callback: ' . $e->getMessage());
                error_log('Stack trace: ' . $e->getTraceAsString());
            }
            
            $error_response = array(
                'code' => '500',
                'msg' => $error_msg,
                'result' => ''
            );
            
            if (defined('WP_DEBUG') && WP_DEBUG) {
                $error_response['debug'] = array(
                    'exception' => $e->getMessage(),
                    'file' => $e->getFile(),
                    'line' => $e->getLine()
                );
            }
            
            wp_send_json($error_response);
        }
        
        wp_die();
    }
        
    }
    new AI_Auto_Tool();

function aiautotool_load_func() {
            $inc_directory = plugin_dir_path(__FILE__) . 'func';
            $inc_files = glob($inc_directory . '/*.php');
            // print_r($inc_files);
            if ($inc_files) {
                foreach ($inc_files as $file) {
                    if (is_file($file)) {
                        include $file;
                    }
                }
            }
        }
aiautotool_load_func();
}

add_filter('auto_update_plugin_ai-auto-tool', '__return_true');
