<?php
/**
 * Video Card Template
 * 
 * This template displays the video card for single property pages.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/single-property/video-card.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/single-property/video-card.php
 * 3. Modify the copied file to customize the video 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_fields = $args['fields'] ?? array();
$hvnly_values = $args['values'] ?? array();
$hvnly_group_base_id = $args['group_base_id'] ?? '';
$hvnly_url_key = $args['url_key'] ?? '';
$hvnly_title_key = $args['title_key'] ?? '';
$hvnly_thumbnail_key = $args['thumbnail_key'] ?? '';

// Get values - FIRST try from the values array
$hvnly_title = $hvnly_values['title'] ?? $hvnly_values['video_title'] ?? '';
$hvnly_url = $hvnly_values['url'] ?? $hvnly_values['video_url'] ?? '';
$hvnly_thumbnail = $hvnly_values['thumbnail'] ?? $hvnly_values['video_thumbnail'] ?? '';

// If not found, try direct meta using the keys
if (empty($hvnly_title) && !empty($hvnly_title_key)) {
    $hvnly_title = get_post_meta($hvnly_property_id, $hvnly_title_key, true);
}
if (empty($hvnly_url) && !empty($hvnly_url_key)) {
    $hvnly_url = get_post_meta($hvnly_property_id, $hvnly_url_key, true);
}
if (empty($hvnly_thumbnail) && !empty($hvnly_thumbnail_key)) {
    $hvnly_thumbnail = get_post_meta($hvnly_property_id, $hvnly_thumbnail_key, true);
}

// BACKWARD COMPATIBILITY: Only as last resort, try legacy keys
if (empty($hvnly_url)) {
    $hvnly_url = get_post_meta($hvnly_property_id, '_hvnly_video_url', true);
}
if (empty($hvnly_title)) {
    $hvnly_title = get_post_meta($hvnly_property_id, '_hvnly_video_title', true);
}
if (empty($hvnly_thumbnail)) {
    $hvnly_thumbnail = get_post_meta($hvnly_property_id, '_hvnly_video_thumbnail', true);
}

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

// Determine video type and extract ID
$hvnly_video_source = '';
$hvnly_video_id = '';
$hvnly_is_valid_video = false;

// Check if it's a YouTube URL
if (strpos($hvnly_url, 'youtube.com') !== false || strpos($hvnly_url, 'youtu.be') !== false) {
    $hvnly_video_source = 'youtube';
    $hvnly_is_valid_video = true;
    
    // Extract YouTube video ID
    if (preg_match('/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/', $hvnly_url, $hvnly_matches)) {
        $hvnly_video_id = $hvnly_matches[1];
    }
}
// Check if it's a Vimeo URL
elseif (strpos($hvnly_url, 'vimeo.com') !== false) {
    $hvnly_video_source = 'vimeo';
    $hvnly_is_valid_video = true;
    
    // Extract Vimeo video ID
    if (preg_match('/(?:vimeo\.com\/)([0-9]+)/', $hvnly_url, $hvnly_matches)) {
        $hvnly_video_id = $hvnly_matches[1];
    }
}

// If not a valid video URL, don't render the video card
if (!$hvnly_is_valid_video) {
    return;
}

// If no thumbnail, use a placeholder
if (empty($hvnly_thumbnail)) {
    $hvnly_thumbnail = HVNLYNAB_ASSETS_URL . 'images/video-placeholder.jpg';
}

// Generate a unique ID for each video card
$hvnly_video_card_id = 'hvnly-video-' . uniqid() . '-' . $hvnly_property_id;
if (!empty($hvnly_group_base_id)) {
    $hvnly_video_card_id = 'hvnly-video-' . $hvnly_group_base_id;
}
?>
<!-- Property Video Card with Thumbnail Overlay -->

<?php if ( ! empty( $label ) ) : ?>
<div class="hvnly-property-single__default-title"><?php echo esc_html( $label ); ?></div>
<?php endif; ?>
<div class="hvnly-property-single__video-card" id="<?php echo esc_attr($hvnly_video_card_id); ?>"
    data-video-source="<?php echo esc_attr($hvnly_video_source); ?>"
    data-video-url="<?php echo esc_attr($hvnly_url); ?>" data-video-id="<?php echo esc_attr($hvnly_video_id); ?>"
    data-video-title="<?php echo esc_attr($hvnly_title); ?>"
    data-group-base-id="<?php echo esc_attr($hvnly_group_base_id); ?>">

    <div class="hvnly-property-single__video-container">
        <?php if ($hvnly_thumbnail) : ?>
        <img src="<?php echo esc_url($hvnly_thumbnail); ?>"
            alt="<?php echo esc_attr($hvnly_title ?: __('Property Video', 'havenlytics')); ?>"
            class="hvnly-property-single__video-placeholder" loading="lazy">
        <?php endif; ?>

        <div class="hvnly-property-single__video-overlay">
            <div class="hvnly-property-single__video-play-btn">
                <svg class="hvnly-icon hvnly-icon-thin">
                    <use xlink:href="#hvnly-play"></use>
                </svg>
            </div>

            <?php if ($hvnly_title) : ?>
            <h3 class="hvnly-property-single__video-title"><?php echo esc_html($hvnly_title); ?></h3>
            <?php else : ?>
            <h3 class="hvnly-property-single__video-title"><?php esc_html_e('Virtual Tour', 'havenlytics'); ?></h3>
            <?php endif; ?>

            <p class="hvnly-property-single__video-description">
                <?php esc_html_e('Take a guided tour of this stunning property, showcasing every detail from the elegant interiors to the magnificent outdoor spaces.', 'havenlytics'); ?>
            </p>


        </div>
    </div>
</div>