<?php
/**
 * CTA Layout for WC Product Display (Free Version).
 *
 * @package Product_Display.
 */

// Exit if accessed directly.
if ( ! defined('ABSPATH') ) {
	exit;
}

/**
 * Class PRODDISP_CTA.
 *
 * This class handles the rendering of the CTA layout for product display.
 */
class PRODDISP_CTA extends PRODDISP_Layout_Base {
	/**
	 * Render the CTA layout.
	 *
	 * @param array $products WooCommerce product objects.
	 * @return string HTML output.
	 */
	public function render( $products ) {
		// Enqueue CTA-specific assets
		PRODDISP_Assets::get_instance()->enqueue_cta_style_assets();

		$pro_result = $this->handle_pro_layout_mode('cta-layout-2', 'PRODDISP_PRO_CTA', 'cta-layout-locked-style-2.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('cta-layout-3', 'PRODDISP_PRO_CTA', 'cta-layout-locked-style-3.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('cta-layout-4', 'PRODDISP_PRO_CTA', 'cta-layout-locked-style-4.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('cta-layout-5', 'PRODDISP_PRO_CTA', 'cta-layout-locked-style-5.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('cta-layout-6', 'PRODDISP_PRO_CTA', 'cta-layout-locked-style-6.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$output = '<div class="wcps-cta-container">';
		if ( empty($products) ) {
			$output .= '<p class="wcps-no-products">' . esc_html__('No products found.', 'product-display') . '</p>';
		} else {
			foreach ( $products as $product ) {
				$output .= $this->render_product($product);
			}
		}
		$output .= '</div>';
		return $output;
	}

	/**
	 * Render a single product in the CTA layout.
	 *
	 * @param Product_Display $product The product object.
	 * @return string HTML for the product.
	 */
	private function render_product( $product ) {
		$product_id = $product->get_id();
		$permalink = get_permalink($product_id);

		// SEO optimized image with proper alt text
		$alt_text = get_post_meta($product->get_image_id(), '_wp_attachment_image_alt', true);
		if ( empty($alt_text) ) {
			$alt_text = $product->get_name();
		}
		$image = $product->get_image('woocommerce_single', [
			'class' => 'wcps-cta-image',
			'alt' => esc_attr($alt_text),
			'loading' => 'lazy',
		]);

		$title = $product->get_name();
		$rating_html = wc_get_rating_html($product->get_average_rating(), $product->get_rating_count());
		$price_html = $product->get_price_html();
		$add_to_cart = $this->render_add_to_cart_button($product);
		$actions = $this->render_product_actions($product);

		$output = '<div class="wcps-cta-item" itemscope itemtype="https://schema.org/Product">';

		// Add structured data
		$output .= '<meta itemprop="name" content="' . esc_attr($product->get_name()) . '">';
		$output .= '<meta itemprop="url" content="' . esc_url($permalink) . '">';
		if ( $product->get_sku() ) {
			$output .= '<meta itemprop="sku" content="' . esc_attr($product->get_sku()) . '">';
		}
		$output .= '<div class="wcps-cta-inner">';
		// Left: Image and actions
		$output .= '<div class="wcps-cta-left">';
		$output .= '<a href="' . esc_url($permalink) . '" class="wcps-cta-image-link">' . $image . '</a>';
		$output .= '<div class="wcps-cta-actions-on-image">' . $actions . '</div>';
		$output .= '</div>';
		// Right: Details
		$output .= '<div class="wcps-cta-right">';
		$output .= '<h2 class="wcps-cta-title" itemprop="name"><a href="' . esc_url($permalink) . '">' . esc_html($title) . '</a></h2>';
		if ( $rating_html && $product->get_rating_count() > 0 ) {
			$output .= '<div class="wcps-cta-rating" itemprop="aggregateRating" itemscope itemtype="https://schema.org/AggregateRating">';
			$output .= '<meta itemprop="ratingValue" content="' . esc_attr($product->get_average_rating()) . '">';
			$output .= '<meta itemprop="reviewCount" content="' . esc_attr($product->get_rating_count()) . '">';
			$output .= '<meta itemprop="bestRating" content="5">';
			$output .= '<meta itemprop="worstRating" content="1">';
			$output .= $rating_html;
			$output .= '</div>';
		}
		$output .= '<div class="wcps-cta-price" itemprop="offers" itemscope itemtype="https://schema.org/Offer">';
		$output .= '<meta itemprop="price" content="' . esc_attr($product->get_price()) . '">';
		$output .= '<meta itemprop="priceCurrency" content="' . esc_attr(get_woocommerce_currency()) . '">';
		$output .= '<meta itemprop="availability" content="' . ( $product->is_in_stock() ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock' ) . '">';
		$output .= $price_html;
		$output .= '</div>';
		$output .= '<div class="wcps-cta-add-to-cart">' . $add_to_cart . '</div>';
		$output .= '</div>';
		$output .= '</div>';
		$output .= '</div>';
		return $output;
	}
}
