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

class Dima_Quick_View {
    
    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_quick_view', array($this, 'ajax_quick_view'));
        add_action('wp_ajax_nopriv_dima_quick_view', array($this, 'ajax_quick_view'));
    }
    
    /**
     * دریافت اطلاعات محصول برای Quick View
     */
    public function get_product_data($product_id) {
        $product = get_post($product_id);
        if (!$product || $product->post_status !== 'publish') {
            return null;
        }
        
        $price = Dima_Product::get_price($product_id);
        $stock = Dima_Product::get_stock($product_id);
        $flash = Dima_Flash_Sale::get_instance()->get_flash_price($product_id);
        
        $data = array(
            'id' => $product_id,
            'title' => get_the_title($product_id),
            'content' => apply_filters('the_content', $product->post_content),
            'excerpt' => $product->post_excerpt,
            'price' => $price,
            'sale_price' => $flash ? $flash->sale_price : null,
            'stock' => $stock,
            'is_in_stock' => ($stock === '' || intval($stock) > 0),
            'image' => get_the_post_thumbnail_url($product_id, 'large'),
            'gallery' => $this->get_gallery_images($product_id),
            'url' => get_permalink($product_id),
            'categories' => wp_get_post_terms($product_id, 'dima_product_cat', array('fields' => 'names')),
            'rating' => get_post_meta($product_id, '_dima_avg_rating', true),
            'review_count' => get_post_meta($product_id, '_dima_review_count', true)
        );
        
        return $data;
    }
    
    /**
     * دریافت گالری تصاویر
     */
    private function get_gallery_images($product_id) {
        $gallery_ids = get_post_meta($product_id, '_dima_gallery', true);
        
        if (empty($gallery_ids)) {
            $gallery_ids = array();
        }
        
        $images = array();
        foreach ($gallery_ids as $image_id) {
            $url = wp_get_attachment_image_url($image_id, 'medium');
            if ($url) {
                $images[] = $url;
            }
        }
        
        return $images;
    }
    
    /**
     * AJAX handler
     */
    public function ajax_quick_view() {
        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;
        }
        
        $data = $this->get_product_data($product_id);
        
        if (!$data) {
            wp_send_json_error(array('message' => 'محصول یافت نشد'));
            return;
        }
        
        // تولید HTML
        ob_start();
        include DIMA_SHOP_PATH . 'templates/quick-view.php';
        $html = ob_get_clean();
        
        wp_send_json_success(array('html' => $html, 'data' => $data));
    }
}