<?php
/**
 * Thumbnail Field Template
 * 
 * This template displays the thumbnail for a property.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/archive/fields/thumbnail.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/archive/fields/thumbnail.php
 * 3. Modify the copied file to customize the thumbnail display
 * 
 * @package     Havenlytics
 * @subpackage  Templates/archive/Fields
 * @since       2.0.0
 */

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

// Prefix all variables with hvnly_ to avoid global namespace conflicts
$hvnly_property_id = $property_id ?? get_the_ID();
$hvnly_is_featured = get_post_meta($hvnly_property_id, '_hvnly_property_action_tool_is_featured', true);
$hvnly_actual_price = get_post_meta($hvnly_property_id, '_hvnly_property_price', true);

// ✅ PROFESSIONAL APPROACH: Use the dedicated get_helper() method
$hvnly_helper = HVNLY_NAB()->get_helper();

// PROPERLY Get gallery images from the meta field - EXACT ORIGINAL LOGIC
$hvnly_gallery_images_meta = get_post_meta($hvnly_property_id, '_hvnly_property_gallery_images', true);
$hvnly_gallery_image_ids = array();

if (!empty($hvnly_gallery_images_meta)) {
    if (is_array($hvnly_gallery_images_meta) && !empty($hvnly_gallery_images_meta[0])) {
        $hvnly_gallery_image_ids = explode(',', $hvnly_gallery_images_meta[0]);
    } elseif (is_string($hvnly_gallery_images_meta) && strpos($hvnly_gallery_images_meta, ',') !== false) {
        $hvnly_gallery_image_ids = explode(',', $hvnly_gallery_images_meta);
    } elseif (is_numeric($hvnly_gallery_images_meta)) {
        $hvnly_gallery_image_ids = array($hvnly_gallery_images_meta);
    }
}

// Clean up the IDs
$hvnly_gallery_image_ids = array_filter(array_map('trim', $hvnly_gallery_image_ids));
$hvnly_gallery_image_ids = array_map('absint', $hvnly_gallery_image_ids);
$hvnly_gallery_image_ids = array_filter($hvnly_gallery_image_ids);

// ✅ USE HELPER CLASS: Get optimized featured image for grid (400x300)
$hvnly_featured_image = $hvnly_helper->get_property_image_url($hvnly_property_id, 'grid');

// Apply gallery order
$hvnly_property_id_int = absint($hvnly_property_id);
$hvnly_gallery_order = ($hvnly_property_id_int % 2 == 0) ? 'ASC' : 'DESC';
if (!empty($hvnly_gallery_image_ids) && $hvnly_gallery_order === 'DESC') {
    $hvnly_gallery_image_ids = array_reverse($hvnly_gallery_image_ids);
}
?>

