<?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 minimal CTA product card template.
 *
 * Renders the §7.1 "single-minimal-cta" key against the §8.2
 * canonical ProductCardViewModel array shape produced by
 * {@see \Affilun\Application\Render\ViewModelArrayDumper}.
 *
 * Minimal call-to-action variation of
 * {@see ./single-default-card.php} optimized for inline contexts
 * where the surrounding article already carries the product's
 * narrative content (an in-paragraph affiliate nudge, a sidebar
 * promo strip, an end-of-section "buy this" stinger). The
 * template intentionally drops every chrome axis except the bare
 * "what is it / how much / where to get it" triad.
 *
 * What the minimal CTA template renders:
 *
 *   - Title (canonical focusable detail-page link).
 *   - Optional price label (driven by `price.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 minimal CTA template intentionally OMITS vs. the
 * default and compact templates:
 *
 *   - Primary image / gallery (the surrounding article's media
 *     is authoritative; a card image would visually compete).
 *   - Brand line.
 *   - Description block.
 *   - Provider badge.
 *   - Availability label (the in-line CTA leaves availability
 *     verification to the destination page).
 *   - `observed_at_label` price metadata.
 *   - Disclaimer footer (per §11.2 the surrounding context's
 *     site-wide disclaimer is authoritative for the inline use
 *     case; per-card disclaimer text would dwarf the CTA itself).
 *   - Badges block.
 *   - Multi-button CTA strip.
 *
 * 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 minimal CTA 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 hidden
 *     price or empty buttons list simply omits the corresponding
 *     region. A title-only minimal CTA (no price, no button) is
 *     the documented worst-case fallback rather than an error.
 *   - §20: The title link is the canonical focusable detail-page
 *     link; there is no decorative image link in this template.
 *   - 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_price       = isset( $viewModel['price'] ) && is_array( $viewModel['price'] ) ? $viewModel['price'] : array();
$affilun_buttons     = isset( $viewModel['buttons'] ) && is_array( $viewModel['buttons'] ) ? $viewModel['buttons'] : 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_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_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_price_visible = ! empty( $affilun_price['visible'] );
$affilun_price_label   = isset( $affilun_price['label'] ) && is_string( $affilun_price['label'] ) ? $affilun_price['label'] : '';

// Minimal-CTA variation: surface only the FIRST button the
// ViewModel produced after Phase-5 button-policy filtering.
// Inline CTAs deliberately collapse to a single primary action so
// the surrounding paragraph flow stays intact.
$affilun_primary_button = null;
foreach ( $affilun_buttons as $affilun_candidate_button ) {
	if ( is_array( $affilun_candidate_button ) ) {
		$affilun_primary_button = $affilun_candidate_button;
		break;
	}
}
?>
<aside class="<?php echo esc_attr( $affilun_root_class ); ?>" data-affilun-card="minimal-cta" 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; ?>>
	<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_price_visible ) : ?>
			<p class="<?php echo esc_attr( $affilun_price_class ); ?>"><?php echo esc_html( $affilun_price_label ); ?></p>
		<?php endif; ?>
		<?php if ( $affilun_primary_button !== null ) : ?>
			<div class="<?php echo esc_attr( $affilun_buttons_class ); ?>">
				<?php
				$affilun_btn_class      = isset( $affilun_primary_button['class_name'] ) ? (string) $affilun_primary_button['class_name'] : 'affilun-button';
				$affilun_btn_url        = isset( $affilun_primary_button['url'] ) ? (string) $affilun_primary_button['url'] : '';
				$affilun_btn_target     = isset( $affilun_primary_button['target'] ) && is_string( $affilun_primary_button['target'] ) && $affilun_primary_button['target'] !== '' ? $affilun_primary_button['target'] : null;
				$affilun_btn_rel        = isset( $affilun_primary_button['rel'] ) ? (string) $affilun_primary_button['rel'] : '';
				$affilun_btn_aria_label = isset( $affilun_primary_button['aria_label'] ) && is_string( $affilun_primary_button['aria_label'] ) && $affilun_primary_button['aria_label'] !== '' ? $affilun_primary_button['aria_label'] : null;
				$affilun_btn_label      = isset( $affilun_primary_button['label'] ) ? (string) $affilun_primary_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>
			</div>
		<?php endif; ?>
	</div>
</aside>
