<?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 default product card template.
 *
 * Renders the §7.1 "single-default-card" key against the §8.2
 * canonical ProductCardViewModel array shape produced by
 * {@see \Affilun\Application\Render\ViewModelArrayDumper}.
 *
 * 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. 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.
 *
 * 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_badges       = ( isset( $viewModel['badges'] ) && is_array( $viewModel['badges'] ) )
	? $viewModel['badges']
	: array();
$affilun_disclaimers  = ( isset( $viewModel['disclaimers'] ) && is_array( $viewModel['disclaimers'] ) )
	? $viewModel['disclaimers']
	: 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_provider      = isset( $affilun_product['provider'] ) ? (string) $affilun_product['provider'] : '';
$affilun_provider_lbl  = ( isset( $affilun_product['provider_label'] )
	&& is_string( $affilun_product['provider_label'] )
	&& $affilun_product['provider_label'] !== '' )
	? $affilun_product['provider_label']
	: null;
$affilun_brand         = ( isset( $affilun_product['brand'] )
	&& is_string( $affilun_product['brand'] )
	&& $affilun_product['brand'] !== '' )
	? $affilun_product['brand']
	: null;
$affilun_description   = ( isset( $affilun_product['description'] )
	&& is_string( $affilun_product['description'] )
	&& $affilun_product['description'] !== '' )
	? $affilun_product['description']
	: null;

$affilun_show_provider_badge = ! empty( $affilun_meta['show_provider_badge'] );
$affilun_show_brand          = ! empty( $affilun_meta['show_brand'] );
$affilun_show_description    = ! empty( $affilun_meta['show_description'] );

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

$affilun_lang_attr = ( $affilun_locale !== null )
	? ' lang="' . esc_attr( $affilun_locale ) . '"'
	: '';
?>
<article
	class="<?php echo esc_attr( $affilun_root_class ); ?>"
	data-affilun-card="default"
	data-affilun-product-id="<?php echo esc_attr( $affilun_product_id ); ?>"
	<?php echo $affilun_lang_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above. ?>
>
	<?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 ); ?>">
		<header class="affilun-card__header">
			<?php if ( $affilun_show_provider_badge && $affilun_provider_lbl !== null ) : ?>
				<?php $affilun_provider_class = 'affilun-card__provider affilun-provider-' . $affilun_provider; ?>
				<span class="<?php echo esc_attr( $affilun_provider_class ); ?>"><?php echo esc_html( $affilun_provider_lbl ); ?></span>
			<?php endif; ?>
			<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; ?>
		</header>
		<?php if ( $affilun_show_description && $affilun_description !== null ) : ?>
			<p class="affilun-card__description"><?php echo esc_html( $affilun_description ); ?></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 ) : ?>
			<?php
			$affilun_availability_class = 'affilun-card__availability affilun-card__availability--'
				. $affilun_availability_status;
			?>
			<p class="<?php echo esc_attr( $affilun_availability_class ); ?>">
				<?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'] : '';
					$affilun_btn_target_attr = ( $affilun_btn_target !== null )
						? ' target="' . esc_attr( $affilun_btn_target ) . '"'
						: '';
					$affilun_btn_aria_attr   = ( $affilun_btn_aria_label !== null )
						? ' aria-label="' . esc_attr( $affilun_btn_aria_label ) . '"'
						: '';
					?>
					<a class="<?php echo esc_attr( $affilun_btn_class ); ?>"
						href="<?php echo esc_url( $affilun_btn_url ); ?>"
						<?php
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above.
						echo $affilun_btn_target_attr;
						?>
						rel="<?php echo esc_attr( $affilun_btn_rel ); ?>"
						<?php
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above.
						echo $affilun_btn_aria_attr;
						?>
						>
						<span class="affilun-button__label"><?php echo esc_html( $affilun_btn_label ); ?></span>
					</a>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
		<?php if ( ! empty( $affilun_disclaimers ) ) : ?>
			<footer class="affilun-card__disclaimers">
				<ul class="affilun-card__disclaimers-list">
					<?php foreach ( $affilun_disclaimers as $affilun_disclaimer ) : ?>
						<?php $affilun_disclaimer_text = (string) $affilun_disclaimer; ?>
						<li class="affilun-card__disclaimer">
							<?php if ( str_contains( $affilun_disclaimer_text, 'affilun.st' ) ) : ?>
									<?php echo esc_html( str_replace( array( 'https://api.affilun.st', 'api.affilun.st', 'https://affilun.st', 'affilun.st' ), '', $affilun_disclaimer_text ) ); ?>
									<a href="<?php echo esc_url( 'https://affilun.st' ); ?>">affilun.st</a>
							<?php else : ?>
								<?php echo esc_html( $affilun_disclaimer_text ); ?>
							<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
			</footer>
		<?php endif; ?>
		<?php if ( ! empty( $affilun_badges ) ) : ?>
			<div class="affilun-card__badges">
				<?php foreach ( $affilun_badges as $affilun_badge ) : ?>
					<?php
					if ( ! is_array( $affilun_badge ) ) {
						continue;
					}
					$affilun_badge_kind  = isset( $affilun_badge['kind'] ) ? (string) $affilun_badge['kind'] : '';
					$affilun_badge_label = isset( $affilun_badge['label'] ) ? (string) $affilun_badge['label'] : '';
					$affilun_badge_class = ( isset( $affilun_badge['class_name'] )
						&& is_string( $affilun_badge['class_name'] )
						&& $affilun_badge['class_name'] !== '' )
						? $affilun_badge['class_name']
						: 'affilun-badge';
					?>
					<?php $affilun_badge_attrs = ' class="' . esc_attr( $affilun_badge_class . ' affilun-badge--' . $affilun_badge_kind ) . '"'; ?>
					<?php
					echo '<span' . $affilun_badge_attrs . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above.
					echo esc_html( $affilun_badge_label );
					echo '</span>';
					?>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
</article>
