<?php
/**
 * Vehicle card partial — design-system .aii-card.
 *
 * Override by copying this file to:
 *   {your-theme}/automotive-feed-import/card-vehicle.php
 *
 * Expects $card (array) in scope — built by
 * AutomotiveFeedImport::get_vehicle_card_data(). Every slot is pre-checked, so
 * empty fields are simply omitted (no blank rows, no dead UI).
 *
 * @package AutomotiveFeedImport
 */

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

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

$aii_card_classes = 'aii-card' . (! empty($card['is_sold']) ? ' aii-card--sold' : '');
?>
<article class="<?php echo esc_attr($aii_card_classes); ?>">
	<div class="aii-card__media">
		<?php if (! empty($card['is_sold']) || ! empty($card['condition'])) : ?>
			<div class="aii-card__badges">
				<?php if (! empty($card['is_sold'])) : ?>
					<span class="aii-badge aii-badge--sold"><?php esc_html_e('Sold', 'automotive-feed-import'); ?></span>
				<?php endif; ?>
				<?php if (! empty($card['condition'])) : ?>
					<span class="aii-badge aii-badge--<?php echo esc_attr($card['condition']['class']); ?>"><?php
						if ('new' === $card['condition']['class']) {
							echo '<span class="aii-badge__dot"></span>';
						}
						echo esc_html($card['condition']['label']);
					?></span>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<div class="aii-photo">
			<?php if (! empty($card['photo_img'])) : ?>
				<a href="<?php echo esc_url($card['permalink']); ?>" tabindex="-1" aria-hidden="true"><?php
					// Safe WordPress core output (get_the_post_thumbnail()).
					echo $card['photo_img']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				?></a>
				<?php if (! empty($card['photo_count'])) : ?>
					<span class="aii-photo__count"><?php
						echo afi_aii_icon('i-camera'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					?> <?php echo esc_html($card['photo_count']); ?></span>
				<?php endif; ?>
			<?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>

	<div class="aii-card__body">
		<?php if ('' !== $card['price']) : ?>
			<div class="aii-card__pricerow">
				<span class="aii-card__price"><?php echo esc_html($card['price']); ?></span>
				<?php if ('' !== $card['msrp']) : ?>
					<span class="aii-card__msrp"><?php echo esc_html($card['msrp']); ?></span>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<h3 class="aii-card__title"><a href="<?php echo esc_url($card['permalink']); ?>"><?php echo esc_html($card['title']); ?></a></h3>

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

		<?php if (! empty($card['specs'])) : ?>
			<div class="aii-card__specs">
				<?php foreach ($card['specs'] as $aii_spec) : ?>
					<span class="aii-spec"><?php
						echo afi_aii_icon($aii_spec['icon'], 'aii-spec__ic'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					?> <?php echo esc_html($aii_spec['text']); ?></span>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

		<div class="aii-card__meta">
			<?php if ('' !== $card['location']) : ?>
				<span class="aii-card__loc"><?php
					echo afi_aii_icon('i-pin'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				?><span><?php echo esc_html($card['location']); ?></span></span>
			<?php endif; ?>
			<a class="aii-card__cta" href="<?php echo esc_url($card['permalink']); ?>"><?php esc_html_e('View details', 'automotive-feed-import'); ?> <?php
				echo afi_aii_icon('i-arrow'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			?></a>
		</div>
	</div>
</article>
