<?php
/**
 * Template part: vehicle-similar
 *
 * Renders the "Similar Vehicles" strip — vehicle cards for listings that
 * share the current listing's body style or make. Server-side; no AJAX.
 *
 * Pulls candidates through SimilarVehicles::find() so the heuristic stays
 * in one place and is easy to swap out per-tier.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 *
 * @var int $post_id Reference vehicle ID.
 * @var int $limit   Max cards (default 4).
 */

declare(strict_types=1);

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

use ThemeREX\AutoDealer\Frontend\SimilarVehicles;
use ThemeREX\AutoDealer\Frontend\TemplateLoader;

$post_id = isset( $post_id ) ? (int) $post_id : (int) get_the_ID();
$limit   = isset( $limit ) ? max( 1, min( 12, (int) $limit ) ) : 4;

if ( $post_id <= 0 ) {
	return;
}

$similar_ids = SimilarVehicles::find( $post_id, $limit );

if ( empty( $similar_ids ) ) {
	return;
}
?>
<section class="trx-ad-similar" aria-labelledby="trx-ad-similar-heading">
	<h2 id="trx-ad-similar-heading" class="trx-ad-similar__heading">
		<?php esc_html_e( 'Similar Vehicles', 'trx-auto-dealer' ); ?>
	</h2>
	<div class="trx-ad-similar__grid">
		<?php foreach ( $similar_ids as $similar_id ) : ?>
			<?php
			TemplateLoader::getTemplatePart(
				'vehicle-card',
				array(
					'post_id' => (int) $similar_id,
				)
			);
			?>
		<?php endforeach; ?>
	</div>
</section>
