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

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

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

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

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

		$pro_result = $this->handle_pro_layout_mode( 'basic-grid-4', 'PRODDISP_Pro_Grid', 'grid-locked-style-4.gif', $products );
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		
		$pro_result = $this->handle_pro_layout_mode( 'basic-grid-5', 'PRODDISP_Pro_Grid', 'grid-locked-style-5.gif', $products );
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		
		$pro_result = $this->handle_pro_layout_mode( 'basic-grid-6', 'PRODDISP_Pro_Grid', 'grid-locked-style-6.gif', $products );
		if ( $pro_result !== false ) {
			return $pro_result;
		}


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

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

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

		// Original grid rendering for basic-grid (default)
		$columns = absint($this->settings['columns'] ?? 4);
		if ( $columns < 1 ) {
			$columns = 4;
		}

		$output = '<div class="wcps-grid-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 grid.
	 *
	 * @param Product_Display $product The product object.
	 * @return string HTML for the product.
	 */
	private function render_product( $product ) {
		$output = '<div class="wcps-grid-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">';

		if ( $product->is_on_sale() ) {
			$output .= '<span class="wcps-product-badge" aria-label="' . esc_attr__('On Sale', 'product-display') . '">' . esc_html__('Sale', 'product-display') . '</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'] ?? 'woocommerce_thumbnail', false, array(
				'class' => 'wcps-image',
				'loading' => 'lazy',
				'alt' => esc_attr($alt_text),
			));
		} else {
			$output .= wc_placeholder_img($this->settings['image_size'] ?? 'woocommerce_thumbnail', 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'] ?? 'below' ) ) {
			$output .= $this->render_product_actions($product);
		}

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

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

		// Product content.
		$output .= $this->render_product_content($product);

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

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

		return $output;
	}
}
