<?php
/**
 * Event Card Component
 * Used for displaying individual event cards in lists and AJAX load more
 * MUST match the exact structure from event-list-template.php
 */

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

// Ensure we have both event post object and event ID
if (!isset($event_post) && isset($event_id)) {
    $event_post = get_post($event_id);
} elseif (isset($event_post) && !isset($event_id)) {
    $event_id = $event_post->ID;
}

// Validate we have required data
if (!isset($event_post) || !$event_post) {
    return; // Exit if no valid event post
}

// Get event data - using same variable names as main template
$event_title = get_the_title($event_post);
$event_description = get_the_excerpt($event_post);

// Get featured image - with fallback to event banner (em_upload_image)
$featured_image = get_the_post_thumbnail_url($event_post, 'medium');

// Fallback to event banner image from settings
if (!$featured_image) {
    // Get event settings if not already loaded
    if (!isset($event_settings)) {
        $event_settings = get_post_meta($event_id, 'awl_em_settings_' . $event_id, true);
        if (!is_array($event_settings)) $event_settings = array();
    }
    
    // Use em_upload_image (event banner) as fallback
    if (isset($event_settings['em_upload_image']) && !empty($event_settings['em_upload_image'])) {
        $featured_image = $event_settings['em_upload_image'];
    }
}

// Get event settings - ensure they're loaded
if (!isset($event_settings)) {
    $event_settings = get_post_meta($event_id, 'awl_em_settings_' . $event_id, true);
    if (!is_array($event_settings)) $event_settings = array();
}

// Get event settings - using same variable names as main template
$start_date = isset($event_settings['em_start_date']) ? $event_settings['em_start_date'] : '';
$location = isset($event_settings['em_event_location']) ? $event_settings['em_event_location'] : '';

// Get category from WordPress taxonomy first, then fallback to meta field
$category = '';
$wp_categories = get_the_terms($event_id, 'awplife-event-categories');
if (!is_wp_error($wp_categories) && !empty($wp_categories)) {
    $category = $wp_categories[0]->name; // Use first category
} elseif (isset($event_settings['em_event_category']) && !empty($event_settings['em_event_category'])) {
    $category = $event_settings['em_event_category']; // Fallback to meta field
}

// Format date - using same format as main template
$formatted_date = '';
if ($start_date) {
    $formatted_date = date_i18n('M j, Y', strtotime($start_date));
}

// Determine view URL - using same logic as main template
if ($atts['view_mode'] === 'modal') {
    $view_url = '#';
    $view_class = 'em-view-modal';
    $view_data = 'data-event-id="' . esc_attr($event_id) . '"';
} else {
    // Get the actual current page URL (not the event's permalink)
    // This is important for backward compatibility when [EM] shortcode is used on various pages
    global $wp;
    $current_url = home_url(add_query_arg(array(), $wp->request));
    
    // Fallback: try using the queried object if available
    if (empty($current_url) || $current_url === home_url()) {
        $queried_object = get_queried_object();
        if ($queried_object && isset($queried_object->ID)) {
            $current_url = get_permalink($queried_object->ID);
        }
    }
    
    // Final fallback: use the current request URI
    if (empty($current_url) || $current_url === home_url()) {
        $current_url = home_url($_SERVER['REQUEST_URI']);
    }
    
    // Remove any existing em_single parameter from current URL
    $current_url = remove_query_arg('em_single', $current_url);
    $current_url = remove_query_arg('em_back', $current_url);
    
    $view_url = add_query_arg(array(
        'em_single' => $event_id,
        'em_back' => urlencode($current_url)
    ), $current_url);
    $view_class = '';
    $view_data = '';
}

// Get custom colors from event settings
$em_primary_color = isset($event_settings['em_primary_color']) ? $event_settings['em_primary_color'] : '';
$em_secondary_color = isset($event_settings['em_secondary_color']) ? $event_settings['em_secondary_color'] : '';
$em_text_color = isset($event_settings['em_text_color']) ? $event_settings['em_text_color'] : '';
?>

<?php if ($em_primary_color || $em_secondary_color || $em_text_color): ?>
<style>
    /* Custom colors for event card <?php echo esc_attr($event_id); ?> */
    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-event-button {
        <?php if ($em_primary_color): ?>
        background-color: <?php echo esc_attr($em_primary_color); ?> !important;
        border-color: <?php echo esc_attr($em_primary_color); ?> !important;
        <?php endif; ?>
    }

    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-event-button:hover {
        <?php if ($em_primary_color): ?>
        background-color: <?php echo esc_attr($em_primary_color); ?>DD !important;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px <?php echo esc_attr($em_primary_color); ?>40;
        <?php endif; ?>
    }

    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-event-meta-icon {
        <?php if ($em_primary_color): ?>
        color: <?php echo esc_attr($em_primary_color); ?> !important;
        <?php endif; ?>
    }

    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-category-badge {
        <?php if ($em_primary_color): ?>
        background-color: <?php echo esc_attr($em_primary_color); ?>20 !important;
        color: <?php echo esc_attr($em_primary_color); ?> !important;
        <?php endif; ?>
    }

    <?php if ($em_text_color): ?>
    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-event-title,
    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-event-description {
        color: <?php echo esc_attr($em_text_color); ?> !important;
    }
    <?php endif; ?>

    <?php if ($em_secondary_color): ?>
    .em-event-card[data-event-id="<?php echo esc_attr($event_id); ?>"] .em-event-meta-item span {
        color: <?php echo esc_attr($em_secondary_color); ?> !important;
    }
    <?php endif; ?>
</style>
<?php endif; ?>

<div class="em-event-card"
     data-event-id="<?php echo esc_attr($event_id); ?>"
     data-category="<?php echo esc_attr(strtolower($category)); ?>"
     data-start-date="<?php echo esc_attr($start_date); ?>"
     data-title="<?php echo esc_attr(strtolower($event_title)); ?>">

    <img class="em-event-image" src="<?php echo esc_url($featured_image); ?>" alt="<?php echo esc_attr($event_title); ?>">

    <div class="em-event-content">
        <?php if ($category): ?>
        <div class="em-category-badge">
            <?php echo esc_html($category); ?>
        </div>
        <?php endif; ?>

        <h2 class="em-event-title"><?php echo esc_html($event_title); ?></h2>

        <div class="em-event-meta">
            <?php if ($formatted_date): ?>
            <div class="em-event-meta-item">
                <svg class="em-event-meta-icon" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
                </svg>
                <span><?php echo esc_html($formatted_date); ?></span>
            </div>
            <?php endif; ?>

            <?php if ($location): ?>
            <div class="em-event-meta-item">
                <svg class="em-event-meta-icon" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a2 2 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0zM15 11a3 3 0 11-6 0 3 3 0 016 0z"></path>
                </svg>
                <span><?php echo esc_html($location); ?></span>
            </div>
            <?php endif; ?>
        </div>

        <?php if ($event_description): ?>
        <div class="em-event-description">
            <?php echo esc_html($event_description); ?>
        </div>
        <?php endif; ?>

        <a href="<?php echo esc_url($view_url); ?>"
           class="em-event-button <?php echo esc_attr($view_class); ?>"
           <?php echo $view_data; ?>>
            <?php _e('View Details', 'event-monster'); ?>
        </a>
    </div>
</div>

<?php
// REMOVED: Registration component inclusion per event card
// This was causing duplicate registration forms and potential conflicts
// Registration functionality is handled globally in the main list template
?>
