<?php
/**
 * Gallery Card Template
 * 
 * This template displays the gallery card for single property pages.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/single-property/gallery-card.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/single-property/gallery-card.php
 * 3. Modify the copied file to customize the gallery card
 * 
 * @package     Havenlytics
 * @subpackage  Templates/single-property/
 * @since       2.0.0
 */

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

// Prefix all variables with hvnly_ to avoid global namespace conflicts
$hvnly_property_id = $args['property_id'] ?? get_the_ID();
$hvnly_group_fields = $args['fields'] ?? array();
$hvnly_values = $args['values'] ?? array();
$hvnly_group_base_id = $args['group_base_id'] ?? '';
$hvnly_title_key = $args['title_key'] ?? '';
$hvnly_images_key = $args['images_key'] ?? '';

// Get title - try multiple sources
$hvnly_title = '';

// 1. Try from values array
if ( ! empty( $hvnly_values['title'] ) ) {
    $hvnly_title = $hvnly_values['title'];
}
// 2. Try from specific title key
elseif ( ! empty( $hvnly_title_key ) ) {
    $hvnly_title = get_post_meta( $hvnly_property_id, $hvnly_title_key, true );
}
// 3. Try any field that might be a title
else {
    foreach ( $hvnly_group_fields as $hvnly_field ) {
        $hvnly_meta_key = $hvnly_field['name'] ?? $hvnly_field['id'] ?? '';
        if ( strpos( $hvnly_meta_key, '_title' ) !== false || ( $hvnly_field['metaKey'] ?? '' ) === 'title' ) {
            $hvnly_title = get_post_meta( $hvnly_property_id, $hvnly_meta_key, true );
            if ( ! empty( $hvnly_title ) ) {
                break;
            }
        }
    }
}

// Get images - try multiple sources
$hvnly_images = '';

// 1. Try from values array
if ( ! empty( $hvnly_values['images'] ) ) {
    $hvnly_images = $hvnly_values['images'];
}
// 2. Try from specific images key
elseif ( ! empty( $hvnly_images_key ) ) {
    $hvnly_images = get_post_meta( $hvnly_property_id, $hvnly_images_key, true );
}
// 3. Try any field that might be images
else {
    foreach ( $hvnly_group_fields as $hvnly_field ) {
        $hvnly_meta_key = $hvnly_field['name'] ?? $hvnly_field['id'] ?? '';
        if ( strpos( $hvnly_meta_key, '_images' ) !== false || ( $hvnly_field['metaKey'] ?? '' ) === 'images' ) {
            $hvnly_images = get_post_meta( $hvnly_property_id, $hvnly_meta_key, true );
            if ( ! empty( $hvnly_images ) ) {
                break;
            }
        }
    }
}

// Backward compatibility
if ( empty( $hvnly_images ) ) {
    $hvnly_images = get_post_meta( $hvnly_property_id, '_hvnly_gallery_images', true );
}
if ( empty( $hvnly_title ) ) {
    $hvnly_title = get_post_meta( $hvnly_property_id, '_hvnly_gallery_title', true );
}

// Process images
if ( is_string( $hvnly_images ) ) {
    $hvnly_images = explode( ',', $hvnly_images );
}

$hvnly_images = array_filter( array_map( 'absint', (array) $hvnly_images ) );

// DON'T show gallery if no images
if ( empty( $hvnly_images ) ) {
    return;
}

