<?php
/**
 * کلاس علاقه‌مندی‌ها (Wishlist)
 */
if (!defined('ABSPATH')) exit;

class Dima_Wishlist {
    
    private static $instance = null;
    
    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_wishlist', array($this, 'ajax_toggle'));
        add_action('wp_ajax_nopriv_dima_toggle_wishlist', array($this, 'ajax_toggle'));
        add_action('wp_ajax_dima_get_wishlist', array($this, 'ajax_get_wishlist'));
        add_action('wp_ajax_nopriv_dima_get_wishlist', array($this, 'ajax_get_wishlist'));
    }
    
    public function get_wishlist() {
        if (is_user_logged_in()) {
            $user_id = get_current_user_id();
            return get_user_meta($user_id, '_dima_wishlist', true) ?: array();
        } else {
            return isset($_COOKIE['dima_wishlist']) ? json_decode(stripslashes($_COOKIE['dima_wishlist']), true) : array();
        }
    }
    
    public function toggle_wishlist($product_id) {
        $wishlist = $this->get_wishlist();
        $product_id = absint($product_id);
        
        if (in_array($product_id, $wishlist)) {
            $wishlist = array_values(array_diff($wishlist, array($product_id)));
            $action = 'removed';
        } else {
            $wishlist[] = $product_id;
            $action = 'added';
        }
        
        if (is_user_logged_in()) {
            update_user_meta(get_current_user_id(), '_dima_wishlist', $wishlist);
        } else {
            setcookie('dima_wishlist', wp_json_encode($wishlist), time() + (86400 * 30), '/');
        }
        
        return array(
            'action' => $action,
            'count' => count($wishlist),
            'wishlist' => $wishlist
        );
    }
    
    public function is_in_wishlist($product_id) {
        return in_array(absint($product_id), $this->get_wishlist());
    }
    
    public function get_count() {
        return count($this->get_wishlist());
    }
    
    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_wishlist($product_id);
        wp_send_json_success($result);
    }
    
    public function ajax_get_wishlist() {
        check_ajax_referer('dima_shop_nonce', 'nonce');
        
        $wishlist = $this->get_wishlist();
        $products = array();
        
        foreach ($wishlist as $product_id) {
            $product = get_post($product_id);
            if (!$product || $product->post_status !== 'publish') continue;
            
            $products[] = array(
                'id' => $product_id,
                'title' => get_the_title($product_id),
                'price' => Dima_Product::get_price($product_id),
                'image' => get_the_post_thumbnail_url($product_id, 'thumbnail'),
                'url' => get_permalink($product_id)
            );
        }
        
        wp_send_json_success(array('products' => $products));
    }
}