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

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

/**
 * Class PRODDISP_List.
 *
 * This class handles the rendering of the Product List layout for product display.
 */
class PRODDISP_List extends PRODDISP_Layout_Base {

	/**
	 * Render the layout for all products.
	 */
	public function render( $products ) {
		// Enqueue list-specific assets
		PRODDISP_Assets::get_instance()->enqueue_list_style_assets();

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

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

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

	/**
	 * Render a single product in the Product List layout.
	 */
	public function render_product( $product ) {
		if ( ! $product ) {
			return '';
		}
		$product_id = $product->get_id();
		$title = $product->get_name();
		$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_thumbnail', array(
			'class' => 'wcps-list-image',
			'alt' => esc_attr($alt_text),
			'loading' => 'lazy',
		) );

		$desc = $product->get_short_description();
		$price = $product->get_price_html();
		$quality = $product->get_attribute( 'quality' ); // Example attribute, adjust as needed
		$actions = $this->render_product_actions( $product );
		// Add quantity input for list layout
		$quantity_input = '<input type="number" class="wcps-qty-input" name="wcps_qty_' . esc_attr($product_id) . '" value="1" min="1" style="width:60px;">';
		// Add to cart button, with data-quantity attribute for JS
		$add_to_cart = '<a href="' . esc_url($product->add_to_cart_url()) . '" data-quantity="1" data-product-id="' . esc_attr($product_id) . '" class="wcps-add-to-cart-button button add_to_cart_button ajax_add_to_cart" rel="nofollow">' . esc_html($product->add_to_cart_text()) . '</a>';

		$output = '<div class="wcps-list-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-list-inner">';
		// Left: Image with actions
		$output .= '<div class="wcps-list-left">';
		$output .= '<a href="' . esc_url( $permalink ) . '" class="wcps-list-image-link">' . $image . '</a>';
		$output .= '<div class="wcps-list-actions-on-image">' . $actions . '</div>';
		$output .= '</div>';
		// Middle: Title, desc, price
		$output .= '<div class="wcps-list-middle">';
		$output .= '<h2 class="wcps-list-title" itemprop="name"><a href="' . esc_url( $permalink ) . '">' . esc_html( $title ) . '</a></h2>';
		$output .= '<div class="wcps-list-desc-price">';
		$output .= '<div class="wcps-list-desc" itemprop="description">' . wp_kses_post( $desc ) . '</div>';
		$output .= '<div class="wcps-list-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;
		$output .= '</div>';
		$output .= '</div>';
		$output .= '</div>';
		// Right: Quality, quantity, add to cart
		$output .= '<div class="wcps-list-right">';
		$output .= '<div class="wcps-list-quality">';
		if ( $product->is_in_stock() ) {
			$output .= esc_html__('In Stock', 'product-display');
		} else {
			$output .= esc_html__('Out of Stock', 'product-display');
		}
		if ( $quality ) {
			$output .= ' | ' . esc_html( $quality );
		}
		$output .= '</div>';
		$output .= '<div class="wcps-list-qty-add">' . $quantity_input . $add_to_cart . '</div>';
		$output .= '</div>';
		$output .= '</div>';
		$output .= '</div>';
		return $output;
	}
}
