<?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 fallback product card template.
 *
 * Renders the §7.1 "single-fallback-card" key against the §8.4
 * canonical FallbackCardViewModel 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.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-affilun-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 `affilun-card--fallback` modifier
 *     §13.3 specifies for the fallback variant root.
 *   - All dynamic output is escaped with `esc_html` /
 *     `esc_attr` / `esc_url`.
 *
 * 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.4 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_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_title_class   = isset( $affilun_classes['title'] ) ? (string) $affilun_classes['title'] : 'affilun-card__title';
$affilun_buttons_class = isset( $affilun_classes['buttons'] ) ? (string) $affilun_classes['buttons'] : 'affilun-card__buttons';

$affilun_reason = isset( $viewModel['reason'] ) ? (string) $viewModel['reason'] : '';
$affilun_title  = ( isset( $viewModel['title'] ) && is_string( $viewModel['title'] ) && $viewModel['title'] !== '' )
	? $viewModel['title']
	: null;
$affilun_button = ( isset( $viewModel['primary_button'] ) && is_array( $viewModel['primary_button'] ) )
	? $viewModel['primary_button']
	: null;
$affilun_note   = ( isset( $viewModel['note'] ) && is_string( $viewModel['note'] ) && $viewModel['note'] !== '' )
	? $viewModel['note']
	: null;

$affilun_locale           = ( isset( $affilun_render_meta['locale'] )
	&& is_string( $affilun_render_meta['locale'] )
	&& $affilun_render_meta['locale'] !== '' )
	? $affilun_render_meta['locale']
	: null;
$affilun_debug            = ! empty( $affilun_render_meta['debug'] );
$affilun_tracking_enabled = ! empty( $affilun_render_meta['tracking_enabled'] );
$affilun_locale_key       = $affilun_locale === null ? 'ja' : strtolower( str_replace( '_', '-', $affilun_locale ) );
$affilun_is_ja            = $affilun_locale_key === 'ja' || strpos( $affilun_locale_key, 'ja-' ) === 0;

$affilun_fallback_copy        = $affilun_is_ja
	? '商品情報・価格・在庫状況はリンク先のAmazonでご確認ください。'
	: 'Check current product details, price, and availability on Amazon.';
$affilun_affiliate_disclosure = $affilun_is_ja
	? '※本リンクはアフィリエイトリンクです。'
	: 'This is an affiliate link.';

$affilun_root_attr_class = $affilun_root_class . ' affilun-card--fallback';
$affilun_lang_attr       = $affilun_locale !== null
	? ' lang="' . esc_attr( $affilun_locale ) . '"'
	: '';
?>
<article
	class="<?php echo esc_attr( $affilun_root_attr_class ); ?>"
	data-affilun-card="fallback"
	data-affilun-reason="<?php echo esc_attr( $affilun_reason ); ?>"
	<?php
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped.
	echo $affilun_lang_attr;
	?>
>
	<?php if ( $affilun_title !== null ) : ?>
		<h3 class="<?php echo esc_attr( $affilun_title_class ); ?>"><?php echo esc_html( $affilun_title ); ?></h3>
	<?php endif; ?>
	<?php if ( $affilun_button !== null ) : ?>
		<?php
		$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 ) . '"'
			: '';
		?>
		<p class="affilun-card__fallback-copy"><?php echo esc_html( $affilun_fallback_copy ); ?></p>
		<div class="<?php echo esc_attr( $affilun_buttons_class ); ?>">
			<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.
				echo $affilun_btn_target_attr;
				?>
				rel="<?php echo esc_attr( $affilun_btn_rel ); ?>"
				<?php
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped.
				echo $affilun_btn_aria_attr;
				?>
			>
				<span class="affilun-button__label"><?php echo esc_html( $affilun_btn_label ); ?></span>
			</a>
		</div>
		<?php if ( $affilun_tracking_enabled ) : ?>
			<footer class="affilun-card__fallback-disclosure"><?php echo esc_html( $affilun_affiliate_disclosure ); ?></footer>
		<?php endif; ?>
	<?php endif; ?>
	<?php if ( $affilun_debug && $affilun_note !== null ) : ?>
		<p class="affilun-card__fallback-note"><?php echo esc_html( $affilun_note ); ?></p>
	<?php endif; ?>
</article>
