<?php
/**
 * search.php – Página de resultados de busca
 */
get_header(); ?>

<main class="site-main">

    <!-- CABEÇALHO DE BUSCA BONITO -->
    <header class="hot-header-search">
        <h1 class="category-title-main">
            🔍 Buscar Resultados
        </h1>
        <?php if (get_search_query()) : ?>
            <p class="search-query-text">
                Buscaring for: "<span class="search-query-highlight"><?php echo esc_html(get_search_query()); ?></span>"
            </p>
            <p class="search-results-count">
                <?php 
                global $wp_query;
                $total = $wp_query->found_posts;
                if ($total > 0) {
                    echo sprintf(
                        _n('%d group found', '%d groups found', $total, 'textdomain'),
                        $total
                    );
                } else {
                    echo 'No se encontraron grupos with this term.';
                }
                ?>
            </p>
        <?php else : ?>
            <p class="search-no-query">
                Enter a term to search for groups
            </p>
        <?php endif; ?>
    </header>

    <!-- BARRA DE BUSCA NOVAMENTE (CASO O USUÁRIO QUEIRA BUSCAR OUTRO TERMO) -->
    <div class="hot-search-bar hot-search-bar-secondary">
        <form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
            <input type="search" name="s" placeholder="Buscar NSFW TELEGRAM PORN..." value="<?php echo esc_attr(get_search_query()); ?>" required>
            <button type="submit">BUSCAR</button>
        </form>
    </div>

    <div class="hot-grid">

        <?php 
        $first = true;
        if (have_posts()) :
            while (have_posts()) : 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;
                        if ($cat) :
                            ?>
                            <a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>" class="categoria">
                                <?php echo esc_html($cat->name); ?>
                            </a>
                        <?php else : ?>
                            <span class="categoria">NSFW TELEGRAM PORN</span>
                        <?php endif; ?>

                        <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;
        else : ?>
            <div class="search-empty-state">
                <div class="search-empty-state-icon">🔍</div>
                <h2>
                    No se encontraron resultados
                </h2>
                <p>
                    No groups match "<strong><?php echo esc_html(get_search_query()); ?></strong>".
                </p>
                <div class="search-empty-state-actions">
                    <a href="<?php echo esc_url(home_url('/')); ?>" class="search-empty-state-link">
                        Volver to homepage
                    </a>
                </div>
                <p class="search-empty-state-hint">
                    Try other search terms
                </p>
            </div>
        <?php endif; ?>

    </div>

    <!-- PAGINAÇÃO CUSTOMIZADA -->
    <?php if ($GLOBALS['wp_query']->max_num_pages > 1) : ?>
        <div class="hot-pagination">
            <?php
            $current = max(1, get_query_var('paged'));
            // Verifica se a função existe antes de chamar (prevenção de erro fatal)
            if (function_exists('hot_custom_pagination')) {
                echo hot_custom_pagination($GLOBALS['wp_query']->max_num_pages, $current);
            } else {
                // Fallback para paginação padrão do WordPress
                echo '<div class="hot-pagination-fallback">';
                echo paginate_links(array(
                    'total' => $GLOBALS['wp_query']->max_num_pages,
                    'current' => $current,
                    'prev_text' => '« Anterior',
                    'next_text' => 'Siguiente »'
                ));
                echo '</div>';
            }
            ?>
        </div>
    <?php endif; ?>

    <!-- CSS movido para functions.php (hot_get_centralized_css) -->

</main>

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

