<?php get_header(); ?>

<main class="site-main">

    <?php 
    // DESTAQUES PATROCINADOS - PHP DIRETO (mais rápido que JS)
    if (function_exists('hot_exibir_destaques')) {
        echo hot_exibir_destaques();
    }
    ?>

    <div class="hot-grid">

        <?php 
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        
        // Query CORRIGIDA - remove otimizações que quebram a contagem
        $query = new WP_Query([
            'posts_per_page' => 10, 
            'paged' => $paged, 
            'post_status' => 'publish',
            'post_type' => 'post',
            // IMPORTANTE: não desabilitar caches para contagem correta
            'update_post_term_cache' => true,  // Mudado de false para true
            'update_post_meta_cache' => true,   // Mudado de false para true
            'no_found_rows' => false,           // Mudado de false para garantir contagem
        ]);

        $first = true;
        if ($query->have_posts()) :
            while ($query->have_posts()) : $query->the_post(); ?>

                <article class="hot-card">
                    <?php if (has_post_thumbnail()) : ?>
                        <a href="<?php the_permalink(); ?>" aria-label="<?php echo esc_attr(get_the_title()); ?>">
                            <?php 
                            if ($first) {
                                // PRIMEIRA IMAGEM = LCP → sem lazy + fetchpriority high
                                echo wp_get_attachment_image(get_post_thumbnail_id(), 'hot_grid', false, [
                                    'class' => 'wp-post-image',
                                    'alt' => 'Telegram Group: ' . get_the_title() . ' - Click to join',
                                    'fetchpriority' => 'high',
                                    'decoding' => 'async',
                                    'sizes' => '(max-width: 480px) 336px, (max-width: 768px) 420px, 420px',
                                    'loading' => 'eager',
                                    'width' => '420',
                                    'height' => '236'
                                ]);
                                $first = false;
                            } else {
                                // Demais imagens = lazy normal
                                echo wp_get_attachment_image(get_post_thumbnail_id(), 'hot_grid', false, [
                                    'class' => 'wp-post-image',
                                    'alt' => 'Telegram Group: ' . get_the_title() . ' - Click to join',
                                    'loading' => 'lazy',
                                    'decoding' => 'async',
                                    'sizes' => '(max-width: 480px) 336px, (max-width: 768px) 420px, 420px',
                                    'width' => '420',
                                    'height' => '236'
                                ]);
                            }
                            ?>
                        </a>
                    <?php endif; ?>

                    <div class="hot-card-content">
                        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

                        <?php
                        $cats = get_the_category();
                        $cat = !empty($cats) ? $cats[0] : false;
                        echo $cat ? '<a href="'.esc_url(get_category_link($cat->term_id)).'" class="categoria">'.esc_html($cat->name).'</a>' 
                                  : '<a href="/" class="categoria">NSFW TELEGRAM PORN</a>';
                        ?>

                        <p><?php echo wp_trim_words(get_the_excerpt(), 28, '...'); ?></p>

                        <a href="<?php the_permalink(); ?>" class="btn-entrar">UNIRSE AL GRUPO</a>
                    </div>
                </article>

            <?php endwhile;
        endif;
        ?>

    </div>

    <?php if ($query->max_num_pages > 1) : ?>
        <div class="hot-pagination">
            <?php
            $current = max(1, get_query_var('paged'));
            
            // Usa função customizada se existir
            if (function_exists('hot_custom_pagination')) {
                echo hot_custom_pagination($query->max_num_pages, $current);
            } else {
                // Fallback para paginação padrão do WordPress
                echo paginate_links(array(
                    'total' => $query->max_num_pages,
                    'current' => $current,
                    'prev_text' => '« Anterior',
                    'next_text' => 'Siguiente »',
                    'type' => 'list',
                ));
            }
            ?>
        </div>
    <?php endif; ?>

</main>

<?php
wp_reset_postdata();
get_footer();
?>
