<?php
/**
 * Template part: vehicle-gallery
 *
 * Renders a static photo grid for a vehicle. Used by Elementor/Gutenberg
 * embeds and as a fallback render path when the Swiper-based media slider
 * isn't loaded. Each thumbnail wraps a link to the full-resolution image
 * with a `data-trx-ad-lightbox` attribute the frontend.js can pick up to
 * open an overlay viewer.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 *
 * @var int  $post_id   Vehicle post ID.
 * @var int  $columns   Optional column count (default 4).
 * @var bool $show_main Optional — render the first image at hero size (default true).
 */

declare(strict_types=1);

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

$post_id   = isset( $post_id ) ? (int) $post_id : (int) get_the_ID();
$columns   = isset( $columns ) ? max( 1, min( 6, (int) $columns ) ) : 4;
$show_main = isset( $show_main ) ? (bool) $show_main : true;

$gallery_raw = get_post_meta( $post_id, TRX_AD_PREFIX . 'gallery', true );
$gallery_ids = array();
if ( is_string( $gallery_raw ) && '' !== $gallery_raw ) {
	$decoded = json_decode( $gallery_raw, true );
	if ( is_array( $decoded ) ) {
		$gallery_ids = array_values( array_filter( array_map( 'intval', $decoded ) ) );
	}
}

// Always lead with the post thumbnail when we have one and the gallery
// hasn't already promoted it.
$thumb_id = (int) get_post_thumbnail_id( $post_id );
if ( $thumb_id > 0 && ! in_array( $thumb_id, $gallery_ids, true ) ) {
	array_unshift( $gallery_ids, $thumb_id );
}

if ( empty( $gallery_ids ) ) {
	return;
}

$main_id   = (int) array_shift( $gallery_ids );
$main_full = wp_get_attachment_image_url( $main_id, 'full' );
// Pull the 'large' src array so we can emit explicit width/height — a missing
// intrinsic size is a Lighthouse CLS + best-practices ding (UAT #50).
$main_src = wp_get_attachment_image_src( $main_id, 'large' );
$main_lg  = is_array( $main_src ) ? (string) $main_src[0] : '';
$main_w   = is_array( $main_src ) ? (int) $main_src[1] : 0;
$main_h   = is_array( $main_src ) ? (int) $main_src[2] : 0;
?>
<div class="trx-ad-gallery" data-trx-ad-gallery>

	<?php if ( $show_main && $main_full ) : ?>
		<a href="<?php echo esc_url( $main_full ); ?>"
			class="trx-ad-gallery__main"
			data-trx-ad-lightbox
			aria-label="<?php esc_attr_e( 'Open full-size image', 'trx-auto-dealer' ); ?>">
			<img src="<?php echo esc_url( $main_lg ?: $main_full ); ?>"
				alt="<?php echo esc_attr( get_the_title( $post_id ) ); ?>"
				<?php if ( $main_w > 0 && $main_h > 0 ) : ?>
				width="<?php echo esc_attr( (string) $main_w ); ?>" height="<?php echo esc_attr( (string) $main_h ); ?>"
				<?php endif; ?>
				decoding="async"
				loading="lazy" />
		</a>
	<?php endif; ?>

	<?php if ( ! empty( $gallery_ids ) ) : ?>
		<ul class="trx-ad-gallery__grid"
			style="--trx-ad-gallery-cols: <?php echo (int) $columns; ?>;"
			role="list">
			<?php foreach ( $gallery_ids as $img_id ) : ?>
				<?php
				$thumb_src = wp_get_attachment_image_src( (int) $img_id, 'medium' );
				$thumb_url = is_array( $thumb_src ) ? (string) $thumb_src[0] : '';
				$thumb_w   = is_array( $thumb_src ) ? (int) $thumb_src[1] : 0;
				$thumb_h   = is_array( $thumb_src ) ? (int) $thumb_src[2] : 0;
				$full_url  = wp_get_attachment_image_url( (int) $img_id, 'full' );
				if ( ! $thumb_url || ! $full_url ) {
					continue;
				}
				$alt = (string) get_post_meta( (int) $img_id, '_wp_attachment_image_alt', true );
				if ( '' === $alt ) {
					$alt = get_the_title( $post_id );
				}
				?>
				<li class="trx-ad-gallery__item">
					<a href="<?php echo esc_url( $full_url ); ?>"
						class="trx-ad-gallery__thumb"
						data-trx-ad-lightbox>
						<img src="<?php echo esc_url( $thumb_url ); ?>"
							alt="<?php echo esc_attr( $alt ); ?>"
							<?php if ( $thumb_w > 0 && $thumb_h > 0 ) : ?>
							width="<?php echo esc_attr( (string) $thumb_w ); ?>" height="<?php echo esc_attr( (string) $thumb_h ); ?>"
							<?php endif; ?>
							decoding="async"
							loading="lazy" />
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
	<?php endif; ?>

</div>
