<?php
/**
 * Plugin Name: BulkSuite - Bulk Order & Product Generator for WooCommerce
 * 
 * @author            devsabbirahmed
 * @copyright         2024- SABBIRSAM
 * @license           GPL-2.0-or-laters
 * @package BulkSuite - Bulk Order & Product Generator for WooCommerce
 * 
 * Plugin URI: https://wordpress.org/plugins/wc-bulk-order-generator
 * Description: Generate, import, export, and delete WooCommerce orders and products in bulk with optimized batch processing for fast, reliable store management and testing.
 * Version: 1.5.0
 * Requires at least: 5.9
 * Requires PHP:      5.6
 * Requires Plugins: woocommerce
 * Author: sabbirsam
 * Author URI:        https://profiles.wordpress.org/devsabbirahmed/
 * Text Domain: wc-bulk-order-generator
 * Domain Path: /languages/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 */

 if ( ! function_exists( 'wbog_fs' ) ) {
    // Create a helper function for easy SDK access.
    function wbog_fs() {
        global $wbog_fs;

        if ( ! isset( $wbog_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $wbog_fs = fs_dynamic_init( array(
                'id'                  => '19520',
                'slug'                => 'wc-bulk-order-generator',
                'type'                => 'plugin',
                'public_key'          => 'pk_acd2a7190335b57a18f1d9080077d',
                'is_premium'          => false,
				'has_premium_version' => true,
				'has_addons'          => false,
				'has_paid_plans'      => false,
				'is_org_compliant'    => true,
                'menu'                => array(
                    'slug'           => 'wc-order-generator',
                    'override_exact' => true,
                    'parent'         => array(
                        'slug' => 'wc-order-generator',
                    ),
                ),
            ) );
        }

        return $wbog_fs;
    }

    // Init Freemius.
    wbog_fs();
    // Signal that SDK was initiated.
    do_action( 'wbog_fs_loaded' );
}

 defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');


// Define plugin constants.
define('WC_BULK_GENERATOR_VERSION', '1.5.0');
define( 'WC_BULK_GENERATOR_PLUGIN_FILE', __FILE__ );
define('WC_BULK_GENERATOR_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WC_BULK_GENERATOR_PLUGIN_URL', plugin_dir_url(__FILE__));

// Require dependencies.
if ( file_exists(WC_BULK_GENERATOR_PLUGIN_DIR . 'includes/class-wc-bulk-product-generator.php') ) {
    require_once WC_BULK_GENERATOR_PLUGIN_DIR . 'includes/class-wc-bulk-product-generator.php';
}

/**
 * Class WC_Bulk_Order_Generator
 * 
 * This class handles the bulk generation of WooCommerce orders and products.
 * It provides an admin interface for configuring generation settings and processes orders/products in batches.
 */
class WC_Bulk_Order_Generator {
    /**
     * Default batch size for order generation.
     *
     * @var int
     */
    private $batch_size = 50;
    /**
     * Cache for products during generation.
     *
     * @var array
     */
    private $products_cache = array();
    /**
     * Instance of the product generator class.
     *
     * @var WC_Bulk_Product_Generator
     */
    private $product_generator;
    private $order_export;
    private $product_export;
    private $order_import;
    private $product_import;
    private $bulk_delete;

    /**
     * WC_Bulk_Order_Generator constructor.
     * Initializes the plugin by setting up dependencies, hooks, and admin functionality.
     */
    public function __construct() {
        // Initialize product generator.
        $this->init_dependencies();

        // Admin menu and scripts.
        add_action('admin_menu', array($this, 'add_admin_menu'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
        // AJAX actions.
        add_action('wp_ajax_process_order_batch', array($this, 'process_order_batch'));
        add_action('wp_ajax_stop_order_generation', array($this, 'stop_order_generation'));
         // Register settings.
        add_action('admin_init', array($this, 'register_settings'));
        // Disable emails during order generation.
        add_action('woocommerce_email', array($this, 'disable_emails'));

        add_action('admin_notices', array($this, 'check_woocommerce_status'));

        // Add custom plugin action links (Dashboard link).
        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_dashboard_link'));

    }

    /**
     * Initializes dependencies for the class, specifically the product generator.
     * Ensures the required file is included and the product generator instance is created.
     */
   
    private function init_dependencies() {
        $dependencies = [
            'WcBulkOrderGenerator\\WC_Bulk_Product_Generator' => 'includes/class-wc-bulk-product-generator.php',
            'WcBulkOrderGenerator\\WC_Bulk_Order_Export' => 'includes/class-wc-bulk-order-export.php',
            'WcBulkOrderGenerator\\WC_Bulk_Product_Export' => 'includes/class-bulk-product-export.php',
            'WcBulkOrderGenerator\\WC_Bulk_Order_Import' => 'includes/class-wc-bulk-order-import.php',
            'WcBulkOrderGenerator\\WC_Bulk_Delete' => 'includes/class-wc-bulk-delete.php',
            'WcBulkOrderGenerator\\WC_Bulk_Product_Import' => 'includes/class-bulk-product-Import.php',
        ];
    
        foreach ($dependencies as $class_name => $file_path) {
            if (!class_exists($class_name)) {
                $full_path = WC_BULK_GENERATOR_PLUGIN_DIR . $file_path;
    
                if (!file_exists($full_path)) {
                    add_action('admin_notices', function() use ($file_path) {
                        echo '<div class="notice notice-error"><p>' .
                             sprintf(
                                 esc_html__('Bulk Order Generator: Required file "%s" is missing.', 'wc-bulk-order-generator'),
                                 esc_html($file_path)
                             ) .
                             '</p></div>';
                    });
                    return;
                }
    
                require_once $full_path;
            }
        }
    
        // Instantiate the classes dynamically with namespaces
        $this->product_generator = new \WcBulkOrderGenerator\WC_Bulk_Product_Generator();
        $this->order_export = new \WcBulkOrderGenerator\WC_Bulk_Order_Export();
        $this->product_export = new \WcBulkOrderGenerator\WC_Bulk_Product_Export();
        $this->order_import = new \WcBulkOrderGenerator\WC_Bulk_Order_Import();
        $this->product_import = new \WcBulkOrderGenerator\WC_Bulk_Product_Import();
        $this->bulk_delete = new \WcBulkOrderGenerator\WC_Bulk_Delete();
    }


    /**
     * Add the "Dashboard" link next to the "Deactivate" button on the plugin page.
     *
     * @param array $links The existing action links.
     * @return array Modified action links.
     */
    public function add_dashboard_link($links) {
        $dashboard_link = array(
            'dashboard' => '<a href="' . admin_url('admin.php?page=wc-order-generator') . '">' . esc_html__('Generator', 'wc-bulk-order-generator') . '</a>',
        );
        return array_merge($dashboard_link, $links);
    }

    /**
     * Registers plugin settings for the Bulk Order & Product Generator, Import Export for WooCommerce.
     * 
     * This function registers the plugin's settings using the WordPress Settings API.
     * It defines default values for all settings and ensures they are saved in the WordPress options table.
     * The settings are sanitized before being saved using the provided callback function.
     * 
     * @return void
     */
    public function register_settings() {
        // Register the settings for the WC Bulk Generator plugin.
        register_setting(
            'wc_bulk_generator', 
            'wc_bulk_generator_settings', 
            array('sanitize_callback' => array($this, 'sanitize_settings')
        ));

         // Set the default values for all settings.
        $defaults = array(
            'batch_size' => 20,
            'max_orders' => 10000,
            'date_range' => 90,
            'products_per_order' => 5,
            'product_batch_size' => 20,
            'max_products' => 10000      
        );

        $existing_settings = get_option('wc_bulk_generator_settings', array());
        $merged_settings = wp_parse_args($existing_settings, $defaults);

        update_option('wc_bulk_generator_settings', $merged_settings);
    }

    /**
     * Sanitizes the settings values before saving them to the database.
     * 
     * This function ensures that all settings are sanitized properly, converting them to integer values 
     * if they are set, or using default values if not. This is to prevent any invalid or malicious data 
     * from being stored.
     * 
     * @param array $settings The settings to be sanitized.
     * 
     * @return array The sanitized settings with default values applied where necessary.
     */
    public function sanitize_settings($settings) {
        return array(
            'batch_size' => isset($settings['batch_size']) ? absint($settings['batch_size']) : 20,
            'max_orders' => isset($settings['max_orders']) ? absint($settings['max_orders']) : 10000,
            'date_range' => isset($settings['date_range']) ? absint($settings['date_range']) : 90,
            'products_per_order' => isset($settings['products_per_order']) ? absint($settings['products_per_order']) : 5,
            'product_batch_size' => isset($settings['product_batch_size']) ? absint($settings['product_batch_size']) : 20,
            'max_products' => isset($settings['max_products']) ? absint($settings['max_products']) : 10000,
        );
    }

    /**
     * Disables email notifications for specific WooCommerce order statuses.
     * 
     * This function removes the default WooCommerce email notifications 
     * that are triggered when an order status changes. This is useful when 
     * using the bulk order generator to avoid sending emails for bulk-created orders.
     * 
     * @param object $email_class The WooCommerce email class instance.
     * 
     * @return void
     */
    public function disable_emails($email_class) {
        remove_all_actions('woocommerce_order_status_pending_to_processing_notification');
        remove_all_actions('woocommerce_order_status_pending_to_completed_notification');
        remove_all_actions('woocommerce_order_status_processing_to_completed_notification');
        remove_all_actions('woocommerce_new_order_notification');
    }

    /**
     * Check if WooCommerce is active or installed and show an admin notice if it's not.
     */
    public function check_woocommerce_status() {
        // Check if WooCommerce is active.
        if (!is_plugin_active('woocommerce/woocommerce.php')) {
            ?>
            <div class="notice notice-error is-dismissible">
            <p>
                <?php 
                printf(
                    esc_html__('WooCommerce is not installed or active. %1$s may not work correctly.', 'wc-bulk-order-generator'),
                    '<strong>' . esc_html__('Bulk Order & Product Generator, Import Export for WooCommerce', 'wc-bulk-order-generator') . '</strong>'
                );
                ?>
                <a href="<?php echo esc_url(admin_url('plugin-install.php?s=woocommerce&tab=search&type=term')); ?>" class="button button-primary" style="margin-left: 10px;">
                    <?php echo esc_html__('Install WooCommerce', 'wc-bulk-order-generator'); ?>
                </a>
            </p>

            </div>
            <?php
        }
    }
    
    /**
     * Enqueues styles and scripts for the bulk order generator page in the WooCommerce admin.
     * 
     * This function loads the necessary CSS and JavaScript files for the bulk order generator page. It 
     * also localizes settings and nonce values to be used in the JavaScript for AJAX requests.
     * 
     * @param string $hook The current admin page hook.
     * 
     * @return void
     */
    public function enqueue_scripts($hook) {
        if ('woocommerce_page_wc-order-generator' !== $hook) {
            return;
        }

        wp_enqueue_style(
            'wc-order-generator', 
            // WC_BULK_GENERATOR_PLUGIN_URL . 'css/generator.css',
            WC_BULK_GENERATOR_PLUGIN_URL . 'build/css/generator.min.css', 
            array(),
            WC_BULK_GENERATOR_VERSION
        );
        
        wp_enqueue_script(
            'wc-order-generator',
            // WC_BULK_GENERATOR_PLUGIN_URL . 'js/generator.js',
            WC_BULK_GENERATOR_PLUGIN_URL . 'build/js/generator.min.js', 
            array('jquery'),
            WC_BULK_GENERATOR_VERSION,
            true
        );
        
        $settings = get_option('wc_bulk_generator_settings', array());
        
        // Ensure all required keys exist with defaults
        $settings = wp_parse_args($settings, array(
            'batch_size' => 20,
            'max_orders' => 10000,
            'product_batch_size' => 20,
            'max_products' => 10000
        ));

        wp_localize_script('wc-order-generator', 'wcOrderGenerator', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('generate_orders_nonce'),
            'products_nonce' => wp_create_nonce('generate_products_nonce'),
            'batch_size' => $settings['batch_size'],
            'max_orders' => $settings['max_orders'],
            'product_batch_size' => $settings['product_batch_size'],
            'max_products' => $settings['max_products'],
            'export_nonce' => wp_create_nonce('export_orders_nonce'),
            'export_products_nonce' => wp_create_nonce('export_products_nonce'),
            'import_nonce' => wp_create_nonce('import_orders_nonce'),
            'import_products_nonce' => wp_create_nonce('import_products_nonce'),
            'poc_nonce' => wp_create_nonce('poc_ajax_nonce')
        ));
    }

    /**
     * Adds a submenu page for the Bulk Order Generator under the WooCommerce menu.
     * Also adds it to Shop Explorer menu if that plugin is active.
     * 
     * This function adds the "Bulk Generator" page to the WooCommerce menu, allowing administrators 
     * to access the bulk order generation interface from the WordPress admin dashboard.
     * If Shop Explorer plugin is active, it also adds the menu there.
     * 
     * @return void
     */
    public function add_admin_menu() {
        // Add submenu under WooCommerce
        add_submenu_page(
            'woocommerce',
            esc_html__('Bulk Generator', 'wc-bulk-order-generator'),
            esc_html__('Bulk Generator', 'wc-bulk-order-generator'),
            'manage_woocommerce',
            'wc-order-generator',
            array($this, 'admin_page')
        );
        
        // Check if Shop Explorer plugin is active and add submenu there too
        if ($this->is_shop_explorer_active()) {
            add_submenu_page(
                'shop-explorer',
                esc_html__('Bulk Generator', 'wc-bulk-order-generator'),
                esc_html__('Bulk Generator', 'wc-bulk-order-generator'),
                'manage_woocommerce',
                'wc-order-generator',
                array($this, 'admin_page')
            );
        }
    }
    
    /**
     * Check if Shop Explorer plugin is active.
     * 
     * @return bool True if Shop Explorer is active, false otherwise.
     */
    private function is_shop_explorer_active() {
        // Check if the Shop Explorer plugin is active
        if (!function_exists('is_plugin_active')) {
            include_once(ABSPATH . 'wp-admin/includes/plugin.php');
        }
        
        // Check for Shop Explorer plugin - adjust the plugin path as needed
        return is_plugin_active('shop-explorer/shop-explorer.php') || 
               is_plugin_active('shop-explorer.php') ||
               class_exists('ShopExplorer') ||
               defined('SHOP_EXPLORER_VERSION');
    }

    /**
     * Renders the Bulk Order Generator admin page.
     * 
     * This method generates the HTML for the "Bulk Order Generator" page within the WordPress admin dashboard.
     * It fetches the plugin settings, sanitizes them, and then outputs a form or user interface for administrators 
     * to configure the bulk order generation settings such as maximum orders and batch size.
     * 
     * This page allows administrators to access the interface for generating orders and products in bulk for testing 
     * or other use cases. The page includes a header, description, and forms to configure settings.
     * 
     * @return void
     * 
     * @since 1.0.0
     */
    public function admin_page() {
        // Get settings and sanitize them
        $settings = get_option('wc_bulk_generator_settings');
        $max_orders = isset($settings['max_orders']) ? intval($settings['max_orders']) : 100;
        $batch_size = isset($settings['batch_size']) ? intval($settings['batch_size']) : 50;
    
        ?>
        <div class="wrap wc-bulk-order-generator-wrap">
            <div class="wc-bulk-order-generator-header">
                <h1><?php esc_html_e('WC Bulk Generator', 'wc-bulk-order-generator'); ?></h1>
                <p class="description"><?php esc_html_e('Generate test orders and products in batches with monitoring data.', 'wc-bulk-order-generator'); ?></p>
            </div>
    
            <div class="wc-tabs-wrapper">
                <nav class="nav-tab-wrapper">
                    <a href="#products" class="nav-tab nav-tab-active"><?php esc_html_e('Products', 'wc-bulk-order-generator'); ?></a>
                    <a href="#orders" class="nav-tab"><?php esc_html_e('Orders', 'wc-bulk-order-generator'); ?></a>
                    <a href="#export" class="nav-tab"><?php esc_html_e('Export', 'wc-bulk-order-generator'); ?></a>
                    <a href="#import" class="nav-tab"><?php esc_html_e('Import', 'wc-bulk-order-generator'); ?></a>
                    <a href="#action" class="nav-tab"><?php esc_html_e('Actions', 'wc-bulk-order-generator'); ?></a>
                </nav>


                <!-- Products Tab -->
                <div id="products" class="tab-content">
                    <form id="product-generator-form" method="post">
                        <div class="settings-grid">
                            <div class="setting-card">
                                <label for="num_products"><?php esc_html_e('Number of Products', 'wc-bulk-order-generator'); ?></label>
                                <input type="number" id="num_products" name="num_products" 
                                       value="20" 
                                       min="1" 
                                       max="10000">
                                <p class="description">
                                    <?php esc_html_e('Total number of product you want to create', 'wc-bulk-order-generator'); ?>
                                </p>
                            </div>

                            <!-- Product Type Selection -->
                            <div class="setting-card">
                                <label><?php esc_html_e('Product Types', 'wc-bulk-order-generator'); ?></label>
                                <div class="product-type-options">
                                    <label class="product-type-checkbox">
                                        <input type="checkbox" name="product_types[]" value="simple" checked>
                                        <?php esc_html_e('Simple', 'wc-bulk-order-generator'); ?>
                                        <span class="checkmark"></span>
                                    </label>
                                    <label class="product-type-checkbox">
                                        <input type="checkbox" name="product_types[]" value="variable">
                                        <?php esc_html_e('Variable', 'wc-bulk-order-generator'); ?>
                                        <span class="checkmark"></span>
                                    </label>
                                    <label class="product-type-checkbox">
                                        <input type="checkbox" name="product_types[]" value="grouped">
                                        <?php esc_html_e('Grouped', 'wc-bulk-order-generator'); ?>
                                        <span class="checkmark"></span>
                                    </label>
                                    <label class="product-type-checkbox">
                                        <input type="checkbox" name="product_types[]" value="external">
                                        <?php esc_html_e('Affiliate/External', 'wc-bulk-order-generator'); ?>
                                        <span class="checkmark"></span>
                                    </label>
                                </div>
                                <p class="description"><?php esc_html_e('Select which types of products to generate. If more than one is selected, types will be chosen randomly for each product.', 'wc-bulk-order-generator'); ?></p>
                            </div>

                            <!-- Product Name Prefix -->
                            <div class="setting-card">
                                <label for="product_name_prefix"><?php esc_html_e('Product Name Prefix (Optional)', 'wc-bulk-order-generator'); ?></label>
                                <input type="text" id="product_name_prefix" name="product_name_prefix" 
                                       value="" 
                                       placeholder="e.g., Premium, Deluxe">
                                <p class="description">
                                    <?php esc_html_e('Add a prefix before each product name', 'wc-bulk-order-generator'); ?>
                                </p>
                            </div>

                            <!-- Product Name Suffix -->
                            <div class="setting-card">
                                <label for="product_name_suffix"><?php esc_html_e('Product Name Suffix (Optional)', 'wc-bulk-order-generator'); ?></label>
                                <input type="text" id="product_name_suffix" name="product_name_suffix" 
                                       value="" 
                                       placeholder="e.g., Edition, Series">
                                <p class="description">
                                    <?php esc_html_e('Add a suffix after each product name', 'wc-bulk-order-generator'); ?>
                                </p>
                            </div>

                            <div class="setting-card">
                                <label for="product_batch_size"><?php esc_html_e('Batch Size', 'wc-bulk-order-generator'); ?></label>
                                <input type="number" id="product_batch_size" name="product_batch_size" 
                                       value="10" 
                                       min="5" 
                                       max="30">
                                <p class="description">
                                    <?php esc_html_e('Set the amount to generate product at a time in each batch (5-30)', 'wc-bulk-order-generator'); ?>
                                </p>
                                <div class="poc-note">
                                    <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                    <?php esc_html_e('Adjust the batch size based on your system\'s capacity. Higher values may require more memory.', 'wc-bulk-order-generator'); ?></p>
                                </div>
                            </div>

                            <!-- Use Random Images Checkbox -->
                            <div class="setting-card">
                                <label><?php esc_html_e('Product Images', 'wc-bulk-order-generator'); ?></label>
                                <div class="product-type-options">
                                    <label class="product-type-checkbox">
                                        <input type="checkbox" id="use_random_images" name="use_random_images" value="1">
                                        <?php esc_html_e('Generate Product Images', 'wc-bulk-order-generator'); ?>
                                        <span class="checkmark"></span>
                                    </label>
                                </div>
                                <p class="description">
                                    <?php esc_html_e('Enable to generate product images. This may increase processing time.', 'wc-bulk-order-generator'); ?>
                                </p>
                            </div>
                        </div>
    
                        <div class="progress-wrapper">
                            <div class="product-progress-bar" style="width: 0%"></div>
                        </div>
    
                        <div class="stats-grid">
                            <div class="stat-card">
                                <div class="stat-value" id="products-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Products Created', 'wc-bulk-order-generator'); ?></div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value" id="products-failed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Failed', 'wc-bulk-order-generator'); ?></div>
                            </div>
                            
                        </div>
    
                        <div id="product-generation-status" class="notice notice-info" style="display: none;"></div>
    
                        <div class="control-buttons">
                            <input type="submit" id="start-product-generation" class="button button-primary" value="<?php esc_attr_e('Generate Products', 'wc-bulk-order-generator'); ?>">
                            <button type="button" id="stop-product-generation" class="button" disabled><?php esc_html_e('Stop Generation', 'wc-bulk-order-generator'); ?></button>
                            <button type="button" id="reset-product-generation" class="button button-secondary"><?php esc_html_e('Reset', 'wc-bulk-order-generator'); ?></button>
                        </div>
                    </form>
                </div>
    
                <!-- Orders Tab -->
                <div id="orders" class="tab-content">
                    <form id="order-generator-form" method="post">
                        <div class="settings-grid">
                            <div class="setting-card">
                                <label for="num_orders"><?php esc_html_e('Number of Orders', 'wc-bulk-order-generator'); ?></label>
                                <input type="number" id="num_orders" name="num_orders" 
                                    value="100" 
                                    min="1" 
                                    max="10000">
                                <p class="description">
                                    <?php esc_html_e('Total number of order you want to create', 'wc-bulk-order-generator'); ?>
                                </p>
                            </div>

                            <div class="setting-card">
                                <label for="batch_size"><?php esc_html_e('Batch Size', 'wc-bulk-order-generator'); ?></label>
                                <input type="number" id="batch_size" name="batch_size" 
                                       value="10" 
                                       min="5" 
                                       max="30">
                                <p class="description">
                                    <?php esc_html_e('Set the amount to generate order at a time in each batch (5-30)', 'wc-bulk-order-generator'); ?>
                                </p>
                                <div class="poc-note">
                                    <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                    <?php esc_html_e('Adjust the batch size based on your system\'s capacity. Higher values may require more memory.', 'wc-bulk-order-generator'); ?></p>
                                </div>
                            </div>
                        </div>
    
                        <div class="progress-wrapper">
                            <div class="progress-bar" style="width: 0%"></div>
                        </div>
    
                        <div class="stats-grid">
                            <div class="stat-card">
                                <div class="stat-value" id="total-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Total Processed', 'wc-bulk-order-generator'); ?></div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value" id="success-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Successful', 'wc-bulk-order-generator'); ?></div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value" id="failed-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Failed', 'wc-bulk-order-generator'); ?></div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value" id="processing-rate"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Orders/Second', 'wc-bulk-order-generator'); ?></div>
                            </div>
                        </div>
    
                        <div class="stats-grid">
                            <div class="stat-card">
                                <div class="stat-value" id="elapsed-time"><?php esc_html_e('0s', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Elapsed Time', 'wc-bulk-order-generator'); ?></div>
                            </div>
                            <div class="stat-card">
                                <div class="stat-value" id="time-remaining"><?php esc_html_e('--', 'wc-bulk-order-generator'); ?></div>
                                <div class="stat-label"><?php esc_html_e('Estimated Time Remaining', 'wc-bulk-order-generator'); ?></div>
                            </div>
                        </div>
    
                        <div id="generation-status" class="notice notice-info" style="display: none;"></div>
    
                        <div class="control-buttons">
                            <input type="submit" id="start-generation" class="button button-primary" value="<?php esc_attr_e('Generate Orders', 'wc-bulk-order-generator'); ?>">
                            <button type="button" id="stop-generation" class="button" disabled><?php esc_html_e('Stop Generation', 'wc-bulk-order-generator'); ?></button>
                            <button type="button" id="reset-generation" class="button button-secondary"><?php esc_html_e('Reset', 'wc-bulk-order-generator'); ?></button>
                        </div>
                    </form>
                </div>
    

                <!-- Import and Export  -->
                <div id="export" class="tab-content">
                    
                    <!-- Export section  -->
                    <div class="export-section">
                        <h2><?php esc_html_e('Order Export', 'wc-bulk-order-generator'); ?></h2>
                        <form id="order-export-form">
                            <div class="setting-card">
                                <label for="export-batch-size"><?php esc_html_e('Batch Size', 'wc-bulk-order-generator'); ?></label>
                                <input type="number" id="export-batch-size" name="export-batch-size" 
                                    value="10" min="5" max="30">
                                <p class="description"><?php esc_html_e('Number of orders to export per batch (5-30).', 'wc-bulk-order-generator'); ?></p>
                                <div class="poc-note">
                                    <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                    <?php esc_html_e('Adjust the batch size based on your system\'s capacity. Higher values may require more memory.', 'wc-bulk-order-generator'); ?></p>
                                </div>
                            </div>

                            <div class="setting-card">
                                <label for="export-status"><?php esc_html_e('Order Status', 'wc-bulk-order-generator'); ?></label>
                                <p><?php esc_html_e('Select the order types you want to export. If none are selected, all data will be exported.', 'wc-bulk-order-generator'); ?> </p>
                                <select id="export-status" name="export-status" multiple>
                                    <?php
                                    $order_statuses = wc_get_order_statuses();
                                    foreach ($order_statuses as $status => $label) {
                                        echo '<option value="' . esc_attr($status) . '">' . esc_html($label) . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="progress-wrapper">
                                <div class="export-progress-bar"></div>
                            </div>

                            <div class="stats-grid">
                                <div class="stat-card">
                                    <div class="stat-value" id="export-total-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Total Processed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="export-success-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Successful', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="export-failed-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Failed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                            </div>

                            <div class="stats-grid">
                                <div class="stat-card">
                                    <div class="stat-value" id="export-elapsed-time"><?php esc_html_e('0s', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Elapsed Time', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="export-time-remaining"><?php esc_html_e('--', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Estimated Time Remaining', 'wc-bulk-order-generator'); ?></div>
                                </div>
                            </div>

                            <div id="export-order-status" class="notice notice-info" style="display: none;"></div>

                            <div class="control-buttons">
                                <input type="submit" id="start-order-export" class="button button-primary" value="<?php esc_attr_e('Export Orders', 'wc-bulk-order-generator'); ?>">
                                <button type="button" id="reset-order-export" class="button button-secondary"><?php esc_html_e('Reset', 'wc-bulk-order-generator'); ?></button>
                            </div>
                        </form>
                    </div>

                    <br>
                    <br>
                    <br>
                    <hr>
                    <hr>
                    <br>
                    <br>
                     <!-- Product Export section -->
                    <div class="export-section">
                        <h2><?php esc_html_e('Product Export', 'wc-bulk-order-generator'); ?></h2>
                        <form id="product-export-form">
                            <div class="setting-card">
                                <label for="product-export-batch-size"><?php esc_html_e('Batch Size', 'wc-bulk-order-generator'); ?></label>
                                <input type="number" id="product-export-batch-size" name="product-export-batch-size" 
                                    value="10" min="5" max="30">
                                <p class="description"><?php esc_html_e('Number of products to export per batch (5-30)', 'wc-bulk-order-generator'); ?></p>
                                <div class="poc-note">
                                    <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                    <?php esc_html_e('Adjust the batch size based on your system\'s capacity. Higher values may require more memory.', 'wc-bulk-order-generator'); ?></p>
                                </div>
                            </div>

                            <div class="setting-card">
                                <label for="product-type"><?php esc_html_e('Product Type', 'wc-bulk-order-generator'); ?></label>
                                <p><?php esc_html_e('Choose the data types from the selection fields below. If none are selected, all products will be exported.', 'wc-bulk-order-generator'); ?></p>

                                <select id="product-type" name="product-type" multiple>
                                    <?php
                                    $product_types = wc_get_product_types();
                                    foreach ($product_types as $type => $label) {
                                        echo '<option value="' . esc_attr($type) . '">' . esc_html($label) . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="setting-card">
                                <label for="product-category"><?php esc_html_e('Product Category', 'wc-bulk-order-generator'); ?></label>
                                <select id="product-category" name="product-category" multiple>
                                    <?php
                                    $product_categories = get_terms(['taxonomy' => 'product_cat', 'hide_empty' => false]);
                                    foreach ($product_categories as $category) {
                                        echo '<option value="' . esc_attr($category->term_id) . '">' . esc_html($category->name) . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="setting-card">
                                <label for="product-tag"><?php esc_html_e('Product Tag', 'wc-bulk-order-generator'); ?></label>
                                <select id="product-tag" name="product-tag" multiple>
                                    <?php
                                    $product_tags = get_terms(['taxonomy' => 'product_tag', 'hide_empty' => false]);
                                    foreach ($product_tags as $tag) {
                                        echo '<option value="' . esc_attr($tag->term_id) . '">' . esc_html($tag->name) . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="progress-wrapper">
                                <div class="product-export-progress-bar"></div>
                            </div>

                            <div class="stats-grid">
                                <div class="stat-card">
                                    <div class="stat-value" id="product-export-total-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Total Processed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-export-success-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Successful', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-export-failed-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Failed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                            </div>

                            <div class="stats-grid">
                                <div class="stat-card">
                                    <div class="stat-value" id="product-export-elapsed-time"><?php esc_html_e('0s', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Elapsed Time', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-export-time-remaining"><?php esc_html_e('--', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Estimated Time Remaining', 'wc-bulk-order-generator'); ?></div>
                                </div>
                            </div>

                            <div id="export-product-status" class="notice notice-info" style="display: none;"></div>

                            <div class="control-buttons">
                                <input type="submit" id="start-product-export" class="button button-primary" value="<?php esc_attr_e('Export Products', 'wc-bulk-order-generator'); ?>">
                                <button type="button" id="reset-product-export" class="button button-secondary"><?php esc_html_e('Reset', 'wc-bulk-order-generator'); ?></button>
                            </div>
                        </form>
                    </div>


                </div>

                <div id="import" class="tab-content">
                    <!-- Import  -->
                    <div class="import-section">
                        <h2><?php esc_html_e('Order Import', 'wc-bulk-order-generator'); ?></h2>
                        <form id="order-import-form" enctype="multipart/form-data">
                            
                            <table class="form-table enhanced-import-settings">
                                <tr>
                                    <th scope="row">
                                        <label for="import-csv"><?php esc_html_e('CSV File', 'wc-bulk-order-generator'); ?></label>
                                    </th>
                                    <td>
                                        <div class="file-upload-wrapper">
                                            <input type="file" 
                                                id="import-csv" 
                                                name="csv_file" 
                                                accept=".csv" 
                                                class="file-upload-input" 
                                                required>
                                            <div class="file-upload-preview">
                                                <div class="upload-placeholder">
                                                    <i class="upload-icon">📤</i>
                                                    <span class="upload-text">Drag & Drop or Click to Upload CSV</span>
                                                </div>
                                            </div>
                                        </div>
                                        <p class="description"><?php esc_html_e('Upload a CSV file with order details', 'wc-bulk-order-generator'); ?></p>
                                        <div class="file-upload-validation">
                                            <small><?php esc_html_e('Accepted: .csv | 50MB only | Use only WC Bulk Generator CSV', 'wc-bulk-order-generator'); ?></small>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">
                                        <label for="import-batch-size"><?php esc_html_e('Batch Size', 'wc-bulk-order-generator'); ?></label>
                                    </th>
                                    <td>
                                        <input type="number" id="import-batch-size" name="batch_size" 
                                            value="10" min="5" max="30">
                                        <p class="description"><?php esc_html_e('Number of orders to process per batch (5-30)', 'wc-bulk-order-generator'); ?></p>
                                        <div class="poc-note">
                                            <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                            <?php esc_html_e('Adjust the batch size based on your system\'s capacity. Higher values may require more memory.', 'wc-bulk-order-generator'); ?></p>
                                        </div>
                                    </td>
                                </tr>
                            </table>

                            <div class="progress-wrapper">
                                <div class="import-progress-bar"></div>
                            </div>

                            <div class="stats-grid">
                                <div class="stat-card">
                                    <div class="stat-value" id="import-total-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Total Processed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="import-success-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Successful', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="import-failed-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Failed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="import-skipped-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Skipped', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="import-elapsed-time"><?php esc_html_e('0s', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Elapsed Time', 'wc-bulk-order-generator'); ?></div>
                                </div>
                            </div>

                            <div id="import-order-status" class="notice notice-info" style="display: none;"></div>

                            <p class="submit">
                                <input type="submit" class="button button-primary" value="<?php esc_attr_e('Import Orders', 'wc-bulk-order-generator'); ?>">
                                <button type="button" id="reset-order-import" class="button button-secondary"><?php esc_html_e('Reset', 'wc-bulk-order-generator'); ?></button>
                            </p>
                        </form>
                    </div>

                    <br>
                    <br>
                    <br>
                    <hr>
                    <hr>
                    <br>
                    <br>
                    <!-- Product import  -->
                    <div class="import-section">
                        <h2><?php esc_html_e('Product Import', 'wc-bulk-order-generator'); ?></h2>
                        <form id="product-import-form" enctype="multipart/form-data">
                            
                            <table class="form-table enhanced-import-settings">
                                <tr>
                                    <th scope="row">
                                        <label for="product-import-csv"><?php esc_html_e('CSV File', 'wc-bulk-order-generator'); ?></label>
                                    </th>
                                    <td>
                                        <div class="file-upload-wrapper">
                                            <input type="file" 
                                                id="product-import-csv" 
                                                name="csv_file" 
                                                accept=".csv" 
                                                class="file-upload-input" 
                                                required>
                                            <div class="file-upload-preview">
                                                <div class="upload-placeholder">
                                                    <i class="upload-icon">📤</i>
                                                    <span class="upload-text">Drag & Drop or Click to Upload CSV</span>
                                                </div>
                                            </div>
                                        </div>
                                        <p class="description"><?php esc_html_e('Upload a CSV file with product details', 'wc-bulk-order-generator'); ?></p>
                                        <div class="file-upload-validation">
                                            <small><?php esc_html_e('Accepted: .csv | 50MB only | Use only WC Bulk Generator CSV ', 'wc-bulk-order-generator'); ?></small>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">
                                        <label for="product-import-batch-size"><?php esc_html_e('Batch Size', 'wc-bulk-order-generator'); ?></label>
                                    </th>
                                    <td>
                                        <input type="number" id="product-import-batch-size" name="batch_size" 
                                            value="10" min="5" max="30">
                                        <p class="description"><?php esc_html_e('Number of products to process per batch (5-30)', 'wc-bulk-order-generator'); ?></p>
                                        
                                        <div class="poc-note">
                                            <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                            <?php esc_html_e('Adjust the batch size based on your system\'s capacity. Higher values may require more memory.', 'wc-bulk-order-generator'); ?></p>
                                        </div>

                                    </td>
                                </tr>
                            </table>

                            <div class="progress-wrapper">
                                <div class="product-import-progress-bar"></div>
                            </div>

                            <div class="stats-grid">
                                <div class="stat-card">
                                    <div class="stat-value" id="product-import-total-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Total Processed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-import-success-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Successful', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-import-failed-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Failed', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-import-skipped-count"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Skipped', 'wc-bulk-order-generator'); ?></div>
                                </div>
                                <div class="stat-card">
                                    <div class="stat-value" id="product-import-elapsed-time"><?php esc_html_e('0s', 'wc-bulk-order-generator'); ?></div>
                                    <div class="stat-label"><?php esc_html_e('Elapsed Time', 'wc-bulk-order-generator'); ?></div>
                                </div>
                            </div>

                            <div id="import-product-status" class="notice notice-info" style="display: none;"></div>

                            <p class="submit">
                                <input type="submit" class="button button-primary" value="<?php esc_attr_e('Import Products', 'wc-bulk-order-generator'); ?>">
                                <button type="button" id="reset-product-import" class="button button-secondary"><?php esc_html_e('Reset', 'wc-bulk-order-generator'); ?></button>
                            </p>
                        </form>
                    </div>
                    

                </div>

                <div id="action" class="tab-content">
                    <div class="wrap">
                        <h1> <?php esc_html_e('Delete Order/Product', 'wc-bulk-order-generator'); ?></h1>
                        
                        <div class="poc-container">
                            <div class="poc-warning">
                                <p><strong><?php esc_html_e('Warning:', 'wc-bulk-order-generator'); ?></strong> 
                                <?php esc_html_e('These actions permanently delete data and cannot be undone. Please backup your database before proceeding.', 'wc-bulk-order-generator'); ?></p>
                            </div>
                            <div class="poc-note">
                                <p><strong><?php esc_html_e('Note:', 'wc-bulk-order-generator'); ?></strong> 
                                <?php esc_html_e('If any new orders or products are found, or if errors occur during deletion, they will be automatically adjusted in deletion process. Reloading or resetting the page during deletion may interrupt the batch process.', 'wc-bulk-order-generator'); ?></p>
                            </div>
                            <br>
                            <br>

                            <!-- Delete Products Card -->
                            <div class="poc-card">
                                <h3><?php esc_html_e('Delete All Products', 'wc-bulk-order-generator'); ?></h3>
                                <p><?php esc_html_e('This will remove:', 'wc-bulk-order-generator'); ?></p>
                                <ul class="poc-list">
                                    <li><?php esc_html_e('All product data and meta', 'wc-bulk-order-generator'); ?></li>
                                    <li><?php esc_html_e('Product categories and tags', 'wc-bulk-order-generator'); ?></li>
                                    <li><?php esc_html_e('Product images', 'wc-bulk-order-generator'); ?></li>
                                    <li><?php esc_html_e('Product variations', 'wc-bulk-order-generator'); ?></li>
                                </ul>
                                
                                <div id="product-progress-container" class="poc-progress-container" style="display: none;">
                                    <div class="poc-progress-wrapper">
                                        <div id="product-progress-bar" class="poc-progress-bar" style="width: 0%"></div>
                                    </div>
                                    <p class="poc-progress-text">
                                    <?php esc_html_e('Processed:', 'wc-bulk-order-generator'); ?> <span id="product-processed">
                                        <?php esc_html_e('0', 'wc-bulk-order-generator'); ?>
                                    </span> 
                                    </span> 
                                        <?php esc_html_e('/', 'wc-bulk-order-generator'); ?> 
                                        <span id="product-total"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?>
                                    </span>
                                        <br>
                                        
                                    </p>
                                </div>

                                <div id="delete-product" class="notice notice-info" style="display: none;"></div>
                                
                                <button type="button" id="delete-products-btn" class="poc-btn delete">
                                    <span class="poc-spinner" style="display: none;"></span>
                                    <?php esc_html_e('Delete All Products', 'wc-bulk-order-generator'); ?>
                                </button>
                            </div>

                            <!-- Delete Orders Card -->
                            <div class="poc-card">
                                <h3><?php esc_html_e('Delete All Orders', 'wc-bulk-order-generator'); ?></h3>
                                <p><?php esc_html_e('This will remove:', 'wc-bulk-order-generator'); ?></p>
                                <ul class="poc-list">
                                    <li><?php esc_html_e('All order data and meta', 'wc-bulk-order-generator'); ?></li>
                                    <li><?php esc_html_e('Order items and notes', 'wc-bulk-order-generator'); ?></li>
                                    <li><?php esc_html_e('Customer order history', 'wc-bulk-order-generator'); ?></li>
                                    <li><?php esc_html_e('Related refunds', 'wc-bulk-order-generator'); ?></li>
                                </ul>
                                
                                <div id="order-progress-container" class="poc-progress-container" style="display: none;">
                                    <div class="poc-progress-wrapper">
                                        <div id="order-progress-bar" class="poc-progress-bar" style="width: 0%"></div>
                                    </div>
                                    <p class="poc-progress-text">
                                    <?php esc_html_e('Processed:', 'wc-bulk-order-generator'); ?> <span id="order-processed"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></span> <?php esc_html_e('/', 'wc-bulk-order-generator'); ?>  <span id="order-total"><?php esc_html_e('0', 'wc-bulk-order-generator'); ?></span>
                                        <br>
                                        
                                    </p>
                                </div>
                                
                                <div id="delete-order" class="notice notice-info" style="display: none;"></div>

                                <button type="button" id="delete-orders-btn" class="poc-btn delete">
                                    <span class="poc-spinner" style="display: none;"></span>
                                    <?php esc_html_e('Delete All Orders', 'wc-bulk-order-generator'); ?>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
        
            </div>
        </div>
        <?php
    }
    

    /**
     * Caches the products from the WooCommerce store.
     * 
     * This method checks if the products are already cached. If not, it retrieves all published products 
     * from the store using `wc_get_products()` and stores them in a class variable `$products_cache`. 
     * This helps in optimizing performance when accessing products multiple times.
     * 
     * @return array The list of cached or retrieved WooCommerce products.
     * 
     * @since 1.0.0
     */
    private function cache_products() {
        if (empty($this->products_cache)) {
            $this->products_cache = wc_get_products(array(
                'status' => 'publish',
                'limit' => -1
            ));
        }
        return $this->products_cache;
    }

    /**
     * Processes a batch of orders for generation.
     * 
     * This method is responsible for generating a batch of orders, creating orders with random products,
     * random shipping, and customer details. It processes multiple orders at once in a batch to improve performance 
     * when creating test orders. The method handles order creation, error logging, and transaction management.
     * 
     * It uses AJAX to handle requests from the admin interface, ensures the current user has the necessary permissions, 
     * and sends the generated orders to the database.
     * 
     * @return void JSON response with success or error data.
     * 
     * @since 1.0.0
     */
    public function process_order_batch() {
        check_ajax_referer('generate_orders_nonce', 'nonce');
        
        if (!current_user_can('manage_woocommerce')) {
            wp_send_json_error('Insufficient permissions');
            return;
        }
        
        $this->disable_emails(null);
        
        // Increase limits for long-running processes
        set_time_limit(0); // No time limit.
        ini_set('memory_limit', '1024M');
        ini_set('max_execution_time', 0);
        
        // Get batch size from POST data
        $batch_size = isset($_POST['batch_size']) ? absint($_POST['batch_size']) : 20;
        if ($batch_size < 1 || $batch_size > 100) {
            $batch_size = 20;
        }
        
        // Update the settings with new batch size
        $settings = get_option('wc_bulk_generator_settings', array());
        $settings['batch_size'] = $batch_size;
        update_option('wc_bulk_generator_settings', $settings);
        
        $success_count = 0;
        $failed_count = 0;
    
        try {
            // Cache products before the loop
            $available_products = wp_cache_get('available_products');
            if (!$available_products) {
                $available_products = $this->cache_products();
                wp_cache_set('available_products', $available_products, '', HOUR_IN_SECONDS);
            }
    
            if (empty($available_products)) {
                wp_send_json_error('No products found');
                return;
            }
    
            $max_products_per_order = absint($settings['products_per_order']);
            $date_range = absint($settings['date_range']);
    
            $country_codes = array('US', 'GB', 'CA', 'AU');
            $states = array(
                'US' => array('NY', 'CA', 'TX', 'FL', 'IL'),
                'GB' => array('LND', 'BKM', 'ESX', 'KNT'),
                'CA' => array('ON', 'BC', 'QC', 'AB'),
                'AU' => array('NSW', 'VIC', 'QLD', 'WA')
            );
            
            // Process orders in smaller chunks to prevent timeout
            $chunk_size = min(10, $batch_size);
            $chunks = ceil($batch_size / $chunk_size);
    
            global $wpdb;
            
            for ($chunk = 0; $chunk < $chunks; $chunk++) {
                $wpdb->query('START TRANSACTION'); // Begin transaction
                
                $current_chunk_size = min($chunk_size, $batch_size - ($chunk * $chunk_size));
                
                for ($i = 0; $i < $current_chunk_size; $i++) {
                    try {
                        // Check for memory usage and clean up if needed
                        if (memory_get_usage() > 83886080) { // 80MB threshold
                            wp_cache_flush();
                            gc_collect_cycles();
                        }
                        
                        $order = wc_create_order();
                        
                        $num_products = wp_rand(1, max(1, $max_products_per_order));
                        $product_keys = array_rand($available_products, $num_products);
                        if (!is_array($product_keys)) {
                            $product_keys = array($product_keys);
                        }
    
                        $order_total = 0.0;
                        foreach ($product_keys as $key) {
                            $product = $available_products[$key];
                            $quantity = wp_rand(1, 5);
                            $price = (float)$product->get_price();
                            
                            if ($price > 0) {
                                $order->add_product($product, $quantity);
                                $order_total += ($price * $quantity);
                            }
                        }
    
                        $country = $country_codes[array_rand($country_codes)];
                        $state = $states[$country][array_rand($states[$country])];
    
                        $this->set_customer_details($order, $country, $state);
    
                        $date = gmdate('Y-m-d H:i:s', strtotime('-' . wp_rand(0, $date_range) . ' days'));
                        $order->set_date_created($date);
    
                        $this->set_order_status($order, $date);
    
                        if ($order_total > 0) {
                            $this->add_shipping_and_tax($order, $order_total, $country);
                        }
    
                        $payment_methods = array('bacs', 'cheque', 'cod', 'paypal');
                        $order->set_payment_method($payment_methods[array_rand($payment_methods)]);
    
                        $order->calculate_totals();
                        $order->save();
    
                        $success_count++;
    
                    } catch (Exception $e) {
                        // error_log('Order generation error: ' . $e->getMessage());
                        $failed_count++;
                    }
                }
                
                $wpdb->query('COMMIT'); // Commit transaction
                
                // Give the server a small break between chunks
                if ($chunk < $chunks - 1) {
                    usleep(100000); // 0.1 second pause
                }
            }
    
            wp_send_json_success(array(
                'success' => $success_count,
                'failed' => $failed_count
            ));
    
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK'); // Rollback on failure
            wp_send_json_error($e->getMessage());
        }
    }
    

    /**
     * Sets the customer details for an order.
     * 
     * This method generates random customer details (first name, last name, address, email, etc.) 
     * and sets them on the order for both billing and shipping. It ensures that every generated order 
     * has unique and realistic-looking customer data.
     * 
     * @param WC_Order $order The WooCommerce order object to populate with customer data.
     * @param string $country The country code (e.g., 'US', 'GB') for setting the customer's location.
     * @param string $state The state code (e.g., 'CA', 'TX') for setting the customer's location.
     * 
     * @return void
     * 
     * @since 1.0.0
     */
    private function set_customer_details($order, $country, $state) {
        $first_names = array('John', 'Jane', 'Michael', 'Sarah', 'David', 'Emma');
        $last_names = array('Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia');
        
        $first_name = $first_names[array_rand($first_names)];
        $last_name = $last_names[array_rand($last_names)];
        $address = wp_rand(100, 9999) . ' ' . array_rand(array('Main St' => 1, 'Oak Ave' => 1, 'Market St' => 1));
        $city = array_rand(array('New York' => 1, 'Los Angeles' => 1, 'Chicago' => 1, 'Houston' => 1));
        $postcode = sprintf('%05d', wp_rand(100000, 999999));
        
        $address_data = array(
            'first_name' => $first_name,
            'last_name'  => $last_name,
            'address_1'  => $address,
            'address_2'  => wp_rand(0, 1) ? 'Apt ' . wp_rand(10000, 99999) : '',
            'city'       => $city,
            'state'      => $state,
            'postcode'   => $postcode,
            'country'    => $country
        );

        $billing_data = array_merge($address_data, array(
            'email' => strtolower($first_name . '.' . $last_name . wp_rand(10000, 9999999) . '@example.com'),
            'phone' => sprintf('(%d) %d-%d', wp_rand(2000, 9999), wp_rand(2000, 9999), wp_rand(10000, 9999999))
        ));

        foreach ($billing_data as $key => $value) {
            $method = "set_billing_{$key}";
            if (method_exists($order, $method)) {
                $order->$method($value);
            }
        }

        foreach ($address_data as $key => $value) {
            $method = "set_shipping_{$key}";
            if (method_exists($order, $method)) {
                $order->$method($value);
            }
        }
    }


    /**
     * Sets the order status based on the age of the order.
     * 
     * This method assigns a status to the given order based on how old it is. Orders that are older than 
     * 7 days have a higher likelihood of being marked as 'completed', while recent orders have a wider 
     * range of statuses (such as 'pending', 'processing', etc.). The status is chosen using weighted probabilities.
     * 
     * @param WC_Order $order The WooCommerce order object to set the status for.
     * @param string $date The date when the order was created, used to calculate the order age.
     * 
     * @return void
     * 
     * @since 1.0.0
     */
    private function set_order_status($order, $date) {
        $days_ago = (time() - strtotime($date)) / (60 * 60 * 24);
        
        if ($days_ago > 7) {
            // Older orders are more likely to be completed
            $statuses = array(
                'completed' => 70,
                'processing' => 20,
                'refunded' => 5,
                'cancelled' => 5
            );
        } else {
            // Recent orders have more varied statuses
            $statuses = array(
                'completed' => 30,
                'processing' => 40,
                'on-hold' => 15,
                'pending' => 15
            );
        }

        // Choose status based on weighted probability
        $rand = wp_rand(1, 100);
        $cumulative = 0;
        foreach ($statuses as $status => $probability) {
            $cumulative += $probability;
            if ($rand <= $cumulative) {
                $order->set_status($status);
                break;
            }
        }
    }

    /**
     * Adds shipping costs and tax to the order.
     * 
     * This method randomly selects a shipping method and calculates a shipping cost based on predefined 
     * ranges. It also calculates and adds tax to the order if the shipping country is one of the supported 
     * countries (US, CA, GB, AU). The tax is calculated as a percentage of the order total.
     * 
     * @param WC_Order $order The WooCommerce order object to which shipping and tax details are added.
     * @param float $order_total The total amount of the order, used to calculate the tax.
     * @param string $country The country code (e.g., 'US', 'CA', 'GB', 'AU') used to determine tax rates.
     * 
     * @return void
     * 
     * @since 1.0.0
     */
    private function add_shipping_and_tax($order, $order_total, $country) {
        $shipping_methods = array(
            'flat_rate' => array(5, 15),
            'free_shipping' => array(0, 0),
            'express' => array(15, 30)
        );
        
        $method = array_rand($shipping_methods);
        $range = $shipping_methods[$method];
        $shipping_cost = (float)wp_rand($range[0], $range[1]);
        
        if ($shipping_cost > 0) {
            $item = new WC_Order_Item_Shipping();
            $item->set_method_title($method);
            $item->set_total($shipping_cost);
            $order->add_item($item);
        }

        if (in_array($country, array('US', 'CA', 'GB', 'AU'))) {
            $tax_rate = (float)wp_rand(5, 20) / 100;
            $tax_total = (float)$order_total * $tax_rate;
            $order->set_cart_tax($tax_total);
        }
    }

    /**
     * Stops the order generation process.
     * 
     * This method handles an AJAX request to stop the generation of orders. It verifies the nonce for security 
     * and sends a success response back to the client. This function is useful for controlling the generation 
     * of orders through AJAX.
     * 
     * @return void
     * 
     * @since 1.0.0
     */
    public function stop_order_generation() {
        check_ajax_referer('generate_orders_nonce', 'nonce');
        wp_send_json_success();
    }

}

/**
 * Initializes the Bulk Order & Product Generator, Import Export for WooCommerce plugin.
 * 
 * This function is called when the plugins are loaded in WordPress. It initializes the `WC_Bulk_Order_Generator` 
 * class, which is responsible for generating bulk orders in WooCommerce. The function is hooked to the `plugins_loaded` 
 * action, ensuring that the class is instantiated only after all plugins are loaded.
 * 
 * @return void
 * 
 * @since 1.0.0
 */
function wc_bulk_generator_init() {
    new WC_Bulk_Order_Generator();
}
add_action('plugins_loaded', 'wc_bulk_generator_init');


