<?php
/**
 * Event schedule component
 */
if (!defined('ABSPATH')) exit;

// Get schedule settings - force enable if component is being rendered
$em_schedule_heading = $event_settings['em_schedule_heading'] ?? 'Event Schedule';
$em_schedule_items = $event_settings['em_schedule_items'] ?? array();

// Only show if has schedule data
if (empty($em_schedule_items)) {
    return;
}

// Filter out empty items
$em_schedule_items = array_filter($em_schedule_items, function($item) {
    return !empty($item['title']);
});

// If no valid items after filtering, don't show the component
if (empty($em_schedule_items)) {
    return;
}

// Get event dates to determine if multi-day
$em_start_date = $event_settings['em_start_date'] ?? '';
$em_end_date = $event_settings['em_end_date'] ?? '';
$is_multi_day = !empty($em_start_date) && !empty($em_end_date) && ($em_start_date !== $em_end_date);

// Get date format
$em_date_format = get_option('date_format');
$em_time_format = get_option('time_format');

// Check if magazine layout
$layout_preference = $event_settings['em_layout_preference'] ?? 'default';
$rounded_class = ($layout_preference === 'magazine') ? 'em-rounded-2xl' : '';
?>
<div class="em-bg-white em-p-5 em-border em-border-slate-200 em-shadow-sm em-h-full em-flex em-flex-col <?php echo esc_attr($rounded_class); ?>">
    <h2 class="em-text-lg em-font-bold em-text-slate-900 em-mb-4"><?php echo esc_html($em_schedule_heading); ?></h2>
    <div class="em-relative em-border-l-2 em-border-slate-200 em-space-y-6 em-pl-6 em-text-sm">
        <?php
        $session_count = 0;

        foreach ($em_schedule_items as $index => $item) {
            $time = $item['time'] ?? '';
            $title = $item['title'] ?? '';
            $description = $item['description'] ?? '';
            $speaker = $item['speaker'] ?? '';
            $location = $item['location'] ?? '';
            $date = $item['date'] ?? ''; // For future use when date field is added

            if (empty($title)) continue;

            // Format time if provided
            $formatted_time = '';
            if (!empty($time)) {
                $formatted_time = date_i18n($em_time_format, strtotime($time));
            }

            $session_count++;

            // Show day label only for multi-day events
            $day_label = '';
            if ($is_multi_day) {
                $day_label = sprintf(__('Day %d', 'event-monster'), $session_count);
            }
            ?>
            <div class="em-relative">
                <div class="em-absolute em--left-[33px] em-top-1 em-w-4 em-h-4 em-bg-indigo-500 em-rounded-full em-border-4 em-border-white"></div>
                <?php if ($is_multi_day && $day_label): ?>
                    <p class="em-font-semibold em-text-indigo-500">
                        <?php echo esc_html($day_label); ?><?php if ($formatted_time): ?>: <?php echo esc_html($formatted_time); ?><?php endif; ?>
                    </p>
                <?php elseif ($formatted_time): ?>
                    <p class="em-font-semibold em-text-indigo-500"><?php echo esc_html($formatted_time); ?></p>
                <?php endif; ?>

                <h4 class="em-font-semibold em-text-slate-800"><?php echo esc_html($title); ?></h4>

                <?php if (!empty($description)): ?>
                    <p class="em-text-slate-600 em-text-xs em-mt-1"><?php echo esc_html($description); ?></p>
                <?php endif; ?>

                <?php if (!empty($speaker) || !empty($location)): ?>
                    <div class="em-flex em-gap-3 em-mt-2 em-text-xs em-text-slate-500">
                        <?php if (!empty($speaker)): ?>
                            <span>👤 <?php echo esc_html($speaker); ?></span>
                        <?php endif; ?>
                        <?php if (!empty($location)): ?>
                            <span>📍 <?php echo esc_html($location); ?></span>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
            <?php
        }
        ?>
    </div>
</div>
