<?php
/**
 * Single vehicle detail — design-system .aii-detail.
 *
 * Override by copying this file to:
 *   {your-theme}/automotive-feed-import/single-vehicle.php
 *
 * Expects $detail (array) in scope — built by
 * AutomotiveFeedImport::get_vehicle_detail_data(). Markup is intentionally
 * semantic (single <h1>, <dl> spec pairs) so a Pro build can emit JSON-LD
 * Vehicle schema from the same data. Free build = native gallery links +
 * "Contact Dealer" mailto; the Pro lightbox enhances the gallery in place.
 *
 * @package AutomotiveFeedImport
 */

if (! defined('ABSPATH')) {
	exit;
}

if (empty($detail) || ! is_array($detail)) {
	return;
}

$d         = $detail;
$img_count = count($d['gallery']);
?>
<div class="aii-detail">
	<div class="aii-detail__top">

		<div class="aii-gallery" data-aii-gallery>
			<div class="aii-gallery__main">
				<div class="aii-photo">
					<?php if ($img_count > 0) : $g0 = $d['gallery'][0]; ?>
						<a href="<?php echo esc_url($g0['full']); ?>" data-aii-index="0" aria-label="<?php
							/* translators: %d: total photo count. */
							echo esc_attr(sprintf(__('View photo 1 of %d', 'automotive-feed-import'), $img_count));
						?>">
							<img src="<?php echo esc_url($g0['large'] ? $g0['large'] : $g0['full']); ?>" alt="<?php echo esc_attr($g0['alt']); ?>" loading="eager">
						</a>
						<span class="aii-photo__count"><?php
							echo afi_aii_icon('i-camera'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?> <?php
							/* translators: %d: number of photos. */
							echo esc_html(sprintf(_n('%d photo', '%d photos', $img_count, 'automotive-feed-import'), $img_count));
						?></span>
					<?php else : ?>
						<div class="aii-photo__placeholder"><?php
							echo afi_aii_icon('i-nophoto'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?><span><?php esc_html_e('No photos available', 'automotive-feed-import'); ?></span></div>
					<?php endif; ?>
				</div>
			</div>

			<?php if ($img_count > 1) : ?>
				<div class="aii-gallery__thumbs">
					<?php
					$max  = 5;
					$show = min($img_count, $max);
					for ($i = 0; $i < $show; $i++) :
						$g       = $d['gallery'][$i];
						$is_more = ($img_count > $max && $i === $max - 1);
						if ($is_more) :
							?>
							<a class="aii-thumb aii-thumb--more" href="<?php echo esc_url($g['full']); ?>" data-aii-index="<?php echo (int) $i; ?>">+<?php
								echo (int) ($img_count - ($max - 1));
							?> <?php esc_html_e('more', 'automotive-feed-import'); ?></a>
						<?php else : ?>
							<a class="aii-thumb<?php echo 0 === $i ? ' is-active' : ''; ?>" href="<?php echo esc_url($g['full']); ?>" data-aii-index="<?php echo (int) $i; ?>"><div class="aii-photo"><img src="<?php echo esc_url($g['thumb'] ? $g['thumb'] : $g['full']); ?>" alt="" loading="lazy"></div></a>
						<?php endif;
					endfor; ?>
				</div>
			<?php endif; ?>
		</div>

		<div class="aii-summary">
			<div class="aii-summary__head">
				<?php if (! empty($d['condition']) || '' !== $d['location']) : ?>
					<div class="aii-summary__badges">
						<?php if (! empty($d['condition'])) : ?>
							<span class="aii-badge aii-badge--<?php echo esc_attr($d['condition']['class']); ?>"><?php
								if ('new' === $d['condition']['class']) {
									echo '<span class="aii-badge__dot"></span>';
								}
								echo esc_html($d['condition']['label']);
							?></span>
						<?php endif; ?>
						<?php if ('' !== $d['location']) : ?>
							<span class="aii-badge aii-badge--location"><?php
								echo afi_aii_icon('i-pin'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							?> <?php echo esc_html($d['location']); ?></span>
						<?php endif; ?>
					</div>
				<?php endif; ?>

				<h1 class="aii-summary__title"><?php echo esc_html($d['title']); ?></h1>

				<?php if ('' !== $d['trim']) : ?>
					<div class="aii-summary__trim"><?php echo esc_html($d['trim']); ?></div>
				<?php endif; ?>
			</div>

			<div class="aii-priceblock">
				<?php if ('' !== $d['price']) : ?>
					<div class="aii-priceblock__row">
						<span class="aii-priceblock__price"><?php echo esc_html($d['price']); ?></span>
						<?php if ('' !== $d['msrp']) : ?>
							<span class="aii-priceblock__msrp"><?php echo esc_html($d['msrp']); ?></span>
						<?php endif; ?>
					</div>
				<?php endif; ?>
				<?php if ('' !== $d['note']) : ?>
					<span class="aii-priceblock__note"><?php echo esc_html($d['note']); ?></span>
				<?php endif; ?>
				<?php
				$afi_default_cta = '';
				if ('' !== $d['mailto']) {
					$afi_default_cta = '<div class="aii-priceblock__cta"><a class="aii-btn aii-btn--primary aii-btn--lg aii-btn--block" href="' . esc_url($d['mailto']) . '">'
						. afi_aii_icon('i-mail') . ' ' . esc_html__('Contact Dealer', 'automotive-feed-import') . '</a></div>';
				}
				/**
				 * Filter the vehicle CTA. Free = "Contact Dealer" mailto button;
				 * the Pro lead-capture form replaces it via this filter.
				 *
				 * @param string $cta     Default CTA HTML.
				 * @param int    $post_id Vehicle post ID.
				 * @param array  $detail  Detail data array.
				 */
				echo apply_filters('afi_vehicle_cta', $afi_default_cta, get_the_ID(), $d); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				?>
			</div>

			<?php if (! empty($d['keyfacts'])) : ?>
				<div class="aii-keyfacts">
					<?php foreach ($d['keyfacts'] as $kf) : ?>
						<div class="aii-keyfact"><?php
							echo afi_aii_icon($kf['icon']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?><div><div class="aii-keyfact__v"><?php echo esc_html($kf['v']); ?></div><div class="aii-keyfact__k"><?php echo esc_html($kf['k']); ?></div></div></div>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>
		</div>
	</div>

	<?php if (! empty($d['specs'])) : ?>
		<div class="aii-section">
			<h2 class="aii-section__title"><?php
				echo afi_aii_icon('i-info'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			?> <?php esc_html_e('Vehicle specifications', 'automotive-feed-import'); ?></h2>
			<div class="aii-specs-table">
				<dl class="aii-specs-grid">
					<?php foreach ($d['specs'] as $s) : ?>
						<div class="aii-specrow"><?php
							echo afi_aii_icon($s['icon'], 'aii-specrow__ic'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?><dt class="aii-specrow__k"><?php echo esc_html($s['label']); ?></dt><dd class="aii-specrow__v<?php echo ! empty($s['mono']) ? ' is-mono' : ''; ?>"><?php echo esc_html($s['value']); ?></dd></div>
					<?php endforeach; ?>
				</dl>
			</div>
		</div>
	<?php endif; ?>

	<?php if ('' !== $d['description']) : ?>
		<div class="aii-section">
			<h2 class="aii-section__title"><?php
				echo afi_aii_icon('i-car'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			?> <?php esc_html_e('Description', 'automotive-feed-import'); ?></h2>
			<div class="aii-description"><?php echo wp_kses_post($d['description']); ?></div>
		</div>
	<?php endif; ?>
</div>
