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

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

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

$section_title = isset($attributes['sectionTitle']) ? $attributes['sectionTitle'] : 'Upcoming Workshops';
$section_subtitle = isset($attributes['sectionSubtitle']) ? $attributes['sectionSubtitle'] : 'Join me in person or online';
$activities = isset($attributes['activities']) ? $attributes['activities'] : [];

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

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

// Generate Dynamic CSS
$custom_css = "";

if ($accent_color) {
    $custom_css .= "#{$element_id} .awt-accent { color: {$accent_color} !important; }";
    
    // Icon Box Dynamic Coloring
    $custom_css .= "#{$element_id} .awt-icon-box { color: {$accent_color} !important; }";
    $custom_css .= "#{$element_id} .group:hover .awt-icon-box { background-color: {$accent_color} !important; color: #ffffff !important; }";
    $custom_css .= "#{$element_id} .group:hover { border-color: {$accent_color} !important; }";
}

if ($card_bg_color) {
    $custom_css .= "#{$element_id} .awt-card-bg { background-color: {$card_bg_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-activities bg-white',
    '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="awt-container">
        
        <div class="text-center mb-16" data-aos="fade-up">
            <h2 class="text-4xl font-serif font-bold text-primary mb-4"><?php echo esc_html($section_title); ?></h2>
            <p class="text-lg text-secondary max-w-2xl mx-auto"><?php echo esc_html($section_subtitle); ?></p>
        </div>

        <?php if (!empty($activities)) : ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 text-center">
                <?php foreach ($activities as $index => $activity) : 
                    $title = isset($activity['title']) ? $activity['title'] : '';
                    $location = isset($activity['location']) ? $activity['location'] : ''; // Reused as Description
                    $icon_url = isset($activity['iconUrl']) ? $activity['iconUrl'] : '';
                    $svg_content = isset($activity['svgContent']) ? $activity['svgContent'] : '';
                    
                    $link = isset($activity['link']) ? $activity['link'] : '#';
                    $link_text = isset($activity['linkText']) ? $activity['linkText'] : '';
                ?>
                    <div class="awt-card-bg p-8 border border-gray-100 rounded-2xl hover:border-accent hover:shadow-lg transition duration-300 group flex flex-col items-center" data-aos="fade-up" data-aos-delay="<?php echo intval($index * 100); ?>">
                        
                        <!-- Icon Area -->
                        <div class="awt-icon-box w-16 h-16 mx-auto bg-orange-50 text-accent rounded-full flex items-center justify-center text-2xl mb-6 group-hover:bg-accent group-hover:text-white transition duration-300">
                             <?php if ( ! empty( $icon_url ) ) : ?>
                                <img src="<?php echo esc_url( $icon_url ); ?>" alt="" class="w-8 h-8 object-contain filter group-hover:brightness-0 group-hover:invert transition duration-300">
                            <?php elseif ( ! empty( $svg_content ) ) : ?>
                                <!-- Render raw SVG -->
                                <div class="w-8 h-10 flex items-center justify-center filter group-hover:brightness-0 group-hover:invert transition duration-300">
                                    <?php echo $svg_content; // Escaping not needed for static trusted SVGs in attributes, or use wp_kses safely if user content ?>
                                </div>
                            <?php else : ?>
                                <!-- Fallback default generic icon if nothing is set -->
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-8 h-8 fill-current filter group-hover:brightness-0 group-hover:invert transition duration-300" viewBox="0 0 24 24">
                                    <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/>
                                </svg>
                            <?php endif; ?>
                        </div>

                        <!-- Title (Conditional Link) -->
                        <h3 class="text-xl font-bold text-primary mb-3">
                            <?php if (empty($link_text) && !empty($link) && $link !== '#') : ?>
                                <a href="<?php echo esc_url($link); ?>" class="hover:text-accent transition"><?php echo esc_html($title); ?></a>
                            <?php else : ?>
                                <?php echo esc_html($title); ?>
                            <?php endif; ?>
                        </h3>

                        <p class="text-sm text-secondary leading-relaxed mb-6"><?php echo esc_html($location); ?></p>
                        
                        <!-- Read More Link -->
                        <?php if ( ! empty( $link_text ) ) : ?>
                            <a href="<?php echo esc_url($link); ?>" class="mt-auto inline-flex items-center gap-2 text-sm font-bold uppercase tracking-wider awt-accent group-hover:opacity-80 transition">
                                <?php echo esc_html($link_text); ?>
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 transition-transform group-hover:translate-x-1" viewBox="0 0 20 20" fill="currentColor">
                                    <path fill-rule="evenodd" d="M12.293 5.293a1 1 0 011.414 0l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-2.293-2.293a1 1 0 010-1.414z" clip-rule="evenodd" />
                                </svg>
                            </a>
                        <?php endif; ?>

                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>