// Generate a unique gallery ID
$hvnly_gallery_id = 'hvnly-gallery-' . $hvnly_property_id . '-' . ($hvnly_group_base_id ?: uniqid());
$hvnly_has_gallery = ! empty( $hvnly_images ) && is_array( $hvnly_images );
?>
<!-- Property Gallery Field Card -->
<div class="hvnly-property-single__gallery-field" id="<?php echo esc_attr( $hvnly_gallery_id ); ?>"
    data-gallery-id="<?php echo esc_attr( $hvnly_gallery_id ); ?>"
    data-property-id="<?php echo esc_attr( $hvnly_property_id ); ?>"
    data-field-name="<?php echo esc_attr( $hvnly_images_key ?: 'gallery' ); ?>">

    <?php if ( ! empty( $hvnly_title ) ) : ?>
    <div class="hvnly-property-single__gallery-title"><?php echo esc_html( $hvnly_title ); ?></div>
    <?php endif; ?>

    <?php if ( $hvnly_has_gallery ) : ?>
    <div class="hvnly-property-single__gallery-grid">
        <?php 
            $hvnly_image_count = 0;
            foreach ( $hvnly_images as $hvnly_image_id ) : 
                if ( empty( $hvnly_image_id ) ) continue;
                
                $hvnly_full_image_url = wp_get_attachment_image_url( $hvnly_image_id, 'full' );
                $hvnly_medium_image_url = wp_get_attachment_image_url( $hvnly_image_id, 'medium' );
                $hvnly_image_title = get_the_title( $hvnly_image_id );
                $hvnly_image_alt = get_post_meta( $hvnly_image_id, '_wp_attachment_image_alt', true ) ?: $hvnly_image_title;
                
                if ( $hvnly_full_image_url ) : 
                    $hvnly_image_count++;
                    if ( $hvnly_image_count <= 50 ) : // Show first 50 images
            ?>
        <div class="hvnly-property-single__gallery-item" data-gallery-id="<?php echo esc_attr( $hvnly_gallery_id ); ?>"
            data-image-index="<?php echo esc_attr( $hvnly_image_count - 1 ); ?>"
            data-full-image="<?php echo esc_url( $hvnly_full_image_url ); ?>"
            data-image-title="<?php echo esc_attr( $hvnly_image_title ); ?>"
            data-image-alt="<?php echo esc_attr( $hvnly_image_alt ); ?>">

            <?php 
                    $hvnly_aria_label = sprintf(
                        /* translators: 1: image number, 2: image title */
                        __( 'View image %1$d: %2$s', 'havenlytics' ),
                        $hvnly_image_count,
                        $hvnly_image_title
                    );
                    ?>

            <a href="<?php echo esc_url( $hvnly_full_image_url ); ?>"
                class="hvnly-property-single__gallery-link hvnly-gallery-popup-trigger"
                data-gallery-id="<?php echo esc_attr( $hvnly_gallery_id ); ?>"
                data-image-index="<?php echo esc_attr( $hvnly_image_count - 1 ); ?>"
                aria-label="<?php echo esc_attr( $hvnly_aria_label ); ?>">

                <img src="<?php echo esc_url( $hvnly_medium_image_url ); ?>"
                    alt="<?php echo esc_attr( $hvnly_image_alt ); ?>" class="hvnly-property-single__gallery-image"
                    loading="lazy">

                <?php if ( $hvnly_image_count === 6 && count( $hvnly_images ) > 6 ) : ?>
                <div class="hvnly-property-single__gallery-more">
                    <?php 
                                /* translators: %d: number of remaining images in the gallery */
                                printf(
                                    '+%1$d %2$s',
                                    count( $hvnly_images ) - 6,
                                    esc_html__( 'more', 'havenlytics' )
                                ); 
                                ?>
                </div>
                <?php endif; ?>

            </a>
        </div>
        <?php 
                    endif;
                endif;
            endforeach; 
            ?>
    </div>

    <div class="hvnly-property-single__gallery-info">
        <p>
            <?php 
                $hvnly_image_total = count( $hvnly_images );
                $hvnly_gallery_text = sprintf(
                    /* translators: %d: number of images in the gallery */
                    _n( '%d image', '%d images', $hvnly_image_total, 'havenlytics' ), 
                    $hvnly_image_total
                );
                echo esc_html( $hvnly_gallery_text );
                ?>
        </p>
    </div>
    <?php else : ?>
    <div class="hvnly-property-single__no-gallery">
        <svg class="hvnly-icon hvnly-icon-lg">
            <use xlink:href="#hvnly-images"></use>
        </svg>
        <p><?php esc_html_e( 'No gallery images available', 'havenlytics' ); ?></p>
    </div>
    <?php endif; ?>
</div>