<?php
/**
 * Shared base for product-grid Woo widgets (Related Products, Upsell Products).
 *
 * Holds the common controls and the grid/card rendering. Subclasses only supply
 * their identity (name/title/icon) and the list of product IDs to display.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.3.0
 */

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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;

/**
 * Class Captwiki_Woo_Products_Base
 *
 * @since 1.3.0
 */
abstract class Captwiki_Woo_Products_Base extends Widget_Base {

	/**
	 * Asset slug, e.g. "woo-related-products".
	 *
	 * @since 1.3.0
	 * @return string
	 */
	abstract protected function captwiki_css_slug(): string;

	/**
	 * Default heading text.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	abstract protected function captwiki_default_heading(): string;

	/**
	 * Collect the product IDs to display for the resolved product.
	 *
	 * @since 1.3.0
	 * @param \WC_Product $product Source product.
	 * @param int         $limit   Maximum number of products.
	 * @return int[]
	 */
	abstract protected function captwiki_get_product_ids( \WC_Product $product, int $limit ): array;

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

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.3.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.3.0
	 * @return array
	 */
	protected function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Classic', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Card', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Overlay', 'captain-widgets-kit' ),
		);
	}

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

		$options = array();

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

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

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

		return $options;
	}

	/**
	 * Register controls.
	 *
	 * @since 1.3.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_heading_style_controls();
		$this->register_card_style_controls();
		$this->register_title_price_style_controls();
		$this->register_button_style_controls();
	}

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

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Products', '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(
			'product_source',
			array(
				'label'   => esc_html__( 'Based On', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'current',
				'options' => array(
					'current'  => esc_html__( 'Current Product', 'captain-widgets-kit' ),
					'specific' => esc_html__( 'Select a Product', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'product_id',
			array(
				'label'       => esc_html__( 'Choose Product', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'options'     => $this->captwiki_get_products_options(),
				'condition'   => array( 'product_source' => 'specific' ),
			)
		);

		$this->add_control(
			'limit',
			array(
				'label'   => esc_html__( 'Products', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 24,
				'default' => 4,
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'          => esc_html__( 'Columns', 'captain-widgets-kit' ),
				'type'           => Controls_Manager::SLIDER,
				'range'          => array( 'px' => array( 'min' => 1, 'max' => 6, 'step' => 1 ) ),
				'default'        => array( 'size' => 4 ),
				'tablet_default' => array( 'size' => 3 ),
				'mobile_default' => array( 'size' => 2 ),
				'selectors'      => array(
					'{{WRAPPER}} .captwiki-products__grid' => 'grid-template-columns: repeat({{SIZE}}, 1fr);',
				),
			)
		);

		$this->add_control(
			'show_heading',
			array(
				'label'        => esc_html__( 'Show Heading', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'heading_text',
			array(
				'label'       => esc_html__( 'Heading', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => $this->captwiki_default_heading(),
				'label_block' => true,
				'condition'   => array( 'show_heading' => 'yes' ),
			)
		);

		$this->add_control(
			'display_heading',
			array(
				'label'     => esc_html__( 'Card Elements', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'show_image',
			array(
				'label'        => esc_html__( 'Image', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_sale_badge',
			array(
				'label'        => esc_html__( 'Sale Badge', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => array( 'show_image' => 'yes' ),
			)
		);

		$this->add_control(
			'show_title',
			array(
				'label'        => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_rating',
			array(
				'label'        => esc_html__( 'Rating', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_price',
			array(
				'label'        => esc_html__( 'Price', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_cart',
			array(
				'label'        => esc_html__( 'Add to Cart', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_responsive_control(
			'content_align',
			array(
				'label'     => esc_html__( 'Text Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'left',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-products__card' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Heading style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_heading_style_controls(): void {

		$selector = '{{WRAPPER}} .captwiki-products__heading';

		$this->start_controls_section(
			'section_style_heading',
			array(
				'label'     => esc_html__( 'Heading', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_heading' => 'yes' ),
			)
		);

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => $selector,
			)
		);

		$this->add_responsive_control(
			'heading_spacing',
			array(
				'label'      => esc_html__( 'Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 80, 'step' => 1 ) ),
				'selectors'  => array(
					$selector => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Card + image style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_card_style_controls(): void {

		$card_selector = '{{WRAPPER}} .captwiki-products__card';
		$img_selector  = '{{WRAPPER}} .captwiki-products__media img';

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

		$this->add_responsive_control(
			'grid_gap',
			array(
				'label'      => esc_html__( 'Grid Gap', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 60, 'step' => 1 ) ),
				'default'    => array( 'size' => 24 ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-products__grid' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

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

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'card_border',
				'selector' => $card_selector,
			)
		);

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

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'card_box_shadow',
				'selector' => $card_selector,
			)
		);

		$this->add_responsive_control(
			'card_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$card_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

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

		$this->end_controls_section();
	}

	/**
	 * Title / price style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_title_price_style_controls(): void {

		$title_selector = '{{WRAPPER}} .captwiki-products__title';
		$price_selector = '{{WRAPPER}} .captwiki-products__price';

		$this->start_controls_section(
			'section_style_text',
			array(
				'label' => esc_html__( 'Title & Price', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'title_heading',
			array(
				'label' => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => $title_selector,
			)
		);

		$this->add_control(
			'price_heading',
			array(
				'label'     => esc_html__( 'Price', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'price_typography',
				'selector' => $price_selector,
			)
		);

		$this->end_controls_section();
	}

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

		$button_selector = '{{WRAPPER}} .captwiki-products__cart a.button';

		$this->start_controls_section(
			'section_style_button',
			array(
				'label'     => esc_html__( 'Add to Cart', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_cart' => 'yes' ),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'button_typography',
				'selector' => $button_selector,
			)
		);

		$this->start_controls_tabs( 'tabs_button' );

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

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

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

			$this->end_controls_tab();

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

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

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

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_button', array( 'type' => Controls_Manager::DIVIDER ) );

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

		$this->add_responsive_control(
			'button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$button_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Resolve the source product.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 * @return \WC_Product|false
	 */
	protected function captwiki_resolve_product( array $settings ) {

		$source = ! empty( $settings['product_source'] ) ? $settings['product_source'] : 'current';

		if ( 'specific' === $source && ! empty( $settings['product_id'] ) ) {
			return wc_get_product( absint( $settings['product_id'] ) );
		}

		$product = wc_get_product( get_the_ID() );

		if ( $product ) {
			return $product;
		}

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() || \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
			$latest = wc_get_products(
				array(
					'status' => 'publish',
					'limit'  => 1,
				)
			);

			if ( ! empty( $latest ) ) {
				return $latest[0];
			}
		}

		return false;
	}

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

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

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

	/**
	 * Render a single product card.
	 *
	 * @since 1.3.0
	 * @param \WC_Product $product Product to render.
	 * @param array       $settings Widget settings.
	 */
	protected function captwiki_render_card( \WC_Product $product, array $settings ): void {

		$show_image  = ! empty( $settings['show_image'] ) && 'yes' === $settings['show_image'];
		$show_sale   = ! empty( $settings['show_sale_badge'] ) && 'yes' === $settings['show_sale_badge'];
		$show_title  = ! empty( $settings['show_title'] ) && 'yes' === $settings['show_title'];
		$show_rating = ! empty( $settings['show_rating'] ) && 'yes' === $settings['show_rating'];
		$show_price  = ! empty( $settings['show_price'] ) && 'yes' === $settings['show_price'];
		$show_cart   = ! empty( $settings['show_cart'] ) && 'yes' === $settings['show_cart'];

		$permalink = get_permalink( $product->get_id() );

		echo '<div class="captwiki-products__card">';

		if ( $show_image ) {
			echo '<a class="captwiki-products__media" href="' . esc_url( $permalink ) . '">';
			if ( $show_sale && $product->is_on_sale() ) {
				echo '<span class="captwiki-products__sale">' . esc_html__( 'Sale!', 'captain-widgets-kit' ) . '</span>';
			}
			// Trusted core image markup (srcset attributes wp_kses_post would strip).
			echo $product->get_image( 'woocommerce_thumbnail' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo '</a>';
		}

		echo '<div class="captwiki-products__body">';

		if ( $show_title ) {
			echo '<h3 class="captwiki-products__title"><a href="' . esc_url( $permalink ) . '">' . esc_html( $product->get_name() ) . '</a></h3>';
		}

		if ( $show_rating && wc_review_ratings_enabled() ) {
			$rating_html = wc_get_rating_html( $product->get_average_rating() );
			if ( $rating_html ) {
				echo '<div class="captwiki-products__rating">' . $rating_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}
		}

		if ( $show_price && '' !== $product->get_price_html() ) {
			echo '<div class="captwiki-products__price">' . $product->get_price_html() . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		if ( $show_cart ) {
			$prev_product       = isset( $GLOBALS['product'] ) ? $GLOBALS['product'] : null;
			$GLOBALS['product'] = $product;

			echo '<div class="captwiki-products__cart">';
			woocommerce_template_loop_add_to_cart(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo '</div>';

			$GLOBALS['product'] = $prev_product;
		}

		echo '</div>'; // .captwiki-products__body
		echo '</div>'; // .captwiki-products__card
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.3.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 this widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings = $this->get_settings_for_display();
		$product  = $this->captwiki_resolve_product( $settings );

		if ( ! $product instanceof \WC_Product ) {
			$this->captwiki_render_editor_notice( esc_html__( 'No product found. Place this widget on a product template or select a product.', 'captain-widgets-kit' ) );
			return;
		}

		$limit = ! empty( $settings['limit'] ) ? absint( $settings['limit'] ) : 4;
		$ids   = $this->captwiki_get_product_ids( $product, $limit );

		if ( empty( $ids ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'No products to display for this selection.', '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';
		$slug           = $this->captwiki_css_slug();

		wp_enqueue_style(
			$this->get_name() . '-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/' . $slug . '/captwiki-' . $slug . '-' . $style . '.css',
			array( $this->get_name() ),
			CAPTWIKI_VERSION
		);

		if ( ! empty( $settings['show_cart'] ) && 'yes' === $settings['show_cart'] ) {
			wp_enqueue_script( 'wc-add-to-cart' );
		}

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

		echo '<div class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '">';

		if ( ! empty( $settings['show_heading'] ) && 'yes' === $settings['show_heading'] && ! empty( $settings['heading_text'] ) ) {
			echo '<h2 class="captwiki-products__heading">' . esc_html( $settings['heading_text'] ) . '</h2>';
		}

		echo '<div class="captwiki-products__grid">';

		foreach ( $ids as $id ) {
			$card_product = wc_get_product( $id );

			if ( $card_product instanceof \WC_Product && $card_product->is_visible() ) {
				$this->captwiki_render_card( $card_product, $settings );
			}
		}

		echo '</div>'; // .captwiki-products__grid
		echo '</div>'; // .captwiki-products
	}
}
