<?php
/**
 * Shortcode: [web_services]
 * Displays pages with category "web" as stylish cards
 * 
 * Usage: [web_services posts_per_page="6" columns="3" show_excerpt="yes"]
 */

function kpy_web_services_shortcode($atts) {
    $atts = shortcode_atts(array(
        'posts_per_page' => 9,
        'columns' => 3,
        'show_excerpt' => 'yes',
        'orderby' => 'date',
        'order' => 'DESC',
    ), $atts);

    $args = array(
        'post_type'      => 'page',
        'posts_per_page' => intval($atts['posts_per_page']),
        'orderby'        => sanitize_text_field($atts['orderby']),
        'order'          => sanitize_text_field($atts['order']),
        'tax_query'      => array(
            array(
                'taxonomy' => 'page_category',
                'field'    => 'slug',
                'terms'    => 'web',
            ),
        ),
    );

    $query = new WP_Query($args);
    $columns = intval($atts['columns']);
    $grid_class = 'kpy-web-grid kpy-web-cols-' . $columns;
    
    ob_start();
    
    if ($query->have_posts()) : ?>
        <div class="kpy-web-services-wrapper" 
             data-aos="fade-up" 
             data-aos-duration="800" 
             data-aos-offset="100">
            <div class="<?php echo esc_attr($grid_class); ?>">
                <?php 
                $card_index = 0;
                while ($query->have_posts()) : $query->the_post(); 
                    $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
                    $bg_image = $featured_image ? $featured_image[0] : get_template_directory_uri() . '/assets/images/default-service.jpg';
                    $icon_class = get_web_service_icon(get_the_ID());
                    
                    // Calculate staggered delays
                    $delay = 0.1 + ($card_index * 0.08);
                    $icon_delay = $delay + 0.1;
                    $title_delay = $delay + 0.15;
                    $excerpt_delay = $delay + 0.2;
                    $button_delay = $delay + 0.25;
                ?>
                
                <div class="kpy-web-card" 
                     data-aos="fade-up" 
                     data-aos-duration="700" 
                     data-aos-delay="<?php echo $delay * 1000; ?>"
                     data-aos-offset="50">
                    <div class="kpy-web-card-inner" style="background-image: linear-gradient(135deg, rgba(0,0,0,0.7) 0%, rgba(129,20,17,0.85) 100%), url('<?php echo esc_url($bg_image); ?>');">
                        <div class="kpy-web-card-content">
                            <div class="kpy-web-icon" 
                                 data-aos="zoom-in" 
                                 data-aos-duration="500" 
                                 data-aos-delay="<?php echo $icon_delay * 1000; ?>">
                                <i class="fas <?php echo esc_attr($icon_class); ?>"></i>
                            </div>
                            <h3 class="kpy-web-title" 
                                data-aos="fade-up" 
                                data-aos-duration="500" 
                                data-aos-delay="<?php echo $title_delay * 1000; ?>">
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>
                            <?php if ($atts['show_excerpt'] === 'yes') : ?>
                                <div class="kpy-web-excerpt" 
                                     data-aos="fade-up" 
                                     data-aos-duration="600" 
                                     data-aos-delay="<?php echo $excerpt_delay * 1000; ?>">
                                    <?php echo wp_trim_words(get_the_excerpt() ? get_the_excerpt() : get_the_content(), 15, '...'); ?>
                                </div>
                            <?php endif; ?>
                            <div class="kpy-web-link" 
                                 data-aos="fade-up" 
                                 data-aos-duration="400" 
                                 data-aos-delay="<?php echo $button_delay * 1000; ?>">
                                <a href="<?php the_permalink(); ?>" class="kpy-web-btn">
                                    <span>Learn More</span>
                                    <i class="fas fa-arrow-right"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                
                <?php 
                $card_index++;
                endwhile; ?>
            </div>
        </div>
    <?php else : ?>
        <div class="kpy-web-no-results" data-aos="fade-up" data-aos-duration="600">
            <p><?php _e('No web service pages found.', 'kpy-theme'); ?></p>
        </div>
    <?php endif;
    
    wp_reset_postdata();
    
    return ob_get_clean();
}
add_shortcode('web_services', 'kpy_web_services_shortcode');

// Helper function to get icons for web services
function get_web_service_icon($post_id) {
    $title = strtolower(get_the_title($post_id));
    
    // Comprehensive icon mapping based on keywords
    $icon_map = array(
        // Photography & Video
        'photography' => 'fa-camera',
        'video' => 'fa-video',
        'production' => 'fa-film',
        'corporate photography' => 'fa-camera-retro',
        
        // Enterprise & Productivity
        'enterprise' => 'fa-building',
        'productivity' => 'fa-chart-line',
        'microsoft' => 'fa-windows',
        'workflow' => 'fa-tasks',
        
        // Website & Development
        'website' => 'fa-laptop-code',
        'web' => 'fa-globe',
        'development' => 'fa-code',
        'performance' => 'fa-tachometer-alt',
        'maintenance' => 'fa-tools',
        'care' => 'fa-heartbeat',
        
        // Brand & Design
        'brand' => 'fa-trademark',
        'identity' => 'fa-id-card',
        'design' => 'fa-palette',
        'corporate design' => 'fa-draw-polygon',
        
        // Marketing & Social
        'marketing' => 'fa-chart-simple',
        'digital marketing' => 'fa-bullhorn',
        'content' => 'fa-pen-fancy',
        'strategy' => 'fa-chart-line',
        'social media' => 'fa-share-alt',
        'social media management' => 'fa-hashtag',
        
        // Advertising
        'advertising' => 'fa-ad',
        'paid' => 'fa-dollar-sign',
        'campaign' => 'fa-chart-line',
        
        // Email & Collaboration
        'email' => 'fa-envelope',
        'collaboration' => 'fa-users',
        'business email' => 'fa-mail-bulk',
        
        // Cloud & Tech
        'cloud' => 'fa-cloud',
        'google' => 'fa-google',
        'solutions' => 'fa-cogs',
        
        // E-commerce
        'ecommerce' => 'fa-shopping-cart',
        'shop' => 'fa-store',
        
        // Security
        'security' => 'fa-shield-alt',
        'backup' => 'fa-database',
        
        // Hosting
        'hosting' => 'fa-server',
    );
    
    // Check each keyword and return matching icon
    foreach ($icon_map as $keyword => $icon) {
        if (strpos($title, $keyword) !== false) {
            return $icon;
        }
    }
    
    // Default icon based on first word or fallback
    $first_word = explode(' ', $title)[0];
    $default_icons = array(
        'corporate' => 'fa-building',
        'enterprise' => 'fa-chart-line',
        'website' => 'fa-laptop-code',
        'brand' => 'fa-trademark',
        'digital' => 'fa-bullhorn',
        'social' => 'fa-share-alt',
        'paid' => 'fa-ad',
        'business' => 'fa-envelope',
        'web' => 'fa-globe',
    );
    
    if (isset($default_icons[$first_word])) {
        return $default_icons[$first_word];
    }
    
    return 'fa-globe';
}
?>