<?php
/**
 * کلاس مقایسه محصولات
 */
if (!defined('ABSPATH')) exit;

class Dima_Compare {
    
    private static $instance = null;
    private $max_products = 4;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        add_action('wp_ajax_dima_toggle_compare', array($this, 'ajax_toggle'));
        add_action('wp_ajax_nopriv_dima_toggle_compare', array($this, 'ajax_toggle'));
        add_action('wp_ajax_dima_remove_compare', array($this, 'ajax_remove'));
        add_action('wp_ajax_nopriv_dima_remove_compare', array($this, 'ajax_remove'));
    }
    
    public function get_compare_list() {
        if (is_user_logged_in()) {
            return get_user_meta(get_current_user_id(), '_dima_compare', true) ?: array();
        } else {
            return isset($_COOKIE['dima_compare']) ? json_decode(stripslashes($_COOKIE['dima_compare']), true) : array();
        }
    }
    
    public function toggle_compare($product_id) {
        $compare = $this->get_compare_list();
        $product_id = absint($product_id);
        
        if (in_array($product_id, $compare)) {
            $compare = array_values(array_diff($compare, array($product_id)));
            $action = 'removed';
        } else {
            if (count($compare) >= $this->max_products) {
                return new WP_Error('max_reached', 'حداکثر ' . $this->max_products . ' محصول قابل مقایسه است');
            }
            $compare[] = $product_id;
            $action = 'added';
        }
        
        if (is_user_logged_in()) {
            update_user_meta(get_current_user_id(), '_dima_compare', $compare);
        } else {
            setcookie('dima_compare', wp_json_encode($compare), time() + (86400 * 30), '/');
        }
        
        return array(
            'action' => $action,
            'count' => count($compare)
        );
    }
    
    public function get_comparison_data() {
        $compare = $this->get_compare_list();
        $products = array();
        
        foreach ($compare as $product_id) {
            $product = get_post($product_id);
            if (!$product) continue;
            
            $products[] = array(
                'id' => $product_id,
                'title' => get_the_title($product_id),
                'image' => get_the_post_thumbnail_url($product_id, 'medium'),
                'price' => Dima_Product::get_price($product_id),
                'stock' => Dima_Product::get_stock($product_id),
                'sku' => Dima_Product::get_sku($product_id),
                'description' => get_post_field('post_excerpt', $product_id),
                'attributes' => Dima_Attribute::get_product_attributes($product_id),
                'rating' => get_post_meta($product_id, '_dima_avg_rating', true),
                'url' => get_permalink($product_id)
            );
        }
        
        return $products;
    }
    
    public function get_count() {
        return count($this->get_compare_list());
    }
    
    public function ajax_toggle() {
        check_ajax_referer('dima_shop_nonce', 'nonce');
        
        $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
        if ($product_id <= 0) {
            wp_send_json_error(array('message' => 'درخواست نامعتبر'));
            return;
        }
        
        $result = $this->toggle_compare($product_id);
        
        if (is_wp_error($result)) {
            wp_send_json_error(array('message' => $result->get_error_message()));
            return;
        }
        
        wp_send_json_success($result);
    }
    
    public function ajax_remove() {
        check_ajax_referer('dima_shop_nonce', 'nonce');
        
        $product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
        $this->toggle_compare($product_id);
        
        wp_send_json_success(array('count' => count($this->get_compare_list())));
    }
}