<?php
/**
 * 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 \CommerceLinkBuilder\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 \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 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.
 *
 * 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_price       = isset( $viewModel['price'] ) && is_array( $viewModel['price'] ) ? $viewModel['price'] : array();
$clb_buttons     = isset( $viewModel['buttons'] ) && is_array( $viewModel['buttons'] ) ? $viewModel['buttons'] : 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_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_detail_url = isset( $clb_product['detail_url'] ) ? (string) $clb_product['detail_url'] : '';
$clb_title      = isset( $clb_product['title'] ) ? (string) $clb_product['title'] : '';

$clb_price_visible = ! empty( $clb_price['visible'] );
$clb_price_label   = isset( $clb_price['label'] ) && is_string( $clb_price['label'] ) ? $clb_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.
$clb_primary_button = null;
foreach ( $clb_buttons as $clb_candidate_button ) {
	if ( is_array( $clb_candidate_button ) ) {
		$clb_primary_button = $clb_candidate_button;
		break;
	}
}
?>
<aside class="<?php echo esc_attr( $clb_root_class ); ?>" data-clb-card="minimal-cta"<?php if ( $clb_locale !== null ) : ?> lang="<?php echo esc_attr( $clb_locale ); ?>"<?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_price_visible ) : ?>
			<p class="<?php echo esc_attr( $clb_price_class ); ?>"><?php echo esc_html( $clb_price_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>
</aside>
