<?php
/**
 * Template part – Trending Posts Section
 *
 * Layout: Numbered List (free) | Card Grid / Scroll Strip (Pro)
 *
 * @package Blog_Over
 * @since   1.1.0
 */

// ── Guard: enabled? ──────────────────────────────────────────────────────────
if ( ! get_theme_mod( 'blog_over_trending_enable', false ) ) {
    return;
}

// ── Guard: page visibility ───────────────────────────────────────────────────
$show_on    = get_theme_mod( 'blog_over_trending_show_on', array( 'home', 'blog' ) );
$show_on    = is_array( $show_on ) ? $show_on : array();
$should_show = false;

if ( in_array( 'home',    $show_on ) && is_front_page() )                                              $should_show = true;
if ( in_array( 'blog',    $show_on ) && is_home() && ! is_front_page() )                               $should_show = true;
if ( in_array( 'archive', $show_on ) && ( is_archive() || is_category() || is_tag() || is_date() ) )   $should_show = true;
if ( in_array( 'single',  $show_on ) && is_singular( 'post' ) )                                        $should_show = true;
if ( in_array( 'page',    $show_on ) && is_page() )                                                    $should_show = true;

if ( ! $should_show ) {
    return;
}

// ── Gather settings ──────────────────────────────────────────────────────────
$section_title  = get_theme_mod( 'blog_over_trending_title',       __( 'Trending Now',  'blog-over' ) );
$badge_text     = get_theme_mod( 'blog_over_trending_badge_text',  '🔥 Hot' );
$layout         = get_theme_mod( 'blog_over_trending_layout',      'numbered' );
$sort_by        = get_theme_mod( 'blog_over_trending_sort_by',     'comments' );
$period         = get_theme_mod( 'blog_over_trending_period',      'all_time' );
$post_count     = absint( get_theme_mod( 'blog_over_trending_count', 5 ) );
$category       = get_theme_mod( 'blog_over_trending_category',    'all' );
$show_rank      = (bool) get_theme_mod( 'blog_over_trending_show_rank',  true );
$show_thumb     = (bool) get_theme_mod( 'blog_over_trending_show_thumb', true );
$elements       = get_theme_mod( 'blog_over_trending_elements', array( 'category', 'title', 'meta' ) );
$section_width  = get_theme_mod( 'blog_over_trending_section_width', 'boxed' );
$columns        = sanitize_key( get_theme_mod( 'blog_over_trending_columns', '3' ) );

$elements = is_array( $elements ) ? $elements : array( 'category', 'title', 'meta' );

// ── Build WP_Query ───────────────────────────────────────────────────────────
$query_args = array(
    'post_type'           => 'post',
    'posts_per_page'      => $post_count,
    'ignore_sticky_posts' => true,
    'orderby'             => 'comment_count', // default: most commented
    'order'               => 'DESC',
);

// Category filter
if ( $category !== 'all' && ! empty( $category ) ) {
    $query_args['category_name'] = sanitize_title( $category );
}

// Allow Pro plugin to modify query args (sort method, period, etc.)
$query_args = apply_filters( 'blog_over_trending_query_args', $query_args, $sort_by, $period );

$trending_query = new WP_Query( $query_args );


// ── Container class ──────────────────────────────────────────────────────────
$container_class = ( $section_width === 'full-width' )
    ? 'blog-over-container blog-over-container-wide'
    : 'blog-over-container';

$layout_class = 'blog-over-trending-layout-' . esc_attr( $layout );
?>

