<?php
/**
 * Stats Template
 * 
 * This template displays the statistics for single property pages.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/single-property/stats.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/single-property/stats.php
 * 3. Modify the copied file to customize the statistics display
 * 
 * @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 = get_the_ID();
$hvnly_views_data = get_post_meta($hvnly_property_id, '_hvnly_property_views', true);
$hvnly_views_analytics = get_post_meta($hvnly_property_id, '_hvnly_property_view_analytics', true);

// Parse views data
$hvnly_total_views = 0;
$hvnly_unique_views = 0;
$hvnly_today_views = 0;
$hvnly_this_week_views = 0;

if ($hvnly_views_data) {
    if (is_array($hvnly_views_data)) {
        $hvnly_total_views = isset($hvnly_views_data['total']) ? intval($hvnly_views_data['total']) : 0;
        $hvnly_unique_views = isset($hvnly_views_data['unique']) ? intval($hvnly_views_data['unique']) : 0;
        $hvnly_today_views = isset($hvnly_views_data['today']) ? intval($hvnly_views_data['today']) : 0;
        $hvnly_this_week_views = isset($hvnly_views_data['this_week']) ? intval($hvnly_views_data['this_week']) : 0;
    } elseif (is_string($hvnly_views_data) && strpos($hvnly_views_data, 'a:') === 0) {
        $hvnly_parsed_data = maybe_unserialize($hvnly_views_data);
        if (is_array($hvnly_parsed_data)) {
            $hvnly_total_views = isset($hvnly_parsed_data['total']) ? intval($hvnly_parsed_data['total']) : 0;
            $hvnly_unique_views = isset($hvnly_parsed_data['unique']) ? intval($hvnly_parsed_data['unique']) : 0;
            $hvnly_today_views = isset($hvnly_parsed_data['today']) ? intval($hvnly_parsed_data['today']) : 0;
            $hvnly_this_week_views = isset($hvnly_parsed_data['this_week']) ? intval($hvnly_parsed_data['this_week']) : 0;
        }
    }
}

// Calculate days listed (simplified - using post date)
$hvnly_post_date = get_the_date('Y-m-d H:i:s', $hvnly_property_id);
$hvnly_days_listed = round((time() - strtotime($hvnly_post_date)) / (60 * 60 * 24));

// Get saves count (placeholder - you might want to track this separately)
$hvnly_saves_count = 0;

// Get property age
$hvnly_property_age = '';
$hvnly_year_built = get_post_meta($hvnly_property_id, '_hvnly_property_year_built', true);
if ($hvnly_year_built && $hvnly_year_built != '0') {
    $hvnly_current_year = gmdate('Y');
    $hvnly_property_age = $hvnly_current_year - intval($hvnly_year_built);
}
?>
<!-- Property Stats -->
<div class="hvnly-property-single__stats">
    <?php if ($hvnly_total_views > 0) : ?>
        <div class="hvnly-property-single__stat-item">
            <svg class="hvnly-icon hvnly-icon-thin" style="margin-bottom: var(--hvnly-space-xs);"><use xlink:href="#hvnly-eye"></use></svg>
            <span class="hvnly-property-single__stat-value"><?php //echo esc_html(hvnly_format_large_number($hvnly_total_views)); ?></span>
            <span class="hvnly-property-single__stat-label"><?php esc_html_e('Views', 'havenlytics'); ?></span>
        </div>
    <?php endif; ?>
    
    <?php if ($hvnly_saves_count > 0) : ?>
        <div class="hvnly-property-single__stat-item">
            <svg class="hvnly-icon hvnly-icon-thin" style="margin-bottom: var(--hvnly-space-xs);"><use xlink:href="#hvnly-heart"></use></svg>
            <span class="hvnly-property-single__stat-value"><?php echo esc_html($hvnly_saves_count); ?></span>
            <span class="hvnly-property-single__stat-label"><?php esc_html_e('Saves', 'havenlytics'); ?></span>
        </div>
    <?php endif; ?>
    
    <?php if ($hvnly_days_listed > 0) : ?>
        <div class="hvnly-property-single__stat-item">
            <svg class="hvnly-icon hvnly-icon-thin" style="margin-bottom: var(--hvnly-space-xs);"><use xlink:href="#hvnly-calendar"></use></svg>
            <span class="hvnly-property-single__stat-value"><?php echo esc_html($hvnly_days_listed); ?></span>
            <span class="hvnly-property-single__stat-label"><?php esc_html_e('Days Listed', 'havenlytics'); ?></span>
        </div>
    <?php endif; ?>
    
    <?php if ($hvnly_today_views > 0) : ?>
        <div class="hvnly-property-single__stat-item">
            <svg class="hvnly-icon hvnly-icon-thin" style="margin-bottom: var(--hvnly-space-xs);"><use xlink:href="#hvnly-chart"></use></svg>
            <span class="hvnly-property-single__stat-value"><?php echo esc_html($hvnly_today_views); ?></span>
            <span class="hvnly-property-single__stat-label"><?php esc_html_e('Today Views', 'havenlytics'); ?></span>
        </div>
    <?php endif; ?>
    
    <?php if ($hvnly_unique_views > 0) : ?>
        <div class="hvnly-property-single__stat-item">
            <svg class="hvnly-icon hvnly-icon-thin" style="margin-bottom: var(--hvnly-space-xs);"><use xlink:href="#hvnly-user"></use></svg>
            <span class="hvnly-property-single__stat-value"><?php //echo esc_html(hvnly_format_large_number($hvnly_unique_views)); ?></span>
            <span class="hvnly-property-single__stat-label"><?php esc_html_e('Unique Views', 'havenlytics'); ?></span>
        </div>
    <?php endif; ?>
    
    <?php if ($hvnly_property_age) : ?>
        <div class="hvnly-property-single__stat-item">
            <svg class="hvnly-icon hvnly-icon-thin" style="margin-bottom: var(--hvnly-space-xs);"><use xlink:href="#hvnly-home"></use></svg>
            <span class="hvnly-property-single__stat-value"><?php echo esc_html($hvnly_property_age); ?> yrs</span>
            <span class="hvnly-property-single__stat-label"><?php esc_html_e('Property Age', 'havenlytics'); ?></span>
        </div>
    <?php endif; ?>
</div>

<?php 
// Helper function to format large numbers
if (!function_exists('hvnly_format_large_number')) {
    function hvnly_format_large_number($number) {
        if ($number >= 1000000) {
            return round($number / 1000000, 1) . 'M';
        } elseif ($number >= 1000) {
            return round($number / 1000, 1) . 'K';
        }
        return $number;
    }
}
?>