<?php
/**
 * Single compact product card template.
 *
 * Renders the §7.1 "single-compact-card" key against the §8.2
 * canonical ProductCardViewModel array shape produced by
 * {@see \CommerceLinkBuilder\Application\Render\ViewModelArrayDumper}.
 *
 * Compact variation of {@see ./single-default-card.php} optimized
 * for in-grid use (driven by `ProductGridBlockRenderer::DEFAULT_ITEM_TEMPLATE`)
 * and inline contexts where vertical real estate is at a premium.
 *
 * What the compact template renders:
 *
 *   - Optional primary image (decorative link, same accessibility
 *     posture as the default template per §20).
 *   - Title (canonical focusable detail-page link).
 *   - Optional brand line (driven by `meta.show_brand`).
 *   - Optional price line (driven by `price.visible`); the
 *     `observed_at_label` and `disclaimer_required` axes are
 *     omitted from the compact surface intentionally to keep the
 *     in-grid cell tall enough for a single product to remain
 *     visually scannable.
 *   - Optional availability label (driven by
 *     `availability.visible`).
 *   - At most ONE primary CTA button (the first button the
 *     ViewModel produced after Phase-5 button-policy filtering);
 *     the spec §13.3 graceful-degradation rule still applies
 *     when no buttons survive that filter.
 *
 * What the compact template intentionally OMITS vs. the default
 * template:
 *
 *   - Description block.
 *   - Provider badge.
 *   - Disclaimer footer (the disclaimers themselves still render
 *     once at the §11.2 grid level when the grid template is in
 *     use; carrying per-cell disclaimer text in a compact
 *     in-grid surface would dwarf the cell content).
 *   - Badges block.
 *   - Multi-button CTA strip.
 *
 * The template is invoked by
 * {@see \CommerceLinkBuilder\Application\Render\ProductCardRenderer}
 * via its include strategy with a single $viewModel local — the
 * read-only associative-array copy of the §8.2 view model with a
 * synthetic `_render_meta` block attached. See
 * {@see ./single-default-card.php} for the exhaustive shape
 * documentation; the compact template only reads the subset
 * documented above.
 *
 * Output rules (per `specifications/rendering-and-template-spec.md`):
 *
 *   - §10 / §15: BEM class strings come pre-tokenized in
 *     `$viewModel['classes']`. The template stamps them verbatim
 *     and never synthesizes its own modifier strings.
 *   - §13.3: Optional sections degrade gracefully — a null /
 *     empty primary image, hidden price, hidden availability,
 *     missing brand, or empty buttons list simply omits the
 *     corresponding region.
 *   - §20: The image link is decorative (`tabindex="-1"`,
 *     `aria-hidden="true"`); the title link is the canonical
 *     focusable detail-page link.
 *   - All dynamic output is escaped with `esc_html` /
 *     `esc_attr` / `esc_url`. The $viewModel array is treated
 *     as read-only by convention.
 *
 * 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. This
 * still hard-stops a direct browser hit on the template file
 * (where $viewModel is undefined) without breaking the renderer-
 * driven include path used by the test suite.
 *
 * @package CommerceLinkBuilder
 *
 * @var array<string, mixed> $viewModel §8.2 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_product      = isset( $viewModel['product'] ) && is_array( $viewModel['product'] ) ? $viewModel['product'] : array();
$clb_images       = isset( $viewModel['images'] ) && is_array( $viewModel['images'] ) ? $viewModel['images'] : array();
$clb_price        = isset( $viewModel['price'] ) && is_array( $viewModel['price'] ) ? $viewModel['price'] : array();
$clb_availability = isset( $viewModel['availability'] ) && is_array( $viewModel['availability'] ) ? $viewModel['availability'] : array();
$clb_buttons      = isset( $viewModel['buttons'] ) && is_array( $viewModel['buttons'] ) ? $viewModel['buttons'] : 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-card';
$clb_image_class   = isset( $clb_classes['image'] ) ? (string) $clb_classes['image'] : 'clb-card__image';
$clb_body_class    = isset( $clb_classes['body'] ) ? (string) $clb_classes['body'] : 'clb-card__body';
$clb_title_class   = isset( $clb_classes['title'] ) ? (string) $clb_classes['title'] : 'clb-card__title';
$clb_price_class   = isset( $clb_classes['price'] ) ? (string) $clb_classes['price'] : 'clb-card__price';
$clb_buttons_class = isset( $clb_classes['buttons'] ) ? (string) $clb_classes['buttons'] : 'clb-card__buttons';

$clb_locale        = isset( $clb_render_meta['locale'] ) && is_string( $clb_render_meta['locale'] ) && $clb_render_meta['locale'] !== '' ? $clb_render_meta['locale'] : null;
$clb_primary_image = isset( $clb_images['primary'] ) && is_array( $clb_images['primary'] ) ? $clb_images['primary'] : null;
$clb_detail_url    = isset( $clb_product['detail_url'] ) ? (string) $clb_product['detail_url'] : '';
$clb_title         = isset( $clb_product['title'] ) ? (string) $clb_product['title'] : '';
$clb_brand         = isset( $clb_product['brand'] ) && is_string( $clb_product['brand'] ) && $clb_product['brand'] !== '' ? $clb_product['brand'] : null;

$clb_show_brand = ! empty( $clb_meta['show_brand'] );

$clb_price_visible        = ! 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;
$clb_availability_visible = ! empty( $clb_availability['visible'] );
$clb_availability_label   = isset( $clb_availability['label'] ) && is_string( $clb_availability['label'] ) && $clb_availability['label'] !== '' ? $clb_availability['label'] : null;
$clb_availability_status  = isset( $clb_availability['status'] ) ? (string) $clb_availability['status'] : 'unknown';

// Compact variation: surface only the FIRST button the ViewModel
// produced after Phase-5 button-policy filtering. Multi-button
// CTAs would dwarf the in-grid cell.
$clb_primary_button = null;
foreach ( $clb_buttons as $clb_candidate_button ) {
	if ( is_array( $clb_candidate_button ) ) {
		$clb_primary_button = $clb_candidate_button;
		break;
	}
}
?>
<article class="<?php echo esc_attr( $clb_root_class ); ?>" data-clb-card="compact"<?php if ( $clb_locale !== null ) : ?> lang="<?php echo esc_attr( $clb_locale ); ?>"<?php endif; ?>>
	<?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="<?php echo esc_attr( $clb_image_class ); ?>">
			<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="<?php echo esc_attr( $clb_body_class ); ?>">
		<h3 class="<?php echo esc_attr( $clb_title_class ); ?>">
			<a href="<?php echo esc_url( $clb_detail_url ); ?>"><?php echo esc_html( $clb_title ); ?></a>
		</h3>
		<?php if ( $clb_show_brand && $clb_brand !== null ) : ?>
			<p class="clb-card__brand"><?php echo esc_html( $clb_brand ); ?></p>
		<?php endif; ?>
		<?php if ( $clb_price_visible ) : ?>
			<p class="<?php echo esc_attr( $clb_price_class ); ?>">
				<span class="clb-card__price-amount"><?php echo esc_html( $clb_price_label ); ?></span>
				<?php if ( $clb_price_observed_at !== null ) : ?>
					<span class="clb-card__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 ( $clb_availability_visible && $clb_availability_label !== null ) : ?>
			<p class="clb-card__availability clb-card__availability--<?php echo esc_attr( $clb_availability_status ); ?>"><?php echo esc_html( $clb_availability_label ); ?></p>
		<?php endif; ?>
		<?php if ( $clb_primary_button !== null ) : ?>
			<div class="<?php echo esc_attr( $clb_buttons_class ); ?>">
				<?php
				$clb_btn_class      = isset( $clb_primary_button['class_name'] ) ? (string) $clb_primary_button['class_name'] : 'clb-button';
				$clb_btn_url        = isset( $clb_primary_button['url'] ) ? (string) $clb_primary_button['url'] : '';
				$clb_btn_target     = isset( $clb_primary_button['target'] ) && is_string( $clb_primary_button['target'] ) && $clb_primary_button['target'] !== '' ? $clb_primary_button['target'] : null;
				$clb_btn_rel        = isset( $clb_primary_button['rel'] ) ? (string) $clb_primary_button['rel'] : '';
				$clb_btn_aria_label = isset( $clb_primary_button['aria_label'] ) && is_string( $clb_primary_button['aria_label'] ) && $clb_primary_button['aria_label'] !== '' ? $clb_primary_button['aria_label'] : null;
				$clb_btn_label      = isset( $clb_primary_button['label'] ) ? (string) $clb_primary_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>
			</div>
		<?php endif; ?>
	</div>
</article>
