<?php
/**
 * Widget Name: Compare Table (Pro)
 * Description: Builds a side-by-side WooCommerce product comparison table —
 *              products as columns, features as rows (image, price, rating, SKU,
 *              stock, short description, attributes and add-to-cart) — with a
 *              highlighted column, sticky labels and multiple attractive styles.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.4.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

/**
 * Class Captwiki_Woo_Compare_Table
 *
 * @since 1.4.0
 */
class Captwiki_Woo_Compare_Table extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-compare';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Compare Table', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-table captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-woo-commerce' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'product', 'compare', 'comparison', 'table', 'versus', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.4.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Available visual styles.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Striped', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Bordered', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Cards', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Minimal', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Dark Header', 'captain-widgets-kit' ),
			'style-6' => esc_html__( 'Style 6 – Modern', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Build a list of published products for the source selector.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	private function captwiki_get_products_options(): array {

		$options = array();

		if ( ! function_exists( 'wc_get_products' ) ) {
			return $options;
		}

		$products = wc_get_products(
			array(
				'status'  => 'publish',
				'limit'   => 100,
				'orderby' => 'title',
				'order'   => 'ASC',
				'return'  => 'objects',
			)
		);

		foreach ( $products as $product ) {
			$options[ $product->get_id() ] = $product->get_name();
		}

		return $options;
	}

	/**
	 * Register controls.
	 *
	 * @since 1.4.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_rows_controls();
		$this->register_header_style_controls();
		$this->register_cell_style_controls();
		$this->register_button_style_controls();
	}

	/**
	 * Content tab controls.
	 *
	 * @since 1.4.0
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Compare', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'products',
			array(
				'label'       => esc_html__( 'Products', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT2,
				'multiple'    => true,
				'label_block' => true,
				'options'     => $this->captwiki_get_products_options(),
				'description' => esc_html__( 'Pick the products to compare (left to right).', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'highlight_index',
			array(
				'label'       => esc_html__( 'Highlight Column', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 20,
				'default'     => 0,
				'description' => esc_html__( 'Emphasize one product column (1 = first). 0 = none.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'highlight_label',
			array(
				'label'     => esc_html__( 'Highlight Ribbon Text', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Recommended', 'captain-widgets-kit' ),
				'condition' => array( 'highlight_index!' => 0 ),
			)
		);

		$this->add_control(
			'sticky_labels',
			array(
				'label'        => esc_html__( 'Sticky Label Column', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'link_titles',
			array(
				'label'        => esc_html__( 'Link Product Titles', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Rows (features) controls.
	 *
	 * @since 1.4.0
	 */
	private function register_rows_controls(): void {

		$this->start_controls_section(
			'section_rows',
			array(
				'label' => esc_html__( 'Rows', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$rows = array(
			'image'       => array( esc_html__( 'Image', 'captain-widgets-kit' ), 'yes', '' ),
			'price'       => array( esc_html__( 'Price', 'captain-widgets-kit' ), 'yes', esc_html__( 'Price', 'captain-widgets-kit' ) ),
			'rating'      => array( esc_html__( 'Rating', 'captain-widgets-kit' ), 'yes', esc_html__( 'Rating', 'captain-widgets-kit' ) ),
			'sku'         => array( esc_html__( 'SKU', 'captain-widgets-kit' ), 'no', esc_html__( 'SKU', 'captain-widgets-kit' ) ),
			'stock'       => array( esc_html__( 'Stock', 'captain-widgets-kit' ), 'yes', esc_html__( 'Availability', 'captain-widgets-kit' ) ),
			'description' => array( esc_html__( 'Short Description', 'captain-widgets-kit' ), 'no', esc_html__( 'Summary', 'captain-widgets-kit' ) ),
			'attributes'  => array( esc_html__( 'Attributes', 'captain-widgets-kit' ), 'no', '' ),
			'add_to_cart' => array( esc_html__( 'Add To Cart', 'captain-widgets-kit' ), 'yes', '' ),
		);

		foreach ( $rows as $key => $data ) {
			list( $title, $default, $label ) = $data;

			$this->add_control(
				'show_' . $key,
				array(
					'label'        => $title,
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
					'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
					'return_value' => 'yes',
					'default'      => $default,
					'separator'    => 'before',
				)
			);

			if ( '' !== $label ) {
				$this->add_control(
					'label_' . $key,
					array(
						'label'     => esc_html__( 'Row Label', 'captain-widgets-kit' ),
						'type'      => Controls_Manager::TEXT,
						'default'   => $label,
						'condition' => array( 'show_' . $key => 'yes' ),
					)
				);
			}
		}

		$this->add_control(
			'image_size',
			array(
				'label'      => esc_html__( 'Image Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 40, 'max' => 320 ) ),
				'default'    => array( 'unit' => 'px', 'size' => 120 ),
				'separator'  => 'before',
				'condition'  => array( 'show_image' => 'yes' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-compare__img img' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Header (product columns) style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_header_style_controls(): void {

		$head = '{{WRAPPER}} .captwiki-compare__phead';

		$this->start_controls_section(
			'section_header_style',
			array(
				'label' => esc_html__( 'Product Header', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'head_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$head => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'head_title_color',
			array(
				'label'     => esc_html__( 'Title Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$head . ' .captwiki-compare__title, ' . $head . ' .captwiki-compare__title a' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'head_title_typography',
				'selector' => $head . ' .captwiki-compare__title',
			)
		);

		$this->add_control(
			'highlight_color',
			array(
				'label'     => esc_html__( 'Highlight Accent', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-compare__col--highlight' => 'border-color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-compare__ribbon'         => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Cell / label style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_cell_style_controls(): void {

		$this->start_controls_section(
			'section_cell_style',
			array(
				'label' => esc_html__( 'Cells & Labels', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'label_bg',
			array(
				'label'     => esc_html__( 'Label Column Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-compare__label' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'label_color',
			array(
				'label'     => esc_html__( 'Label Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-compare__label' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'cell_color',
			array(
				'label'     => esc_html__( 'Cell Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-compare__cell' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'cell_typography',
				'selector' => '{{WRAPPER}} .captwiki-compare__cell, {{WRAPPER}} .captwiki-compare__label',
			)
		);

		$this->add_control(
			'border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-compare__table th, {{WRAPPER}} .captwiki-compare__table td' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'stripe_color',
			array(
				'label'     => esc_html__( 'Stripe Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-compare.captwiki-style-1 .captwiki-compare__row:nth-child(even) td, {{WRAPPER}} .captwiki-compare.captwiki-style-1 .captwiki-compare__row:nth-child(even) th' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'cell_padding',
			array(
				'label'      => esc_html__( 'Cell Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'separator'  => 'before',
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-compare__table th, {{WRAPPER}} .captwiki-compare__table td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Add to cart button style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_button_style_controls(): void {

		$btn = '{{WRAPPER}} .captwiki-compare__cell .button';

		$this->start_controls_section(
			'section_button_style',
			array(
				'label'     => esc_html__( 'Add To Cart Button', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_add_to_cart' => 'yes' ),
			)
		);

		$this->start_controls_tabs( 'btn_tabs' );

		$this->start_controls_tab( 'btn_normal', array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'btn_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'btn_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'btn_hover', array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'btn_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn . ':hover' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'btn_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn . ':hover' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'btn_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'separator'  => 'before',
				'selectors'  => array(
					$btn => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Compact star markup for a rating value.
	 *
	 * @since 1.4.0
	 * @param float $rating Average rating (0–5).
	 * @return string
	 */
	private function captwiki_stars_html( float $rating ): string {

		$percent = max( 0, min( 100, ( $rating / 5 ) * 100 ) );
		$stars   = '★★★★★';

		return '<span class="captwiki-compare__stars"><span class="captwiki-compare__stars-empty">' . $stars . '</span><span class="captwiki-compare__stars-filled" style="width:' . esc_attr( (string) round( $percent, 2 ) ) . '%">' . $stars . '</span></span>';
	}

	/**
	 * Build the add-to-cart link markup for a product.
	 *
	 * @since 1.4.0
	 * @param \WC_Product $product Product.
	 * @return string
	 */
	private function captwiki_add_to_cart_html( $product ): string {

		$classes = array_filter(
			array(
				'button',
				'product_type_' . $product->get_type(),
				( $product->is_purchasable() && $product->is_in_stock() ) ? 'add_to_cart_button' : '',
				( $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ) ? 'ajax_add_to_cart' : '',
			)
		);

		$aria = method_exists( $product, 'add_to_cart_description' ) ? $product->add_to_cart_description() : $product->add_to_cart_text();

		$link = sprintf(
			'<a href="%s" data-quantity="1" class="%s" %s>%s</a>',
			esc_url( $product->add_to_cart_url() ),
			esc_attr( implode( ' ', $classes ) ),
			wc_implode_html_attributes(
				array(
					'data-product_id'  => $product->get_id(),
					'data-product_sku' => $product->get_sku(),
					'aria-label'       => $aria,
					'rel'              => 'nofollow',
				)
			),
			esc_html( $product->add_to_cart_text() )
		);

		return apply_filters( 'woocommerce_loop_add_to_cart_link', $link, $product );
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.4.0
	 * @param string $message Notice text.
	 */
	private function captwiki_render_editor_notice( string $message ): void {

		if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		echo '<div class="captwiki-compare captwiki-compare--notice"><p>' . esc_html( $message ) . '</p></div>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.4.0
	 */
	protected function render(): void {

		if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'wc_get_product' ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'WooCommerce is required for the Compare Table widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings = $this->get_settings_for_display();
		$ids      = ! empty( $settings['products'] ) ? array_map( 'absint', (array) $settings['products'] ) : array();

		$products = array();
		foreach ( $ids as $id ) {
			$p = wc_get_product( $id );
			if ( $p instanceof \WC_Product ) {
				$products[] = $p;
			}
		}

		if ( empty( $products ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'Select one or more products to compare.', 'captain-widgets-kit' ) );
			return;
		}

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = ! empty( $settings['widget_style'] ) && in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		wp_enqueue_style(
			'captwiki-woo-compare-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-compare-table/captwiki-woo-compare-table-' . $style . '.css',
			array( 'captwiki-woo-compare' ),
			CAPTWIKI_VERSION
		);

		$show = function ( $key ) use ( $settings ) {
			return ! empty( $settings[ 'show_' . $key ] ) && 'yes' === $settings[ 'show_' . $key ];
		};

		$label = function ( $key, $fallback ) use ( $settings ) {
			return ! empty( $settings[ 'label_' . $key ] ) ? $settings[ 'label_' . $key ] : $fallback;
		};

		$highlight   = ! empty( $settings['highlight_index'] ) ? absint( $settings['highlight_index'] ) - 1 : -1;
		$sticky      = ! empty( $settings['sticky_labels'] ) && 'yes' === $settings['sticky_labels'];
		$link_titles = ! empty( $settings['link_titles'] ) && 'yes' === $settings['link_titles'];
		$count       = count( $products );

		// Pre-compute attribute union if needed.
		$attr_labels = array();
		$attr_values = array();
		if ( $show( 'attributes' ) ) {
			foreach ( $products as $i => $product ) {
				foreach ( $product->get_attributes() as $attribute ) {
					if ( ! $attribute instanceof \WC_Product_Attribute || ! $attribute->get_visible() ) {
						continue;
					}
					$name                 = $attribute->get_name();
					$attr_labels[ $name ] = wc_attribute_label( $name );

					if ( $attribute->is_taxonomy() ) {
						$vals = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'names' ) );
					} else {
						$vals = $attribute->get_options();
					}

					$attr_values[ $i ][ $name ] = ! empty( $vals ) ? implode( ', ', $vals ) : '—';
				}
			}
		}

		if ( $show( 'add_to_cart' ) ) {
			wp_enqueue_script( 'wc-add-to-cart' );
		}

		$wrapper_classes = array(
			'captwiki-compare',
			'captwiki-' . $style,
			'captwiki-widget-' . $this->get_id(),
		);

		if ( $sticky ) {
			$wrapper_classes[] = 'captwiki-compare--sticky';
		}

		// Helper to emit a column class for highlight.
		$col_class = function ( $i ) use ( $highlight ) {
			return ( $i === $highlight ) ? ' captwiki-compare__col--highlight' : '';
		};

		echo '<div class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '">';
		echo '<div class="captwiki-compare__scroll">';
		echo '<table class="captwiki-compare__table">';

		// Header: image + title per product.
		echo '<thead><tr>';
		echo '<th class="captwiki-compare__label captwiki-compare__corner"></th>';
		foreach ( $products as $i => $product ) {
			echo '<th class="captwiki-compare__phead captwiki-compare__cell' . esc_attr( $col_class( $i ) ) . '">';

			if ( $i === $highlight && ! empty( $settings['highlight_label'] ) ) {
				echo '<span class="captwiki-compare__ribbon">' . esc_html( $settings['highlight_label'] ) . '</span>';
			}

			if ( $show( 'image' ) ) {
				$img = $product->get_image( 'woocommerce_thumbnail' );
				echo '<span class="captwiki-compare__img">' . wp_kses_post( $img ) . '</span>';
			}

			$title = esc_html( $product->get_name() );
			if ( $link_titles ) {
				$title = '<a href="' . esc_url( get_permalink( $product->get_id() ) ) . '">' . $title . '</a>';
			}
			echo '<span class="captwiki-compare__title">' . wp_kses_post( $title ) . '</span>';

			echo '</th>';
		}
		echo '</tr></thead>';

		echo '<tbody>';

		// Price.
		if ( $show( 'price' ) ) {
			echo '<tr class="captwiki-compare__row captwiki-compare__row--price">';
			echo '<th class="captwiki-compare__label">' . esc_html( $label( 'price', esc_html__( 'Price', 'captain-widgets-kit' ) ) ) . '</th>';
			foreach ( $products as $i => $product ) {
				$val = $product->get_price_html();
				echo '<td class="captwiki-compare__cell' . esc_attr( $col_class( $i ) ) . '">' . ( $val ? wp_kses_post( $val ) : '—' ) . '</td>';
			}
			echo '</tr>';
		}

		// Rating.
		if ( $show( 'rating' ) ) {
			echo '<tr class="captwiki-compare__row captwiki-compare__row--rating">';
			echo '<th class="captwiki-compare__label">' . esc_html( $label( 'rating', esc_html__( 'Rating', 'captain-widgets-kit' ) ) ) . '</th>';
			foreach ( $products as $i => $product ) {
				$avg   = (float) $product->get_average_rating();
				$cnt   = (int) $product->get_review_count();
				$cell  = $cnt > 0 ? $this->captwiki_stars_html( $avg ) . '<span class="captwiki-compare__rcount">(' . esc_html( number_format_i18n( $cnt ) ) . ')</span>' : '—';
				echo '<td class="captwiki-compare__cell' . esc_attr( $col_class( $i ) ) . '">' . wp_kses_post( $cell ) . '</td>';
			}
			echo '</tr>';
		}

		// SKU.
		if ( $show( 'sku' ) ) {
			echo '<tr class="captwiki-compare__row captwiki-compare__row--sku">';
			echo '<th class="captwiki-compare__label">' . esc_html( $label( 'sku', esc_html__( 'SKU', 'captain-widgets-kit' ) ) ) . '</th>';
			foreach ( $products as $i => $product ) {
				$sku = $product->get_sku();
				echo '<td class="captwiki-compare__cell' . esc_attr( $col_class( $i ) ) . '">' . ( '' !== $sku ? esc_html( $sku ) : '—' ) . '</td>';
			}
			echo '</tr>';
		}

		// Stock.
		if ( $show( 'stock' ) ) {
			echo '<tr class="captwiki-compare__row captwiki-compare__row--stock">';
			echo '<th class="captwiki-compare__label">' . esc_html( $label( 'stock', esc_html__( 'Availability', 'captain-widgets-kit' ) ) ) . '</th>';
			foreach ( $products as $i => $product ) {
				$in    = $product->is_in_stock();
				$state = $in ? 'in' : 'out';
				$text  = $in ? esc_html__( 'In stock', 'captain-widgets-kit' ) : esc_html__( 'Out of stock', 'captain-widgets-kit' );
				echo '<td class="captwiki-compare__cell' . esc_attr( $col_class( $i ) ) . '"><span class="captwiki-compare__stock captwiki-compare__stock--' . esc_attr( $state ) . '">' . esc_html( $text ) . '</span></td>';
			}
			echo '</tr>';
		}

		// Short description.
		if ( $show( 'description' ) ) {
			echo '<tr class="captwiki-compare__row captwiki-compare__row--desc">';
			echo '<th class="captwiki-compare__label">' . esc_html( $label( 'description', esc_html__( 'Summary', 'captain-widgets-kit' ) ) ) . '</th>';
			foreach ( $products as $i => $product ) {
				$short = $product->get_short_description();
				$cell  = '' !== trim( $short ) ? wp_kses_post( wpautop( $short ) ) : '—';
				echo '<td class="captwiki-compare__cell captwiki-compare__cell--desc' . esc_attr( $col_class( $i ) ) . '">' . $cell . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}
			echo '</tr>';
		}

		// Attributes (union).
		if ( $show( 'attributes' ) && ! empty( $attr_labels ) ) {
			foreach ( $attr_labels as $name => $attr_label ) {
				echo '<tr class="captwiki-compare__row captwiki-compare__row--attr">';
				echo '<th class="captwiki-compare__label">' . esc_html( $attr_label ) . '</th>';
				foreach ( $products as $i => $product ) {
					$val = isset( $attr_values[ $i ][ $name ] ) ? $attr_values[ $i ][ $name ] : '—';
					echo '<td class="captwiki-compare__cell' . esc_attr( $col_class( $i ) ) . '">' . esc_html( $val ) . '</td>';
				}
				echo '</tr>';
			}
		}

		// Add to cart.
		if ( $show( 'add_to_cart' ) ) {
			echo '<tr class="captwiki-compare__row captwiki-compare__row--cart">';
			echo '<th class="captwiki-compare__label"></th>';
			foreach ( $products as $i => $product ) {
				echo '<td class="captwiki-compare__cell captwiki-compare__cell--cart' . esc_attr( $col_class( $i ) ) . '">' . $this->captwiki_add_to_cart_html( $product ) . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}
			echo '</tr>';
		}

		echo '</tbody>';
		echo '</table>';
		echo '</div>';
		echo '</div>';
	}
}
