<?php
// phpcs:ignoreFile -- Direct-access guard must stay at the top for Plugin Check; escaping is covered in template tests.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Single compact product card template.
 *
 * Renders the §7.1 "single-compact-card" key against the §8.2
 * canonical ProductCardViewModel array shape produced by
 * {@see \Affilun\Application\Render\ViewModelArrayDumper}.
 *
 * Compact variation of {@see ./single-default-card.php} optimized
 * for 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
 *     compact card tall enough for a single product to remain
 *     visually scannable.
 *   - Optional availability label (driven by
 *     `availability.visible`).
 *   - The CTA buttons produced by the ViewModel, using the same
 *     button list as the default card so editor previews and
 *     frontend compact cards expose the same enabled-provider buttons.
 *     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
 *     higher in the containing surface; carrying per-card disclaimer
 *     text in a compact surface would dwarf the cell content).
 *   - Badges block.
 *
 * The template is invoked by
 * {@see \Affilun\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.
 *
 * The template exits on direct file access and also returns silently
 * when its single contract input is missing or has the wrong type.
 *
 * @package Affilun
 *
 * @var array<string, mixed> $viewModel §8.2 array shape with `_render_meta` attached.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

$affilun_classes      = isset( $viewModel['classes'] ) && is_array( $viewModel['classes'] ) ? $viewModel['classes'] : array();
$affilun_product      = isset( $viewModel['product'] ) && is_array( $viewModel['product'] ) ? $viewModel['product'] : array();
$affilun_images       = isset( $viewModel['images'] ) && is_array( $viewModel['images'] ) ? $viewModel['images'] : array();
$affilun_price        = isset( $viewModel['price'] ) && is_array( $viewModel['price'] ) ? $viewModel['price'] : array();
$affilun_availability = isset( $viewModel['availability'] ) && is_array( $viewModel['availability'] ) ? $viewModel['availability'] : array();
$affilun_buttons      = isset( $viewModel['buttons'] ) && is_array( $viewModel['buttons'] ) ? $viewModel['buttons'] : array();
$affilun_meta         = isset( $viewModel['meta'] ) && is_array( $viewModel['meta'] ) ? $viewModel['meta'] : array();
$affilun_render_meta  = isset( $viewModel['_render_meta'] ) && is_array( $viewModel['_render_meta'] ) ? $viewModel['_render_meta'] : array();

$affilun_root_class    = isset( $affilun_classes['root'] ) ? (string) $affilun_classes['root'] : 'affilun-card';
$affilun_image_class   = isset( $affilun_classes['image'] ) ? (string) $affilun_classes['image'] : 'affilun-card__image';
$affilun_body_class    = isset( $affilun_classes['body'] ) ? (string) $affilun_classes['body'] : 'affilun-card__body';
$affilun_title_class   = isset( $affilun_classes['title'] ) ? (string) $affilun_classes['title'] : 'affilun-card__title';
$affilun_price_class   = isset( $affilun_classes['price'] ) ? (string) $affilun_classes['price'] : 'affilun-card__price';
$affilun_buttons_class = isset( $affilun_classes['buttons'] ) ? (string) $affilun_classes['buttons'] : 'affilun-card__buttons';

$affilun_locale        = isset( $affilun_render_meta['locale'] ) && is_string( $affilun_render_meta['locale'] ) && $affilun_render_meta['locale'] !== '' ? $affilun_render_meta['locale'] : null;
$affilun_primary_image = isset( $affilun_images['primary'] ) && is_array( $affilun_images['primary'] ) ? $affilun_images['primary'] : null;
$affilun_detail_url    = isset( $affilun_product['detail_url'] ) ? (string) $affilun_product['detail_url'] : '';
$affilun_affiliate_url = isset( $affilun_product['affiliate_url'] ) ? (string) $affilun_product['affiliate_url'] : '';
$affilun_link_url      = $affilun_affiliate_url !== '' ? $affilun_affiliate_url : $affilun_detail_url;
$affilun_product_id    = isset( $affilun_product['id'] ) ? (string) $affilun_product['id'] : '';
$affilun_title         = isset( $affilun_product['title'] ) ? (string) $affilun_product['title'] : '';
$affilun_brand         = isset( $affilun_product['brand'] ) && is_string( $affilun_product['brand'] ) && $affilun_product['brand'] !== '' ? $affilun_product['brand'] : null;

$affilun_show_brand = ! empty( $affilun_meta['show_brand'] );

$affilun_price_visible        = ! empty( $affilun_price['visible'] );
$affilun_price_label          = isset( $affilun_price['label'] ) && is_string( $affilun_price['label'] ) ? $affilun_price['label'] : '';
$affilun_price_observed_at    = isset( $affilun_price['observed_at_label'] )
	&& is_string( $affilun_price['observed_at_label'] )
	&& $affilun_price['observed_at_label'] !== ''
	? $affilun_price['observed_at_label']
	: null;
$affilun_availability_visible = ! empty( $affilun_availability['visible'] );
$affilun_availability_label   = isset( $affilun_availability['label'] ) && is_string( $affilun_availability['label'] ) && $affilun_availability['label'] !== '' ? $affilun_availability['label'] : null;
$affilun_availability_status  = isset( $affilun_availability['status'] ) ? (string) $affilun_availability['status'] : 'unknown';

?>
<article class="<?php echo esc_attr( $affilun_root_class ); ?>" data-affilun-card="compact" data-affilun-product-id="<?php echo esc_attr( $affilun_product_id ); ?>"
<?php
if ( $affilun_locale !== null ) :
	?>
	lang="<?php echo esc_attr( $affilun_locale ); ?>"<?php endif; ?>>
	<?php if ( $affilun_primary_image !== null ) : ?>
		<?php
		$affilun_img_url     = isset( $affilun_primary_image['url'] ) ? (string) $affilun_primary_image['url'] : '';
		$affilun_img_alt     = isset( $affilun_primary_image['alt'] ) ? (string) $affilun_primary_image['alt'] : '';
		$affilun_img_loading = isset( $affilun_primary_image['loading'] ) ? (string) $affilun_primary_image['loading'] : 'lazy';
		$affilun_img_width   = isset( $affilun_primary_image['width'] ) && is_int( $affilun_primary_image['width'] ) ? $affilun_primary_image['width'] : null;
		$affilun_img_height  = isset( $affilun_primary_image['height'] ) && is_int( $affilun_primary_image['height'] ) ? $affilun_primary_image['height'] : null;
		$affilun_img_dims    = ( $affilun_img_width !== null && $affilun_img_height !== null );
		?>
		<figure class="<?php echo esc_attr( $affilun_image_class ); ?>">
			<a href="<?php echo esc_url( $affilun_link_url ); ?>" tabindex="-1" aria-hidden="true">
				<img
					src="<?php echo esc_url( $affilun_img_url ); ?>"
					alt="<?php echo esc_attr( $affilun_img_alt ); ?>"
					loading="<?php echo esc_attr( $affilun_img_loading ); ?>"
					<?php if ( $affilun_img_dims ) : ?>
					width="<?php echo esc_attr( (string) $affilun_img_width ); ?>"
					height="<?php echo esc_attr( (string) $affilun_img_height ); ?>"
					<?php endif; ?>
				/>
			</a>
		</figure>
	<?php endif; ?>
	<div class="<?php echo esc_attr( $affilun_body_class ); ?>">
		<h3 class="<?php echo esc_attr( $affilun_title_class ); ?>">
			<a href="<?php echo esc_url( $affilun_link_url ); ?>"><?php echo esc_html( $affilun_title ); ?></a>
		</h3>
		<?php if ( $affilun_show_brand && $affilun_brand !== null ) : ?>
			<p class="affilun-card__brand"><?php echo esc_html( $affilun_brand ); ?></p>
		<?php endif; ?>
		<?php if ( $affilun_price_visible ) : ?>
			<p class="<?php echo esc_attr( $affilun_price_class ); ?>">
				<span class="affilun-card__price-amount"><?php echo esc_html( $affilun_price_label ); ?></span>
				<?php if ( $affilun_price_observed_at !== null ) : ?>
					<span class="affilun-card__price-observed-at"><time datetime="<?php echo esc_attr( $affilun_price_observed_at ); ?>"><?php echo esc_html( $affilun_price_observed_at ); ?></time></span>
				<?php endif; ?>
			</p>
		<?php endif; ?>
		<?php if ( $affilun_availability_visible && $affilun_availability_label !== null ) : ?>
			<p class="affilun-card__availability affilun-card__availability--<?php echo esc_attr( $affilun_availability_status ); ?>"><?php echo esc_html( $affilun_availability_label ); ?></p>
		<?php endif; ?>
		<?php if ( ! empty( $affilun_buttons ) ) : ?>
			<div class="<?php echo esc_attr( $affilun_buttons_class ); ?>">
				<?php foreach ( $affilun_buttons as $affilun_button ) : ?>
					<?php
					if ( ! is_array( $affilun_button ) ) {
						continue;
					}
					$affilun_btn_class      = isset( $affilun_button['class_name'] ) ? (string) $affilun_button['class_name'] : 'affilun-button';
					$affilun_btn_url        = isset( $affilun_button['url'] ) ? (string) $affilun_button['url'] : '';
					$affilun_btn_target     = isset( $affilun_button['target'] ) && is_string( $affilun_button['target'] ) && $affilun_button['target'] !== '' ? $affilun_button['target'] : null;
					$affilun_btn_rel        = isset( $affilun_button['rel'] ) ? (string) $affilun_button['rel'] : '';
					$affilun_btn_aria_label = isset( $affilun_button['aria_label'] ) && is_string( $affilun_button['aria_label'] ) && $affilun_button['aria_label'] !== '' ? $affilun_button['aria_label'] : null;
					$affilun_btn_label      = isset( $affilun_button['label'] ) ? (string) $affilun_button['label'] : '';
					?>
					<a class="<?php echo esc_attr( $affilun_btn_class ); ?>"
						href="<?php echo esc_url( $affilun_btn_url ); ?>"
						<?php
						if ( $affilun_btn_target !== null ) :
							?>
							target="<?php echo esc_attr( $affilun_btn_target ); ?>"<?php endif; ?>
						rel="<?php echo esc_attr( $affilun_btn_rel ); ?>"
						<?php
						if ( $affilun_btn_aria_label !== null ) :
							?>
							aria-label="<?php echo esc_attr( $affilun_btn_aria_label ); ?>"<?php endif; ?>>
						<span class="affilun-button__label"><?php echo esc_html( $affilun_btn_label ); ?></span>
					</a>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
</article>
