<?php

/**
 * Template pour l'archive des catégories de guides
 *
 * @package Laurent-Perrier
 */

get_header();

$current_term = get_queried_object();
// $image = get_field('image', $current_term);
?>

<div class="main-content v2 bg-white">
    taxonomy category guide
    <!-- Breadcrumb hiérarchique -->
    <?php lp_display_guide_category_breadcrumb(); ?>

    <!-- Archive Header -->
    <div class="relative flex items-center justify-center min-h-[400px] mb-12">
        <!-- <div class="layer">
            <?php if ($image && $image['ID']):
                echo wp_get_attachment_image($image['ID'], 'medium_large', false, array('class' => 'z-0 w-full h-full object-cover top-0 left-0 absolute'));
            endif; ?>
            <div class="absolute inset-0 bg-black opacity-50"></div>
        </div> -->
        <div class="relative text-center">
            <h1 class="font-heading text-[1.875rem] lg:text-[2.5rem] font-normal uppercase leading-snug text-white mb-4">
                <?php echo esc_html($current_term->name); ?>
            </h1>
            <?php if ($current_term->description): ?>
                <div class="text-lg text-gray-200 max-w-3xl mx-auto">
                    <?php echo wp_kses_post($current_term->description); ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <div class="container py-10 md:py-20">
        taxonomy category guide
        <!-- Guides de cette catégorie -->
        <?php
        $args = array(
            'post_type' => 'guide',
            'posts_per_page' => 12,
            'post_status' => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => 'guide_category',
                    'field'    => 'term_id',
                    'terms'    => $current_term->term_id,
                ),
            ),
        );
        $guides_query = new WP_Query($args);

        if ($guides_query->have_posts()): ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 md:gap-10">
                <?php while ($guides_query->have_posts()): $guides_query->the_post(); ?>
                    <article class="guide-card scroll-appear">
                        <a href="<?php the_permalink(); ?>" class="block group">

                            <!-- Featured Image -->
                            <div class="aspect-[4/3] overflow-hidden mb-4 relative">
                                <?php
                                $cover_image = get_field('guide_cover_image');
                                $reading_time = get_field('guide_reading_time');
                                $difficulty = get_field('guide_difficulty');

                                if ($cover_image): ?>
                                    <img src="<?php echo esc_url($cover_image['sizes']['medium_large']); ?>"
                                        alt="<?php echo esc_attr($cover_image['alt'] ?: get_the_title()); ?>"
                                        class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-105">
                                <?php elseif (has_post_thumbnail()): ?>
                                    <?php the_post_thumbnail('medium_large', [
                                        'class' => 'w-full h-full object-cover transition-transform duration-300 group-hover:scale-105',
                                        'alt' => get_the_title()
                                    ]); ?>
                                <?php else: ?>
                                    <div class="w-full h-full bg-gray-200 flex items-center justify-center">
                                        <svg class="w-16 h-16 text-gray-400" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M4 3a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V5a2 2 0 00-2-2H4zm12 12H4l4-8 3 6 2-4 3 6z" clip-rule="evenodd"></path>
                                        </svg>
                                    </div>
                                <?php endif; ?>

                                <!-- Guide Meta Overlay -->
                                <?php if ($reading_time || $difficulty): ?>
                                    <div class="absolute top-3 right-3 flex gap-2">
                                        <?php if ($reading_time): ?>
                                            <span class="bg-black bg-opacity-70 text-white text-xs px-2 py-1 rounded-full">
                                                <?php echo esc_html($reading_time); ?>min
                                            </span>
                                        <?php endif; ?>
                                        <?php if ($difficulty): ?>
                                            <?php
                                            $difficulty_colors = array(
                                                'beginner' => 'bg-green-500',
                                                'intermediate' => 'bg-yellow-500',
                                                'advanced' => 'bg-red-500'
                                            );
                                            $color_class = isset($difficulty_colors[$difficulty]) ? $difficulty_colors[$difficulty] : 'bg-gray-500';
                                            ?>
                                            <span class="<?php echo $color_class; ?> text-white text-xs px-2 py-1 rounded-full">
                                                <?php echo esc_html(ucfirst($difficulty)); ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </div>

                            <!-- Content -->
                            <div class="guide-content">
                                <h2 class="font-heading text-xl lg:text-2xl font-normal uppercase leading-snug mb-3 group-hover:text-gold transition-colors">
                                    <?php the_title(); ?>
                                </h2>

                                <?php
                                $guide_description = get_field('guide_description');
                                $excerpt_text = '';

                                if ($guide_description) {
                                    $excerpt_text = wp_trim_words($guide_description, 20, '...');
                                } elseif (has_excerpt()) {
                                    $excerpt_text = wp_trim_words(get_the_excerpt(), 20, '...');
                                }

                                if ($excerpt_text): ?>
                                    <p class="text-gray-600 leading-relaxed mb-4">
                                        <?php echo esc_html($excerpt_text); ?>
                                    </p>
                                <?php endif; ?>

                                <!-- Read More -->
                                <span class="inline-flex items-center text-sm font-medium text-gold group-hover:underline">
                                    <?php _e('Lire le guide', 'laurentperrier'); ?>
                                    <svg class="ml-2 w-4 h-4 transition-transform group-hover:translate-x-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
                                    </svg>
                                </span>
                            </div>
                        </a>
                    </article>
                <?php endwhile; ?>
            </div>

            <!-- Pagination -->
            <?php
            $pagination_links = paginate_links(array(
                'total' => $guides_query->max_num_pages,
                'current' => max(1, get_query_var('paged')),
                'format' => '?paged=%#%',
                'show_all' => false,
                'type' => 'array',
                'end_size' => 3,
                'mid_size' => 1,
                'prev_next' => true,
                'prev_text' => __('← Précédent', 'laurentperrier'),
                'next_text' => __('Suivant →', 'laurentperrier'),
                'add_args' => false,
                'add_fragment' => '',
            ));

            if ($pagination_links): ?>
                <div class="flex justify-center items-center space-x-2 mt-12">
                    <?php foreach ($pagination_links as $link): ?>
                        <div class="pagination-item">
                            <?php echo $link; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

        <?php else: ?>
            <!-- No Guides Found -->
            <div class="text-center py-16">
                <div class="max-w-md mx-auto">
                    <h3 class="text-lg font-medium text-gray-900 mb-2">
                        <?php _e('Aucun guide dans cette catégorie', 'laurentperrier'); ?>
                    </h3>
                    <p class="text-gray-500 mb-6">
                        <?php _e('Explorez d\'autres catégories ou revenez plus tard pour découvrir de nouveaux guides.', 'laurentperrier'); ?>
                    </p>
                    <a href="<?php echo get_post_type_archive_link('guide'); ?>" class="inline-flex items-center px-4 py-2 bg-gold text-white rounded-lg hover:bg-gold-dark transition-colors">
                        <?php _e('Voir tous les guides', 'laurentperrier'); ?>
                    </a>
                </div>
            </div>
        <?php endif; ?>

        <?php wp_reset_postdata(); ?>

        <!-- Sous-catégories -->
        <?php
        $child_terms = get_terms(array(
            'taxonomy' => 'guide_category',
            'parent' => $current_term->term_id,
            'hide_empty' => false,
        ));

        if (!empty($child_terms) && !is_wp_error($child_terms)): ?>
            <div class="mt-16 pt-16 border-t border-gray-200">
                <h3 class="font-heading text-2xl font-normal uppercase mb-8 text-center">
                    <?php _e('Sous-catégories', 'laurentperrier'); ?>
                </h3>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <?php foreach ($child_terms as $child_term): ?>
                        <div class="bg-gray-50 rounded-lg p-6 hover:bg-gray-100 transition-colors">
                            <a href="<?php echo get_term_link($child_term); ?>" class="block">
                                <h4 class="font-heading text-lg font-medium mb-2 text-gray-900 hover:text-gold transition-colors">
                                    <?php echo esc_html($child_term->name); ?>
                                </h4>
                                <?php if ($child_term->description): ?>
                                    <p class="text-gray-600 text-sm">
                                        <?php echo esc_html(wp_trim_words($child_term->description, 15, '...')); ?>
                                    </p>
                                <?php endif; ?>
                                <div class="mt-3 text-xs text-gray-500">
                                    <?php printf(_n('%d guide', '%d guides', $child_term->count, 'laurentperrier'), $child_term->count); ?>
                                </div>
                            </a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

    </div>
</div>

<?php get_footer(); ?>