<?php
/**
 * Default product grid template.
 *
 * Renders the §7.2 "grid-default" key against the §8.3 canonical
 * ProductGridViewModel array shape produced by
 * {@see \CommerceLinkBuilder\Application\Render\ViewModelArrayDumper}.
 *
 * The template is invoked by
 * {@see \CommerceLinkBuilder\Application\Render\ProductGridRenderer}
 * via its include strategy with a single $viewModel local — the
 * read-only associative-array copy of the §8.3 view model with a
 * synthetic `_render_meta` block attached at the top level. The
 * shape is:
 *
 *   array{
 *     type: 'product-grid',
 *     template: string,
 *     layout: array{columns: int, gap: string},
 *     items: list<array<string, mixed>>,
 *     meta: array{
 *       count: int,
 *       show_brand: bool,
 *       show_description: bool,
 *       show_price: bool,
 *       show_provider_badge: bool,
 *     },
 *     classes: array{root: string, item: string},
 *     is_fallback: bool,
 *     _render_meta: array{
 *       surface: string,
 *       locale: string|null,
 *       request_id: string|null,
 *       is_amp: bool,
 *       is_feed: bool,
 *       tracking_enabled: bool,
 *       lazy_load_enabled: bool,
 *       debug: bool,
 *     },
 *   }
 *
 * Each `items[i]` is the dumped §8.2 ProductCardViewModel array
 * (`type === 'product-card'`) or the §8.4 FallbackCardViewModel
 * array (`type === 'fallback-card'`). The grid-level
 * `_render_meta` block is NOT propagated to the inner item arrays:
 * the grid template renders compact per-cell markup inline so
 * inner cards do not invoke the single-card include strategy
 * recursively.
 *
 * Output rules (per `specifications/rendering-and-template-spec.md`):
 *
 *   - §10 / §15: BEM class strings come pre-tokenized in
 *     `$viewModel['classes']`. The template stamps `classes.root`
 *     on the outer `<ul>` element and `classes.item` on each
 *     `<li>` wrapper verbatim, never synthesizing its own
 *     modifier strings.
 *   - §7.2 / §13.3: The grid-default MVP template renders an
 *     intentionally compact per-cell payload — title, primary
 *     image, optional provider badge, optional price, and any
 *     primary action buttons. Full single-card behavior
 *     (availability, disclaimers, gallery) is reserved for the
 *     full single-card templates the per-cell variants will
 *     evolve into.
 *   - §13.3: A fallback item (`type === 'fallback-card'`)
 *     renders only the title, the primary button (if present),
 *     and — under debug — the admin-facing diagnostic note.
 *   - All dynamic output is escaped with `esc_html`,
 *     `esc_attr`, or `esc_url`. No `wp_kses` variant is used:
 *     every text node is plain string content and every
 *     attribute is a single discrete token.
 *
 * Soft execution guard: instead of the conventional
 * `! defined( 'ABSPATH' ) && exit;` (which would prevent unit
 * tests from including the template through the renderer's
 * include strategy), the template returns silently when its
 * single contract input is missing or has the wrong type.
 *
 * @package CommerceLinkBuilder
 *
 * @var array<string, mixed> $viewModel §8.3 array shape with `_render_meta` attached.
 */

if ( ! isset( $viewModel ) || ! is_array( $viewModel ) ) {
	return;
}

$clb_classes     = isset( $viewModel['classes'] ) && is_array( $viewModel['classes'] ) ? $viewModel['classes'] : array();
$clb_items       = isset( $viewModel['items'] ) && is_array( $viewModel['items'] ) ? $viewModel['items'] : array();
$clb_meta        = isset( $viewModel['meta'] ) && is_array( $viewModel['meta'] ) ? $viewModel['meta'] : array();
$clb_render_meta = isset( $viewModel['_render_meta'] ) && is_array( $viewModel['_render_meta'] ) ? $viewModel['_render_meta'] : array();

$clb_root_class = isset( $clb_classes['root'] ) ? (string) $clb_classes['root'] : 'clb-grid';
$clb_item_class = isset( $clb_classes['item'] ) ? (string) $clb_classes['item'] : 'clb-grid-item';

