<?php
/**
 * Single default product card template.
 *
 * Renders the §7.1 "single-default-card" key against the §8.2
 * canonical ProductCardViewModel array shape produced by
 * {@see \CommerceLinkBuilder\Application\Render\ViewModelArrayDumper}.
 *
 * 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. The shape is:
 *
 *   array{
 *     type: 'product-card',
 *     template: string,
 *     product: array{
 *       id: int|null,
 *       provider: string,
 *       provider_label: string|null,
 *       provider_item_id: string|null,
 *       title: string,
 *       brand: string|null,
 *       description: string|null,
 *       detail_url: string,
 *       affiliate_url: string|null,
 *     },
 *     images: array{
 *       primary: array{url: string, alt: string, width: int|null, height: int|null, loading: string}|null,
 *       gallery: list<array{url: string, alt: string, width: int|null, height: int|null, loading: string}>,
 *     },
 *     price: array{
 *       visible: bool,
 *       label: string|null,
 *       amount: string|null,
 *       currency: string|null,
 *       observed_at_label: string|null,
 *       disclaimer_required: bool,
 *     },
 *     availability: array{
 *       visible: bool,
 *       status: string,
 *       label: string|null,
 *     },
 *     buttons: list<array{
 *       provider: string,
 *       label: string,
 *       url: string,
 *       target: string|null,
 *       rel: string,
 *       class_name: string,
 *       aria_label: string|null,
 *     }>,
 *     badges: list<array{kind: string, label: string, class_name: string|null}>,
 *     disclaimers: list<string>,
 *     meta: array{
 *       show_brand: bool,
 *       show_description: bool,
 *       show_price: bool,
 *       show_provider_badge: bool,
 *     },
 *     classes: array{root: string, image: string, body: string, title: string, price: string, buttons: 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,
 *     },
 *   }
 *
 * 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, empty buttons list, hidden price,
 *     hidden availability, empty disclaimers, or empty badges
 *     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_badges       = ( isset( $viewModel['badges'] ) && is_array( $viewModel['badges'] ) )
	? $viewModel['badges']
	: array();
$clb_disclaimers  = ( isset( $viewModel['disclaimers'] ) && is_array( $viewModel['disclaimers'] ) )
	? $viewModel['disclaimers']
	: 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_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_description   = ( isset( $clb_product['description'] )
	&& is_string( $clb_product['description'] )
	&& $clb_product['description'] !== '' )
	? $clb_product['description']
	: null;

$clb_show_provider_badge = ! empty( $clb_meta['show_provider_badge'] );
$clb_show_brand          = ! empty( $clb_meta['show_brand'] );
$clb_show_description    = ! empty( $clb_meta['show_description'] );

$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';

$clb_lang_attr = ( $clb_locale !== null )
	? ' lang="' . esc_attr( $clb_locale ) . '"'
	: '';
?>
<article
	class="<?php echo esc_attr( $clb_root_class ); ?>"
	data-clb-card="default"
	<?php echo $clb_lang_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above. ?>
>
	<?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 ); ?>">
		<header class="clb-card__header">
			<?php if ( $clb_show_provider_badge && $clb_provider_lbl !== null ) : ?>
				<?php $clb_provider_class = 'clb-card__provider clb-provider-' . $clb_provider; ?>
				<span class="<?php echo esc_attr( $clb_provider_class ); ?>"><?php echo esc_html( $clb_provider_lbl ); ?></span>
			<?php endif; ?>
			<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; ?>
		</header>
		<?php if ( $clb_show_description && $clb_description !== null ) : ?>
			<p class="clb-card__description"><?php echo esc_html( $clb_description ); ?></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 ) : ?>
			<?php
			$clb_availability_class = 'clb-card__availability clb-card__availability--'
				. $clb_availability_status;
			?>
			<p class="<?php echo esc_attr( $clb_availability_class ); ?>">
				<?php echo esc_html( $clb_availability_label ); ?>
			</p>
		<?php endif; ?>
		<?php if ( ! empty( $clb_buttons ) ) : ?>
			<div class="<?php echo esc_attr( $clb_buttons_class ); ?>">
				<?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'] : '';
					$clb_btn_target_attr = ( $clb_btn_target !== null )
						? ' target="' . esc_attr( $clb_btn_target ) . '"'
						: '';
					$clb_btn_aria_attr   = ( $clb_btn_aria_label !== null )
						? ' aria-label="' . esc_attr( $clb_btn_aria_label ) . '"'
						: '';
					?>
					<a class="<?php echo esc_attr( $clb_btn_class ); ?>"
						href="<?php echo esc_url( $clb_btn_url ); ?>"
						<?php
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above.
						echo $clb_btn_target_attr;
						?>
						rel="<?php echo esc_attr( $clb_btn_rel ); ?>"
						<?php
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above.
						echo $clb_btn_aria_attr;
						?>
						>
						<span class="clb-button__label"><?php echo esc_html( $clb_btn_label ); ?></span>
					</a>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
		<?php if ( ! empty( $clb_disclaimers ) ) : ?>
			<footer class="clb-card__disclaimers">
				<ul class="clb-card__disclaimers-list">
					<?php foreach ( $clb_disclaimers as $clb_disclaimer ) : ?>
						<?php $clb_disclaimer_text = (string) $clb_disclaimer; ?>
						<li class="clb-card__disclaimer">
							<?php if ( str_contains( $clb_disclaimer_text, 'clb.rio.st' ) ) : ?>
								<?php echo esc_html( str_replace( 'clb.rio.st', '', $clb_disclaimer_text ) ); ?>
								<a href="<?php echo esc_url( 'https://clb.rio.st' ); ?>">clb.rio.st</a>
							<?php else : ?>
								<?php echo esc_html( $clb_disclaimer_text ); ?>
							<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
			</footer>
		<?php endif; ?>
		<?php if ( ! empty( $clb_badges ) ) : ?>
			<div class="clb-card__badges">
				<?php foreach ( $clb_badges as $clb_badge ) : ?>
					<?php
					if ( ! is_array( $clb_badge ) ) {
						continue;
					}
					$clb_badge_kind  = isset( $clb_badge['kind'] ) ? (string) $clb_badge['kind'] : '';
					$clb_badge_label = isset( $clb_badge['label'] ) ? (string) $clb_badge['label'] : '';
					$clb_badge_class = ( isset( $clb_badge['class_name'] )
						&& is_string( $clb_badge['class_name'] )
						&& $clb_badge['class_name'] !== '' )
						? $clb_badge['class_name']
						: 'clb-badge';
					?>
					<?php $clb_badge_attrs = ' class="' . esc_attr( $clb_badge_class . ' clb-badge--' . $clb_badge_kind ) . '"'; ?>
					<?php
					echo '<span' . $clb_badge_attrs . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above.
					echo esc_html( $clb_badge_label );
					echo '</span>';
					?>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
</article>
