<?php
/**
 * Recommendations grid template.
 * Variables expected:
 * - array $products (WC_Product[])
 * - array $design_data
 * - \PRECO\Includes\Frontend\Core\Layouts\AbstractLayout $layout
 * - string $label
 * - string $wrapper_style
 * - string $cls
 * - string $cols_style
 *
 * @package PRECO
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
?>
<?php

$cols   = isset( $design_data['columns'] ) && is_array( $design_data['columns'] ) ? $design_data['columns'] : array(
	'lg' => 4,
	'md' => 2,
	'sm' => 1,
);
$col_lg = isset( $cols['lg'] ) ? intval( $cols['lg'] ) : 4;
$col_md = isset( $cols['md'] ) ? intval( $cols['md'] ) : 2;
$col_sm = isset( $cols['sm'] ) ? intval( $cols['sm'] ) : 1;
// We'll output attributes inline to avoid echoing pre-built attribute strings.

// Template layout object (falls back to Layout 1 when the variable is missing).
if ( ! isset( $layout ) || ! is_a( $layout, \PRECO\Includes\Frontend\Core\Layouts\AbstractLayout::class ) ) {
	$layout = \PRECO\Includes\Frontend\Core\Layouts\LayoutFactory::create( isset( $style_data['template_layout'] ) ? $style_data['template_layout'] : '' );
}

// Global card style (may be 'slider').
$layout_style_global = isset( $style_data['layout_style'] ) ? sanitize_html_class( $style_data['layout_style'] ) : 'grid';

// Prepare class lists to avoid inline PHP fragments that may introduce whitespace/newlines.
$outer_class  = is_string( $cls ) ? trim( $cls ) : '';
$grid_classes = array( 'preco-recommendations__grid' );
if ( 'slider' === $layout_style_global ) {
	$grid_classes[] = 'preco-recommendations__grid--slider';
}
$grid_class_attr = implode( ' ', $grid_classes );

$root_attr_string = '';
if ( ! empty( $data_attributes ) && is_array( $data_attributes ) ) {
	foreach ( $data_attributes as $key => $value ) {
		// Only allow data-* attribute names to prevent event-handler injection.
		if ( ! is_string( $key ) || 0 !== strpos( $key, 'data-' ) ) {
			continue;
		}
		if ( $value !== '' ) {
			$root_attr_string .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
		}
	}
}

?>

<div class="<?php echo esc_attr( $outer_class ); ?>"<?php echo $root_attr_string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each key is validated as data-* and each key/value is individually esc_attr()-processed above. ?>
						<?php
						// Build inline style: CSS custom properties + display:none for sequential items.
						$inline_style_parts = array();
						$custom_css         = \PRECO\Includes\Preco::adding_specific_properties( $design_data );
						if ( ! empty( $custom_css ) ) {
							$inline_style_parts[] = $custom_css;
						}
						if ( ! empty( $seq_hidden ) ) {
							$inline_style_parts[] = 'display:none';
						}
						if ( ! empty( $inline_style_parts ) ) :
							?>
	style="<?php echo esc_attr( implode( ';', $inline_style_parts ) ); ?>"<?php endif; ?>>
	<?php
	if ( ! empty( $label ) ) :
		$label_alignment = isset( $design_data['label_alignment'] ) ? sanitize_html_class( $design_data['label_alignment'] ) : 'left';
		$label_class     = 'preco-recommendations__label preco-recommendations__label--' . $label_alignment;
		?>
		<div class="<?php echo esc_attr( $label_class ); ?>"><?php echo esc_html( $label ); ?></div>
	<?php endif; ?>

	<div class="<?php echo esc_attr( $grid_class_attr ); ?>" data-card-style="<?php echo esc_attr( $layout_style_global ); ?>" >
		<?php
		$item_sorting = isset( $design_data['item_sorting'] ) && is_array( $design_data['item_sorting'] ) ? $design_data['item_sorting'] : array();
		$card_args    = array();

		foreach ( $products as $p ) :
			if ( ! $p || ! is_a( $p, 'WC_Product' ) ) {
				continue;
			}
			$layout->render_card( $p, $item_sorting, $card_args );
			endforeach;
		?>
	</div>
</div>
<?php // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