$clb_show_provider_badge = ! empty( $clb_meta['show_provider_badge'] );
$clb_show_price          = ! empty( $clb_meta['show_price'] );

$clb_locale = isset( $clb_render_meta['locale'] ) && is_string( $clb_render_meta['locale'] ) && $clb_render_meta['locale'] !== '' ? $clb_render_meta['locale'] : null;
$clb_debug  = ! empty( $clb_render_meta['debug'] );
?>
<ul class="<?php echo esc_attr( $clb_root_class ); ?>" data-clb-grid="default"<?php if ( $clb_locale !== null ) : ?> lang="<?php echo esc_attr( $clb_locale ); ?>"<?php endif; ?>>
	<?php foreach ( $clb_items as $clb_item ) : ?>
		<?php
		if ( ! is_array( $clb_item ) ) {
			continue;
		}
		$clb_item_type = isset( $clb_item['type'] ) ? (string) $clb_item['type'] : '';
		?>
		<li class="<?php echo esc_attr( $clb_item_class ); ?>" data-clb-item-type="<?php echo esc_attr( $clb_item_type ); ?>">
			<?php if ( $clb_item_type === 'product-card' ) : ?>
				<?php
				$clb_product = isset( $clb_item['product'] ) && is_array( $clb_item['product'] ) ? $clb_item['product'] : array();
				$clb_images  = isset( $clb_item['images'] ) && is_array( $clb_item['images'] ) ? $clb_item['images'] : array();
				$clb_price   = isset( $clb_item['price'] ) && is_array( $clb_item['price'] ) ? $clb_item['price'] : array();
				$clb_buttons = isset( $clb_item['buttons'] ) && is_array( $clb_item['buttons'] ) ? $clb_item['buttons'] : array();
				$clb_item_meta        = isset( $clb_item['meta'] ) && is_array( $clb_item['meta'] ) ? $clb_item['meta'] : array();
				$clb_item_disclaimers = isset( $clb_item['disclaimers'] ) && is_array( $clb_item['disclaimers'] ) ? $clb_item['disclaimers'] : array();

				$clb_detail_url   = isset( $clb_product['detail_url'] ) ? (string) $clb_product['detail_url'] : '';
				$clb_title        = isset( $clb_product['title'] ) ? (string) $clb_product['title'] : '';
				$clb_provider     = isset( $clb_product['provider'] ) ? (string) $clb_product['provider'] : '';
				$clb_provider_lbl = isset( $clb_product['provider_label'] ) && is_string( $clb_product['provider_label'] ) && $clb_product['provider_label'] !== '' ? $clb_product['provider_label'] : null;
				$clb_brand        = isset( $clb_product['brand'] ) && is_string( $clb_product['brand'] ) && $clb_product['brand'] !== '' ? $clb_product['brand'] : null;

				$clb_show_brand_item = ! empty( $clb_item_meta['show_brand'] );

				$clb_primary_image = isset( $clb_images['primary'] ) && is_array( $clb_images['primary'] ) ? $clb_images['primary'] : null;

				$clb_price_visible = $clb_show_price && ! empty( $clb_price['visible'] );
				$clb_price_label   = isset( $clb_price['label'] ) && is_string( $clb_price['label'] ) ? $clb_price['label'] : '';
				$clb_price_observed_at = isset( $clb_price['observed_at_label'] ) && is_string( $clb_price['observed_at_label'] ) && $clb_price['observed_at_label'] !== '' ? $clb_price['observed_at_label'] : null;
				?>
				<article class="clb-grid-item__card">
					<?php if ( $clb_primary_image !== null ) : ?>
						<?php
						$clb_img_url     = isset( $clb_primary_image['url'] ) ? (string) $clb_primary_image['url'] : '';
						$clb_img_alt     = isset( $clb_primary_image['alt'] ) ? (string) $clb_primary_image['alt'] : '';
						$clb_img_loading = isset( $clb_primary_image['loading'] ) ? (string) $clb_primary_image['loading'] : 'lazy';
						$clb_img_width   = isset( $clb_primary_image['width'] ) && is_int( $clb_primary_image['width'] ) ? $clb_primary_image['width'] : null;
						$clb_img_height  = isset( $clb_primary_image['height'] ) && is_int( $clb_primary_image['height'] ) ? $clb_primary_image['height'] : null;
						$clb_img_dims    = ( $clb_img_width !== null && $clb_img_height !== null );
						?>
						<figure class="clb-grid-item__image">
							<a href="<?php echo esc_url( $clb_detail_url ); ?>" tabindex="-1" aria-hidden="true">
								<img
									src="<?php echo esc_url( $clb_img_url ); ?>"
									alt="<?php echo esc_attr( $clb_img_alt ); ?>"
									loading="<?php echo esc_attr( $clb_img_loading ); ?>"
									<?php if ( $clb_img_dims ) : ?>
									width="<?php echo esc_attr( (string) $clb_img_width ); ?>"
									height="<?php echo esc_attr( (string) $clb_img_height ); ?>"
									<?php endif; ?>
								/>
							</a>
						</figure>
					<?php endif; ?>
					<div class="clb-grid-item__body">
						<?php if ( $clb_show_provider_badge && $clb_provider_lbl !== null ) : ?>
							<span class="clb-grid-item__provider clb-provider-<?php echo esc_attr( $clb_provider ); ?>"><?php echo esc_html( $clb_provider_lbl ); ?></span>
						<?php endif; ?>
						<h3 class="clb-grid-item__title">
							<a href="<?php echo esc_url( $clb_detail_url ); ?>"><?php echo esc_html( $clb_title ); ?></a>
						</h3>
						<?php if ( $clb_show_brand_item && $clb_brand !== null ) : ?>
							<p class="clb-grid-item__brand"><?php echo esc_html( $clb_brand ); ?></p>
						<?php endif; ?>
						<?php if ( $clb_price_visible ) : ?>
							<p class="clb-grid-item__price">
								<span class="clb-grid-item__price-amount"><?php echo esc_html( $clb_price_label ); ?></span>
								<?php if ( $clb_price_observed_at !== null ) : ?>
									<span class="clb-grid-item__price-observed-at"><time datetime="<?php echo esc_attr( $clb_price_observed_at ); ?>"><?php echo esc_html( $clb_price_observed_at ); ?></time></span>
								<?php endif; ?>
							</p>
						<?php endif; ?>
						<?php if ( ! empty( $clb_buttons ) ) : ?>
							<div class="clb-grid-item__buttons">
								<?php foreach ( $clb_buttons as $clb_button ) : ?>
									<?php
									if ( ! is_array( $clb_button ) ) {
										continue;
									}
									$clb_btn_class      = isset( $clb_button['class_name'] ) ? (string) $clb_button['class_name'] : 'clb-button';
									$clb_btn_url        = isset( $clb_button['url'] ) ? (string) $clb_button['url'] : '';
									$clb_btn_target     = isset( $clb_button['target'] ) && is_string( $clb_button['target'] ) && $clb_button['target'] !== '' ? $clb_button['target'] : null;
									$clb_btn_rel        = isset( $clb_button['rel'] ) ? (string) $clb_button['rel'] : '';
									$clb_btn_aria_label = isset( $clb_button['aria_label'] ) && is_string( $clb_button['aria_label'] ) && $clb_button['aria_label'] !== '' ? $clb_button['aria_label'] : null;
									$clb_btn_label      = isset( $clb_button['label'] ) ? (string) $clb_button['label'] : '';
									?>
									<a class="<?php echo esc_attr( $clb_btn_class ); ?>"
										href="<?php echo esc_url( $clb_btn_url ); ?>"
										<?php if ( $clb_btn_target !== null ) : ?>target="<?php echo esc_attr( $clb_btn_target ); ?>"<?php endif; ?>
										rel="<?php echo esc_attr( $clb_btn_rel ); ?>"
										<?php if ( $clb_btn_aria_label !== null ) : ?>aria-label="<?php echo esc_attr( $clb_btn_aria_label ); ?>"<?php endif; ?>>
										<span class="clb-button__label"><?php echo esc_html( $clb_btn_label ); ?></span>
									</a>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>
						<?php if ( ! empty( $clb_item_disclaimers ) ) : ?>
							<footer class="clb-grid-item__disclaimers">
								<ul class="clb-grid-item__disclaimers-list">
									<?php foreach ( $clb_item_disclaimers as $clb_item_disclaimer ) : ?>
										<?php $clb_item_disclaimer_text = (string) $clb_item_disclaimer; ?>
										<li class="clb-grid-item__disclaimer">
											<?php if ( str_contains( $clb_item_disclaimer_text, 'clb.rio.st' ) ) : ?>
												<?php echo esc_html( str_replace( 'clb.rio.st', '', $clb_item_disclaimer_text ) ); ?>
												<a href="<?php echo esc_url( 'https://clb.rio.st' ); ?>">clb.rio.st</a>
											<?php else : ?>
												<?php echo esc_html( $clb_item_disclaimer_text ); ?>
											<?php endif; ?>
										</li>
									<?php endforeach; ?>
								</ul>
							</footer>
						<?php endif; ?>
					</div>
				</article>
			<?php elseif ( $clb_item_type === 'fallback-card' ) : ?>
				<?php
				$clb_fb_reason = isset( $clb_item['reason'] ) ? (string) $clb_item['reason'] : '';
				$clb_fb_title  = isset( $clb_item['title'] ) && is_string( $clb_item['title'] ) && $clb_item['title'] !== '' ? $clb_item['title'] : null;
				$clb_fb_button = isset( $clb_item['primary_button'] ) && is_array( $clb_item['primary_button'] ) ? $clb_item['primary_button'] : null;
				$clb_fb_note   = isset( $clb_item['note'] ) && is_string( $clb_item['note'] ) && $clb_item['note'] !== '' ? $clb_item['note'] : null;
				?>
				<article class="clb-grid-item__card clb-grid-item__card--fallback" data-clb-reason="<?php echo esc_attr( $clb_fb_reason ); ?>">
					<?php if ( $clb_fb_title !== null ) : ?>
						<h3 class="clb-grid-item__title"><?php echo esc_html( $clb_fb_title ); ?></h3>
					<?php endif; ?>
					<?php if ( $clb_fb_button !== null ) : ?>
						<?php
						$clb_fb_btn_class      = isset( $clb_fb_button['class_name'] ) ? (string) $clb_fb_button['class_name'] : 'clb-button';
						$clb_fb_btn_url        = isset( $clb_fb_button['url'] ) ? (string) $clb_fb_button['url'] : '';
						$clb_fb_btn_target     = isset( $clb_fb_button['target'] ) && is_string( $clb_fb_button['target'] ) && $clb_fb_button['target'] !== '' ? $clb_fb_button['target'] : null;
						$clb_fb_btn_rel        = isset( $clb_fb_button['rel'] ) ? (string) $clb_fb_button['rel'] : '';
						$clb_fb_btn_aria_label = isset( $clb_fb_button['aria_label'] ) && is_string( $clb_fb_button['aria_label'] ) && $clb_fb_button['aria_label'] !== '' ? $clb_fb_button['aria_label'] : null;
						$clb_fb_btn_label      = isset( $clb_fb_button['label'] ) ? (string) $clb_fb_button['label'] : '';
						?>
						<div class="clb-grid-item__buttons">
							<a class="<?php echo esc_attr( $clb_fb_btn_class ); ?>"
								href="<?php echo esc_url( $clb_fb_btn_url ); ?>"
								<?php if ( $clb_fb_btn_target !== null ) : ?>target="<?php echo esc_attr( $clb_fb_btn_target ); ?>"<?php endif; ?>
								rel="<?php echo esc_attr( $clb_fb_btn_rel ); ?>"
								<?php if ( $clb_fb_btn_aria_label !== null ) : ?>aria-label="<?php echo esc_attr( $clb_fb_btn_aria_label ); ?>"<?php endif; ?>>
								<span class="clb-button__label"><?php echo esc_html( $clb_fb_btn_label ); ?></span>
							</a>
						</div>
					<?php endif; ?>
					<?php if ( $clb_debug && $clb_fb_note !== null ) : ?>
						<p class="clb-grid-item__fallback-note"><?php echo esc_html( $clb_fb_note ); ?></p>
					<?php endif; ?>
				</article>
			<?php endif; ?>
		</li>
	<?php endforeach; ?>
</ul>
