class Hito_Replacement_System {
    
    private $replacement_categories;
    
    public function __construct() {
        $this->replacement_categories = array(
            'tshirt' => array('name' => 'تیشرت', 'limit' => 3),
            'shirt' => array('name' => 'بلوز', 'limit' => 3),
            'pants' => array('name' => 'شلوار', 'limit' => 3),
            'jacket' => array('name' => 'ژاکت', 'limit' => 3) // ✅ Added jacket
        );
        
        add_action('wp_ajax_hito_get_replacement_categories', array($this, 'get_replacement_categories'));
        add_action('wp_ajax_nopriv_hito_get_replacement_categories', array($this, 'get_replacement_categories'));
        add_action('wp_ajax_hito_find_product_category', array($this, 'find_product_category'));
        add_action('wp_ajax_nopriv_hito_find_product_category', array($this, 'find_product_category'));
    }
    /**
     * Get replacement categories WITH ACTUAL PRODUCTS
     */
    public function get_replacement_categories() {
        // Verify nonce
        if (!wp_verify_nonce($_POST['nonce'], 'hito_matrix_nonce')) {
            wp_send_json_error('Invalid nonce');
            return;
        }
        $product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
        $response_data = array();
        if ($product_id) {
            // Get the category of the product being replaced
            $product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
            
            if (!empty($product_categories) && !is_wp_error($product_categories)) {
                $target_category = $product_categories[0];
                
                // Get products from the same category (excluding current product)
                $category_products = $this->get_products_by_category($target_category, $product_id, 3);
                
                if (!empty($category_products)) {
                    $response_data[$target_category] = array(
                        'name' => $this->get_category_display_name($target_category),
                        'products' => $category_products
                    ];
                    
                    wp_send_json_success($response_data);
                    return;
                }
            }
        }
        // Fallback: return all categories with their products
        foreach ($this->replacement_categories as $category_slug => $category_data) {
            $products = $this->get_products_by_category($category_slug, 0, $category_data['limit']);
            $response_data[$category_slug] = array(
                'name' => $category_data['name'],
                'products' => $products
            );
        }
        wp_send_json_success($response_data);
    }
    /**
     * Get products by category slug
     */
    private function get_products_by_category($category_slug, $exclude_product_id = 0, $limit = 3) {
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => $limit,
            'post_status' => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => $category_slug
                )
            )
        );
        if ($exclude_product_id) {
            $args['post__not_in'] = array($exclude_product_id);
        }
        $products = get_posts($args);
        $products_data = array();
        foreach ($products as $product) {
            $wc_product = wc_get_product($product->ID);
            
            $products_data[$product->ID] = array(
                'id' => $product->ID,
                'name' => $product->post_title,
                'price' => $wc_product->get_price(),
                'image' => get_the_post_thumbnail_url($product->ID, 'thumbnail') ?: wc_placeholder_img_src(),
                'product_type' => $wc_product->get_type(),
                'is_variable' => $wc_product->is_type('variable')
            );
        }
        return $products_data;
    }
    /**
     * Get category display name
     */
    private function get_category_display_name($category_slug) {
        $category = get_term_by('slug', $category_slug, 'product_cat');
        return $category ? $category->name : $this->replacement_categories[$category_slug]['name'] ?? $category_slug;
    }
    /**
     * Find which category a product belongs to
     */
    public function find_product_category() {
        // Verify nonce
        if (!wp_verify_nonce($_POST['nonce'], 'hito_matrix_nonce')) {
            wp_send_json_error('Invalid nonce');
            return;
        }
        $product_id = intval($_POST['product_id']);
        
        if (!$product_id) {
            wp_send_json_error('No product ID provided');
            return;
        }
        // Get the actual categories of this specific product
        $product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
        
        error_log("🎯 Finding category for product $product_id. Categories: " . print_r($product_categories, true));
        
        if (!empty($product_categories) && !is_wp_error($product_categories)) {
            // Return the first/main category of this product
            wp_send_json_success(array(
                'category' => $product_categories[0],
                'all_categories' => $product_categories
            ));
            return;
        }
        
        // Fallback if no category found
        wp_send_json_success(array(
            'category' => 'tshirt',
            'all_categories' => array('tshirt')
        ));
    }
Warning:  Cannot modify header information - headers already sent by (output started at /home/avantd/public_html/wp-content/plugins/hito-matrix/includes/class-hito-replacement-system.php:1) in /home/avantd/public_html/wp-includes/pluggable.php on line 1450
Warning:  Cannot modify header information - headers already sent by (output started at /home/avantd/public_html/wp-content/plugins/hito-matrix/includes/class-hito-replacement-system.php:1) in /home/avantd/public_html/wp-includes/pluggable.php on line 1453