<div class="blog-over-trending-wrapper <?php echo esc_attr( $layout_class ); ?>">
    <div class="<?php echo esc_attr( $container_class ); ?>">

        <?php if ( $section_title ) : ?>
            <div class="blog-over-trending-header">
                <div class="blog-over-section-heading-wrap">
                    <h2 class="blog-over-section-heading blog-over-trending-heading">
                        <?php echo esc_html( $section_title ); ?>
                        <?php if ( $badge_text ) : ?>
                            <span class="blog-over-trending-badge" style="display:inline-block; margin-left: 12px; vertical-align: middle; font-size: 0.45em; padding: 4px 10px; background: #ef4444; color: #fff; border-radius: 20px; text-transform: uppercase; letter-spacing: 1px; font-weight: 700; transform: translateY(-3px);"><?php echo esc_html( $badge_text ); ?></span>
                        <?php endif; ?>
                    </h2>
                </div>
            </div>
        <?php endif; ?>

        <?php if ( ! $trending_query->have_posts() ) : ?>
            <div class="blog-over-no-posts-found" style="padding: 40px; text-align: center; background: rgba(0,0,0,0.03); border-radius: 12px; border: 1px dashed rgba(0,0,0,0.15); margin-top: 20px;">
                <h4 style="margin: 0 0 8px; font-weight: 600; font-size: 1.2rem;"><?php esc_html_e( 'No Trending Posts Found', 'blog-over' ); ?></h4>
                <p style="margin: 0; font-size: 0.95rem; opacity: 0.7;"><?php esc_html_e( 'Your current settings (Rank By, Time Period, Category) matched 0 posts. Please adjust these conditions in the Customizer.', 'blog-over' ); ?></p>
            </div>
        <?php else : ?>
            <?php
            $cols_class = ' blog-over-trending-cols-' . esc_attr( $columns );
            ?>
            <div class="blog-over-trending-list<?php echo esc_attr( $cols_class ); ?>">
                <?php
                $rank = 0;
                while ( $trending_query->have_posts() ) :
                    $trending_query->the_post();
                    $rank++;
                    $thumb_url = $show_thumb ? get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' ) : '';
                    $cats      = get_the_category();
                ?>
                <article class="blog-over-trending-item">

                    <?php if ( $show_rank ) : ?>
                        <span class="blog-over-trending-rank" aria-label="<?php printf( esc_attr__( 'Rank %d', 'blog-over' ), $rank ); ?>">
                            <?php echo esc_html( str_pad( $rank, 2, '0', STR_PAD_LEFT ) ); ?>
                        </span>
                    <?php endif; ?>

                    <?php if ( $show_thumb && $thumb_url ) : ?>
                        <div class="blog-over-trending-thumb" aria-hidden="true">
                            <a href="<?php echo esc_url( get_permalink() ); ?>" tabindex="-1">
                                <img src="<?php echo esc_url( $thumb_url ); ?>" alt="" loading="lazy">
                            </a>
                        </div>
                    <?php endif; ?>

                    <div class="blog-over-trending-content">
                        <?php foreach ( $elements as $element ) :
                            switch ( $element ) :

                                case 'category':
                                    if ( ! empty( $cats ) ) : ?>
                                        <div class="blog-over-trending-category">
                                            <a href="<?php echo esc_url( get_category_link( $cats[0]->term_id ) ); ?>">
                                                <?php echo esc_html( $cats[0]->name ); ?>
                                            </a>
                                        </div>
                                    <?php endif;
                                    break;

                                case 'title': ?>
                                    <h3 class="blog-over-trending-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>
                                    <?php
                                    break;

                                case 'meta': ?>
                                    <div class="blog-over-trending-meta">
                                        <span class="blog-over-trending-date">
                                            <i class="far fa-calendar-alt" aria-hidden="true"></i>
                                            <?php echo esc_html( get_the_date() ); ?>
                                        </span>
                                        <?php if ( get_comments_number() > 0 ) : ?>
                                            <span class="blog-over-trending-comments">
                                                <i class="far fa-comment" aria-hidden="true"></i>
                                                <?php
                                                printf(
                                                    /* translators: %d: comment count */
                                                    esc_html( _n( '%d comment', '%d comments', get_comments_number(), 'blog-over' ) ),
                                                    number_format_i18n( get_comments_number() )
                                                );
                                                ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <?php
                                    break;

                            endswitch;
                        endforeach; ?>
                    </div><!-- .blog-over-trending-content -->

                    <a href="<?php the_permalink(); ?>" class="blog-over-trending-link-overlay"
                       aria-label="<?php echo esc_attr( get_the_title() ); ?>" tabindex="-1"></a>

                </article><!-- .blog-over-trending-item -->
                <?php endwhile; ?>
            </div><!-- .blog-over-trending-list -->
        <?php endif; ?>

    </div><!-- .container -->
</div><!-- .blog-over-trending-wrapper -->
<?php
wp_reset_postdata();