<div class="hvnly-default-thumbnail">
    <div class="hvnly-property-grid-list-thumb">
        <div class="hvnly-property-gallery-main">
            <div class="hvnly-property-carousel-gallery"
                id="hvnly-property-carousel-gallery-<?php echo esc_attr($hvnly_property_id); ?>"
                aria-label="<?php esc_attr_e('Property image gallery', 'havenlytics'); ?>">
                <div class="hvnly-property-carousel-track">
                    <?php
                    $hvnly_has_images = false;
                    $hvnly_display_images = array();

                    // First, try to use gallery images
                    if (!empty($hvnly_gallery_image_ids)) {
                        $hvnly_has_images = true;
                        $hvnly_image_count = 0;
                        foreach ($hvnly_gallery_image_ids as $hvnly_image_id) {
                            if ($hvnly_image_count >= 12) break;

                            // ✅ USE HELPER CLASS: Get optimized gallery image for grid
                            $hvnly_image_url = wp_get_attachment_image_url($hvnly_image_id, 'hvnly_property_grid');
                            
                            if ($hvnly_image_url) {
                                $hvnly_display_images[] = array(
                                    'type' => 'gallery',
                                    'url' => esc_url($hvnly_image_url),
                                    'id' => absint($hvnly_image_id)
                                );
                                $hvnly_image_count++;
                            }
                        }
                    }

                    // If no gallery images, use featured image
                    if (!$hvnly_has_images && $hvnly_featured_image) {
                        $hvnly_has_images = true;
                        $hvnly_display_images[] = array(
                            'type' => 'featured',
                            'url' => esc_url($hvnly_featured_image),
                            'id' => 'featured'
                        );
                    }

                    // If still no images, use placeholder
                    if (!$hvnly_has_images) {
                        // ✅ USE HELPER CLASS: Get proper placeholder for grid
                        $hvnly_placeholder_url = $hvnly_helper->get_property_placeholder_url('grid');
                        
                        $hvnly_display_images[] = array(
                            'type' => 'placeholder',
                            'url' => esc_url($hvnly_placeholder_url),
                            'id' => 'placeholder'
                        );
                    }

                    // Display the slides
                    foreach ($hvnly_display_images as $hvnly_index => $hvnly_image_data):
                        $hvnly_slide_index = absint($hvnly_index);
                    ?>
                    <div class="hvnly-property-carousel-slide"
                        style="background-image: url('<?php echo esc_url($hvnly_image_data['url']); ?>');"
                        data-image-id="<?php echo esc_attr($hvnly_image_data['id']); ?>"
                        data-slide-index="<?php echo esc_attr($hvnly_slide_index); ?>" role="img"
                        aria-label="<?php 
                        echo esc_attr(sprintf(
                            /* translators: %1$d: Current image number, %2$d: Total number of images */
                            __('Image %1$d of %2$d', 'havenlytics'), $hvnly_slide_index + 1, count($hvnly_display_images))); ?>">
                    </div>
                    <?php endforeach; ?>
                </div>

                <?php if (count($hvnly_display_images) > 1): ?>
                <div class="hvnly-property-carousel-nav">
                    <button class="hvnly-property-carousel-nav-btn hvnly-property-carousel-prev-btn"
                        aria-label="<?php esc_attr_e('Previous image', 'havenlytics'); ?>">
                        <i class="fas fa-chevron-left" aria-hidden="true"></i>
                    </button>
                    <button class="hvnly-property-carousel-nav-btn hvnly-property-carousel-next-btn"
                        aria-label="<?php esc_attr_e('Next image', 'havenlytics'); ?>">
                        <i class="fas fa-chevron-right" aria-hidden="true"></i>
                    </button>
                </div>
                <div class="hvnly-property-carousel-indicators"
                    aria-label="<?php esc_attr_e('Gallery navigation indicators', 'havenlytics'); ?>">
                    <?php for ($hvnly_i = 0; $hvnly_i < count($hvnly_display_images); $hvnly_i++): ?>
                    <div class="hvnly-property-carousel-indicator <?php echo $hvnly_i === 0 ? 'hvnly-property-carousel-active' : ''; ?>"
                        data-indicator-index="<?php echo esc_attr(absint($hvnly_i)); ?>" role="button" tabindex="0"
                        aria-label="<?php 
                        echo esc_attr(sprintf(
                            /* translators: %d: Image number to navigate to */
                            __('Go to image %d', 'havenlytics'), $hvnly_i + 1)); ?>">
                    </div>
                    <?php endfor; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <div class="hvnly-property--grid-list--overlay">
            <div class="hvnly-property-grid-list-top-overlay">
                <?php if ($hvnly_is_featured): ?>
                <span class="hvnly-property-grid-list-tag"><?php esc_html_e('Featured', 'havenlytics'); ?></span>
                <?php endif; ?>
                <button class="hvnly-property--grid-list--favorite"
                    data-property-id="<?php echo esc_attr($hvnly_property_id); ?>"
                    aria-label="<?php esc_attr_e('Add to favorites', 'havenlytics'); ?>">
                    <i class="far fa-heart" aria-hidden="true"></i>
                </button>
            </div>
            <div class="hvnly-property--grid-list--bottom-overlay">
                <div class="hvnly-property-grid-list-price">
                    <?php if ($hvnly_actual_price): ?>
                    <?php echo wp_kses_post(hvnly_format_price($hvnly_actual_price)); ?>
                    <?php else: ?>
                    <?php esc_html_e('Price on Request', 'havenlytics'); ?>
                    <?php endif; ?>
                </div>
                <button class="hvnly-property-grid-list-share"
                    aria-label="<?php esc_attr_e('Share this property', 'havenlytics'); ?>">
                    <i class="fas fa-share-alt" aria-hidden="true"></i>
                </button>
            </div>
        </div>
    </div>
</div>