<?php
/**
 * Render Events [Author Pro] Block
 */

$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : '';
if (empty($block_id)) {
    $block_id = 'author-pro-events-' . uniqid();
}

$anchor = isset($attributes['anchor']) ? $attributes['anchor'] : '';
$element_id = !empty($anchor) ? $anchor : $block_id;

$section_badge = isset($attributes['sectionBadge']) ? $attributes['sectionBadge'] : 'Tour Dates';
$section_title = isset($attributes['sectionTitle']) ? $attributes['sectionTitle'] : 'Upcoming Events & Talks';
$section_desc = isset($attributes['sectionDesc']) ? $attributes['sectionDesc'] : 'Join me in person for book signings, workshops, and keynote sessions.';
$events = isset($attributes['events']) ? $attributes['events'] : [];

$bg_color = isset($attributes['bgColor']) ? $attributes['bgColor'] : '';
$accent_color = isset($attributes['accentColor']) ? $attributes['accentColor'] : '';

$padding_top = isset($attributes['paddingTop']) ? $attributes['paddingTop'] : 96;
$padding_bottom = isset($attributes['paddingBottom']) ? $attributes['paddingBottom'] : 96;

// Generate Dynamic CSS
$custom_css = "";

if ($accent_color) {
    // Section Badge
    $custom_css .= "#{$element_id} .awt-category { color: {$accent_color} !important; }";
    
    // Day Number Highlight
    $custom_css .= "#{$element_id} .awt-date-text { color: #1e293b; }"; // Default dark
    $custom_css .= "#{$element_id} .group:hover .awt-date-text { color: {$accent_color} !important; }";

    // Card Hover Border
    $custom_css .= "#{$element_id} .awt-event-card:hover { border-color: {$accent_color} !important; }";
}

$style = "padding-top: {$padding_top}px; padding-bottom: {$padding_bottom}px;";
if ($bg_color) {
    $style .= " background-color: {$bg_color};";
}

$wrapper_attributes = get_block_wrapper_attributes([
    'class' => 'awt-author-pro-events py-24 bg-paper',
    'style' => $style,
    'id'    => $element_id
]);
?>

<?php if (!empty($custom_css)) : ?>
    <style><?php echo $custom_css; ?></style>
<?php endif; ?>

<section <?php echo $wrapper_attributes; ?>>
    <div class="max-w-5xl mx-auto px-6 lg:px-8">
        <div class="text-center mb-16" data-aos="fade-up">
            <span class="text-accent font-bold text-xs uppercase tracking-widest"><?php echo esc_html($section_badge); ?></span>
            <h2 class="text-4xl font-serif font-bold text-primary mt-2"><?php echo esc_html($section_title); ?></h2>
            <p class="text-secondary mt-4 max-w-2xl mx-auto"><?php echo esc_html($section_desc); ?></p>
        </div>

        <div class="space-y-6">
            <?php foreach ($events as $index => $event) : 
                $day = isset($event['day']) ? $event['day'] : '';
                $monthYear = isset($event['monthYear']) ? $event['monthYear'] : '';
                $category = isset($event['category']) ? $event['category'] : '';
                $title = isset($event['title']) ? $event['title'] : '';
                $location = isset($event['location']) ? $event['location'] : '';
                $time = isset($event['time']) ? $event['time'] : '';
                $btnText = isset($event['btnText']) ? $event['btnText'] : 'Get Ticket';
                $btnUrl = isset($event['btnUrl']) ? $event['btnUrl'] : '';
            ?>
                <div class="awt-event-card bg-white p-6 md:p-8 rounded-xl border border-gray-100 shadow-sm hover:shadow-lg transition duration-300 flex flex-col md:flex-row items-center gap-6 md:gap-12 group" data-aos="fade-up" data-aos-delay="<?php echo intval($index * 100); ?>">
                    
                    <div class="flex flex-col items-center justify-center min-w-[80px] text-center">
                        <span class="text-3xl font-serif font-bold text-primary transition awt-date-text group-hover:text-accent"><?php echo esc_html($day); ?></span>
                        <span class="text-xs font-bold text-gray-400 uppercase tracking-wider"><?php echo esc_html($monthYear); ?></span>
                    </div>

                    <div class="flex-1 text-center md:text-left border-t md:border-t-0 md:border-l border-gray-100 pt-4 md:pt-0 md:pl-10 w-full">
                        <span class="text-xs font-bold awt-category text-accent uppercase mb-1 block"><?php echo esc_html($category); ?></span>
                        <h3 class="text-xl font-bold text-primary mb-2"><?php echo esc_html($title); ?></h3>
                        <div class="flex flex-wrap justify-center md:justify-start gap-4 text-sm text-secondary">
                            <?php if ($location): ?>
                                <span class="flex items-center gap-2"><i class="fas fa-map-marker-alt text-gray-400"></i> <?php echo esc_html($location); ?></span>
                            <?php endif; ?>
                            <?php if ($time): ?>
                                <span class="flex items-center gap-2"><i class="far fa-clock text-gray-400"></i> <?php echo esc_html($time); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>

                    <?php if (!empty($btnUrl)) : ?>
                        <a href="<?php echo esc_url($btnUrl); ?>" class="px-6 py-3 border border-primary bg-white text-primary font-bold text-xs uppercase rounded hover:bg-primary hover:text-white transition whitespace-nowrap">
                            <?php echo esc_html($btnText); ?>
                        </a>
                    <?php else : ?>
                        <button class="px-6 py-3 border border-primary bg-white text-primary font-bold text-xs uppercase rounded hover:bg-primary hover:text-white transition whitespace-nowrap">
                            <?php echo esc_html($btnText); ?>
                        </button>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>
