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

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

// Get property ID
$hvnly_property_id = $args['property_id'] ?? get_the_ID();
$hvnly_fields = $args['fields'] ?? array();
$hvnly_values = $args['values'] ?? array();

// Initialize variables
$hvnly_address = '';
$hvnly_latitude = '';
$hvnly_longitude = '';

// ============================================================
// METHOD 1: Check stored reference keys (PRIMARY - MOST RELIABLE)
// These tell us exactly which dynamic field names to use
// ============================================================
$hvnly_active_lat_key = get_post_meta($hvnly_property_id, '_hvnly_active_map_latitude_key', true);
$hvnly_active_lng_key = get_post_meta($hvnly_property_id, '_hvnly_active_map_longitude_key', true);
$hvnly_active_address_key = get_post_meta($hvnly_property_id, '_hvnly_active_map_address_key', true);

if (!empty($hvnly_active_lat_key) && !empty($hvnly_active_lng_key)) {
    $hvnly_latitude = get_post_meta($hvnly_property_id, $hvnly_active_lat_key, true);
    $hvnly_longitude = get_post_meta($hvnly_property_id, $hvnly_active_lng_key, true);
    $hvnly_address = get_post_meta($hvnly_property_id, $hvnly_active_address_key, true);
}

// ============================================================
// METHOD 2: Try to get from existing values (passed from renderer)
// ============================================================
if (empty($hvnly_latitude) && !empty($hvnly_values['latitude'])) {
    $hvnly_latitude = $hvnly_values['latitude'];
}
if (empty($hvnly_longitude) && !empty($hvnly_values['longitude'])) {
    $hvnly_longitude = $hvnly_values['longitude'];
}
if (empty($hvnly_address) && !empty($hvnly_values['address'])) {
    $hvnly_address = $hvnly_values['address'];
}

// ============================================================
// METHOD 3: Search ALL meta keys for map_*_latitude/longitude pattern
// This finds dynamic fields even without stored references
// ============================================================
if (empty($hvnly_latitude) || empty($hvnly_longitude)) {
    $hvnly_all_meta = get_post_meta($hvnly_property_id);
    
    foreach ($hvnly_all_meta as $hvnly_meta_key => $hvnly_meta_value) {
        if (preg_match('/^map_.+_latitude$/i', $hvnly_meta_key)) {
            $hvnly_lat_value = is_array($hvnly_meta_value) ? ($hvnly_meta_value[0] ?? '') : $hvnly_meta_value;
            
            if (!empty($hvnly_lat_value) && is_numeric($hvnly_lat_value)) {
                $hvnly_base_key = preg_replace('/_latitude$/i', '', $hvnly_meta_key);
                $hvnly_lng_key = $hvnly_base_key . '_longitude';
                $hvnly_address_key = $hvnly_base_key . '_address';
                
                $hvnly_lng_value = get_post_meta($hvnly_property_id, $hvnly_lng_key, true);
                $hvnly_address_value = get_post_meta($hvnly_property_id, $hvnly_address_key, true);
                
                if (!empty($hvnly_lng_value) && is_numeric($hvnly_lng_value)) {
                    $hvnly_latitude = (float) $hvnly_lat_value;
                    $hvnly_longitude = (float) $hvnly_lng_value;
                    $hvnly_address = $hvnly_address_value;
                    
                    // Cache the found base for future use
                    if (!empty($hvnly_base_key)) {
                        update_post_meta($hvnly_property_id, '_hvnly_active_map_field_base', $hvnly_base_key);
                        update_post_meta($hvnly_property_id, '_hvnly_active_map_address_key', $hvnly_base_key . '_address');
                        update_post_meta($hvnly_property_id, '_hvnly_active_map_latitude_key', $hvnly_base_key . '_latitude');
                        update_post_meta($hvnly_property_id, '_hvnly_active_map_longitude_key', $hvnly_base_key . '_longitude');
                    }
                    break;
                }
            }
        }
    }
}

// No legacy keys - only dynamic fields!

$hvnly_has_location = ! empty( $hvnly_latitude ) && ! empty( $hvnly_longitude ) && is_numeric( $hvnly_latitude ) && is_numeric( $hvnly_longitude );
$hvnly_map_id = 'hvnly-map-' . $hvnly_property_id . '-' . uniqid();
?>
<div class="hvnly-property-single__location-card">
    <div class="hvnly-property-single__location-content">
        <?php if ( ! empty( $hvnly_address ) ) : ?>
        <div class="hvnly-property-single__address">
            <strong><?php esc_html_e( 'Address:', 'havenlytics' ); ?></strong>
            <span><?php echo esc_html( $hvnly_address ); ?></span>
        </div>
        <?php endif; ?>

        <div class="hvnly-property-single__map-container">
            <?php if ( $hvnly_has_location ) : ?>
            <div class="hvnly-property-single__map" id="<?php echo esc_attr( $hvnly_map_id ); ?>"
                data-lat="<?php echo esc_attr( $hvnly_latitude ); ?>"
                data-lng="<?php echo esc_attr( $hvnly_longitude ); ?>"
                data-address="<?php echo esc_attr( $hvnly_address ); ?>"
                data-title="<?php echo esc_attr( get_the_title( $hvnly_property_id ) ); ?>"
                style="height: 400px; width: 100%; background: #f5f5f5;">
                <!-- Map will be initialized by JavaScript -->
            </div>
            <?php else : ?>
            <div class="hvnly-property-single__map-placeholder" style="
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    height: 400px;
                    background: #f5f5f5;
                    border-radius: 8px;
                    padding: 20px;
                    text-align: center;
                    color: #666;
                ">
                <svg width="48" height="48" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"
                    style="margin-bottom: 15px; opacity: 0.5;">
                    <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7z" stroke="#999"
                        stroke-width="1.5" fill="none" />
                    <circle cx="12" cy="9" r="2.5" stroke="#999" stroke-width="1.5" fill="none" />
                </svg>
                <p><?php esc_html_e( 'Map location not available for this property.', 'havenlytics' ); ?></p>
            </div>
            <?php endif; ?>
        </div>

        <?php if ( $hvnly_has_location ) : ?>
        <div class="hvnly-property-single__location-actions" style="margin-top: 15px; display: flex; gap: 10px;">
            <a href="https://www.google.com/maps/search/?api=1&query=<?php echo urlencode( $hvnly_latitude . ',' . $hvnly_longitude ); ?>"
                target="_blank" class="hvnly-button hvnly-button--small"
                style="display: inline-block; padding: 8px 16px;  border-radius: 4px; text-decoration: none;  font-size: 14px;">
                <?php esc_html_e( 'Google Maps', 'havenlytics' ); ?>
            </a>
            <a href="https://maps.apple.com/?q=<?php echo urlencode( $hvnly_latitude . ',' . $hvnly_longitude ); ?>"
                target="_blank" class="hvnly-button hvnly-button--small"
                style="display: inline-block; padding: 8px 16px; border-radius: 4px; text-decoration: none;  font-size: 14px;">
                <?php esc_html_e( 'Apple Maps', 'havenlytics' ); ?>
            </a>
        </div>
        <?php endif; ?>
    </div>
</div>