<?php
/**
 * Single fallback product card template.
 *
 * Renders the §7.1 "single-fallback-card" key against the §8.4
 * canonical FallbackCardViewModel 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.4 view model with a
 * synthetic `_render_meta` block attached. The shape is:
 *
 *   array{
 *     type: 'fallback-card',
 *     template: string,
 *     reason: string,
 *     title: string|null,
 *     primary_button: array{
 *       provider: string,
 *       label: string,
 *       url: string,
 *       target: string|null,
 *       rel: string,
 *       class_name: string,
 *       aria_label: string|null,
 *     }|null,
 *     note: string|null,
 *     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`):
 *
 *   - §13.3: When both `title` and `primary_button` are null,
 *     the template renders an empty `<article>` shell carrying
 *     only the `data-clb-reason` data attribute and the root
 *     class. This satisfies selectors / counters while producing
 *     no visible content.
 *   - §23.1: The admin-facing `note` MUST NOT leak to non-admin
 *     surfaces. Phase-5 gate: emit only when
 *     `_render_meta.debug === true`. Slice 5h / Phase 11 may
 *     tighten this with an explicit admin-capability check.
 *   - §10 / §15: BEM class strings come pre-tokenized in
 *     `$viewModel['classes']`. The template stamps them
 *     verbatim and never synthesizes its own modifier strings,
 *     except for the canonical `clb-card--fallback` modifier
 *     §13.3 specifies for the fallback variant root.
 *   - All dynamic output is escaped with `esc_html` /
 *     `esc_attr` / `esc_url`.
 *
 * 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.
 *
 * @package CommerceLinkBuilder
 *
 * @var array<string, mixed> $viewModel §8.4 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_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_title_class   = isset( $clb_classes['title'] ) ? (string) $clb_classes['title'] : 'clb-card__title';
$clb_buttons_class = isset( $clb_classes['buttons'] ) ? (string) $clb_classes['buttons'] : 'clb-card__buttons';

$clb_reason = isset( $viewModel['reason'] ) ? (string) $viewModel['reason'] : '';
$clb_title  = isset( $viewModel['title'] ) && is_string( $viewModel['title'] ) && $viewModel['title'] !== '' ? $viewModel['title'] : null;
$clb_button = isset( $viewModel['primary_button'] ) && is_array( $viewModel['primary_button'] ) ? $viewModel['primary_button'] : null;
$clb_note   = isset( $viewModel['note'] ) && is_string( $viewModel['note'] ) && $viewModel['note'] !== '' ? $viewModel['note'] : null;

$clb_locale = isset( $clb_render_meta['locale'] ) && is_string( $clb_render_meta['locale'] ) && $clb_render_meta['locale'] !== '' ? $clb_render_meta['locale'] : null;
$clb_debug  = ! empty( $clb_render_meta['debug'] );

$clb_root_attr_class = $clb_root_class . ' clb-card--fallback';
?>
<article class="<?php echo esc_attr( $clb_root_attr_class ); ?>" data-clb-card="fallback" data-clb-reason="<?php echo esc_attr( $clb_reason ); ?>"<?php if ( $clb_locale !== null ) : ?> lang="<?php echo esc_attr( $clb_locale ); ?>"<?php endif; ?>>
	<?php if ( $clb_title !== null ) : ?>
		<h3 class="<?php echo esc_attr( $clb_title_class ); ?>"><?php echo esc_html( $clb_title ); ?></h3>
	<?php endif; ?>
	<?php if ( $clb_button !== null ) : ?>
		<?php
		$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'] : '';
		?>
		<div class="<?php echo esc_attr( $clb_buttons_class ); ?>">
			<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; ?>
	<?php if ( $clb_debug && $clb_note !== null ) : ?>
		<p class="clb-card__fallback-note"><?php echo esc_html( $clb_note ); ?></p>
	<?php endif; ?>
</article>
