<?php
/**
 * Card Layout for WC Product Display.
 *
 * @package Product_Display.
 */

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

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

		// Handle pro layout modes
		$pro_result = $this->handle_pro_layout_mode('card-grid-2', 'PRODDISP_PRO_Card', 'card-grid-locked-style-2.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

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

		$columns = absint($this->settings['columns']);
		if ( $columns < 1 ) {
			$columns = 4;
		}

		$output = '<div class="wcps-card-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 card layout.
	 *
	 * @param Product_Display $product The product object.
	 * @return string HTML for the product.
	 */
	private function render_product( $product ) {
		$output = '<div class="wcps-card-item" itemscope itemtype="https://schema.org/Product">';
		$output .= '<div class="wcps-product">';

		// Add structured data
		$output .= '<meta itemprop="name" content="' . esc_attr($product->get_name()) . '">';
		$output .= '<meta itemprop="url" content="' . esc_url(get_permalink($product->get_id())) . '">';
		if ( $product->get_sku() ) {
			$output .= '<meta itemprop="sku" content="' . esc_attr($product->get_sku()) . '">';
		}

		// Product image.
		$output .= '<div class="wcps-product-image">';

		// Add sale badge if product is on sale.
		if ( $product->is_on_sale() ) {
			$sale_percentage = '';
			if ( $product->get_regular_price() && $product->get_sale_price() ) {
				$percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
				$sale_percentage = '-' . $percentage . '%';
			}
			$badge_text = $sale_percentage ? $sale_percentage : esc_html__('Sale', 'product-display');
			$output .= '<span class="wcps-product-badge" aria-label="' . esc_attr__('On Sale', 'product-display') . '">' . $badge_text . '</span>';
		}

		$output .= '<a href="' . esc_url(get_permalink($product->get_id())) . '">';

		if ( $product->get_image_id() ) {
			$alt_text = get_post_meta($product->get_image_id(), '_wp_attachment_image_alt', true);
			if ( empty($alt_text) ) {
				$alt_text = $product->get_name();
			}
			$output .= wp_get_attachment_image($product->get_image_id(), $this->settings['image_size'], false, array(
				'class' => 'wcps-image',
				'loading' => 'lazy',
				'alt' => esc_attr($alt_text),
			));
		} else {
			$output .= wc_placeholder_img($this->settings['image_size'], array(
				'class' => 'wcps-image',
				'alt' => esc_attr($product->get_name() . ' - ' . __('Product Image', 'product-display')),
			));
		}

		$output .= '</a>';
		// Show action buttons if position is hover.
		if ( 'hover' === $this->settings['action_position'] ) {
			$output .= $this->render_product_actions($product);
		}

		// Show action buttons if position is panel.
		if ( 'panel' === $this->settings['action_position'] ) {
			$output .= $this->render_product_actions($product);
		}

		$output .= '</div>'; // End .wcps-product-image.

		// Product content.
		$output .= '<div class="wcps-product-content">';

		// Add category if enabled.
		if ( isset($this->settings['show_category']) && $this->settings['show_category'] ) {
			$categories = $product->get_category_ids();
			if ( ! empty($categories) ) {
				$first_category = get_term($categories[0], 'product_cat');
				if ( $first_category && ! is_wp_error($first_category) ) {
					$output .= '<div class="wcps-product-category"><a href="' . esc_url(get_term_link($first_category)) . '">' . esc_html($first_category->name) . '</a></div>';
				}
			}
		}

		// Product title.
		if ( isset($this->settings['show_title']) && $this->settings['show_title'] === 'yes' ) {
			$output .= '<h2 class="wcps-product-title" itemprop="name"><a href="' . esc_url(get_permalink($product->get_id())) . '">' . esc_html($product->get_name()) . '</a></h2>';
		}

		// Product price.
		if ( isset($this->settings['show_price']) && $this->settings['show_price'] === 'yes' ) {
			$output .= '<div class="wcps-product-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 .= $product->get_price_html();
			$output .= '</div>';
		}

		// Product rating.
		if ( isset($this->settings['show_rating']) && $this->settings['show_rating'] === 'yes' && $product->get_rating_count() > 0 ) {
			$output .= '<div class="wcps-product-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 .= wc_get_rating_html($product->get_average_rating(), $product->get_rating_count());
			$output .= '<span class="rating-count">(' . $product->get_rating_count() . ')</span>';
			$output .= '</div>';
		}

		// Product description.
		if ( isset($this->settings['show_description']) && $this->settings['show_description'] === 'yes' ) {
			$short_description = $product->get_short_description();
			if ( ! empty($short_description) ) {
				$output .= '<div class="wcps-product-description">' . wp_trim_words($short_description, 15, '...') . '</div>';
			}
		}

		$output .= '</div>'; // End .wcps-product-content.

		// Show action buttons if position is below.
		if ( 'below' === $this->settings['action_position'] ) {
			$output .= $this->render_product_actions($product);
		}

		$output .= '</div>'; // End .wcps-product.
		$output .= '</div>'; // End .wcps-card-item.

		return $output;
	}
}
