<?php
/**
 * Shared blog listing layout
 *
 * @package shopbar
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$shopbar_blog_page_id = get_option( 'page_for_posts' );
$shopbar_blog_home    = $shopbar_blog_page_id ? get_permalink( $shopbar_blog_page_id ) : home_url( '/?post_type=post' );
$shopbar_is_main      = is_home() && ! is_search();
$shopbar_paged        = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$shopbar_is_cat       = is_category();
$shopbar_current_cat  = $shopbar_is_cat ? (int) get_queried_object_id() : 0;

/* Build the page title + intro description */
if ( is_category() ) {
    $shopbar_title = single_cat_title( '', false );
    $shopbar_desc  = wp_strip_all_tags( category_description() );
} elseif ( is_tag() ) {
    $shopbar_title = single_tag_title( '', false );
    $shopbar_desc  = wp_strip_all_tags( tag_description() );
} elseif ( is_author() ) {
    $shopbar_title = esc_html__( 'Author: ', 'shopbar' ) . get_the_author();
    $shopbar_desc  = '';
} elseif ( is_date() ) {
    if ( is_day() ) {
        $shopbar_title = get_the_date();
    } elseif ( is_month() ) {
        $shopbar_title = get_the_date( 'F Y' );
    } else {
        $shopbar_title = get_the_date( 'Y' );
    }
    $shopbar_desc = '';
} else {
    $shopbar_title = get_theme_mod( 'shopbar_blog_heading', __( 'Blog & Insights', 'shopbar' ) );
    $shopbar_desc  = get_theme_mod( 'shopbar_blog_subheading', __( 'Discover tips, trends, and guides to help you shop smarter and live better.', 'shopbar' ) );
}
?>

<div id="primary" class="content-area">
    <div id="main-content" class="site-main" role="main">

    <div class="container">

        <!-- Breadcrumb -->
        <nav class="blog-breadcrumb" aria-label="<?php esc_attr_e( 'Breadcrumb', 'shopbar' ); ?>">
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php esc_html_e( 'Home', 'shopbar' ); ?></a>
            <?php shopbar_icon( 'chevron-right', 11 ); ?>
            <?php if ( is_category() ) : ?>
                <a href="<?php echo esc_url( $shopbar_blog_home ); ?>"><?php esc_html_e( 'Blog', 'shopbar' ); ?></a>
                <?php shopbar_icon( 'chevron-right', 11 ); ?>
                <span class="bc-current"><?php single_cat_title(); ?></span>
            <?php elseif ( is_tag() ) : ?>
                <a href="<?php echo esc_url( $shopbar_blog_home ); ?>"><?php esc_html_e( 'Blog', 'shopbar' ); ?></a>
                <?php shopbar_icon( 'chevron-right', 11 ); ?>
                <span class="bc-current"><?php single_tag_title(); ?></span>
            <?php elseif ( is_author() ) : ?>
                <a href="<?php echo esc_url( $shopbar_blog_home ); ?>"><?php esc_html_e( 'Blog', 'shopbar' ); ?></a>
                <?php shopbar_icon( 'chevron-right', 11 ); ?>
                <span class="bc-current"><?php echo esc_html( $shopbar_title ); ?></span>
            <?php elseif ( is_date() ) : ?>
                <a href="<?php echo esc_url( $shopbar_blog_home ); ?>"><?php esc_html_e( 'Blog', 'shopbar' ); ?></a>
                <?php shopbar_icon( 'chevron-right', 11 ); ?>
                <span class="bc-current"><?php echo esc_html( $shopbar_title ); ?></span>
            <?php else : ?>
                <span class="bc-current"><?php echo esc_html( get_theme_mod( 'shopbar_blog_heading', __( 'Blog', 'shopbar' ) ) ); ?></span>
            <?php endif; ?>
        </nav>

        <!-- Page Header -->
        <header class="blog-intro">
            <h1 class="blog-intro-title"><?php echo esc_html( $shopbar_title ); ?></h1>
            <?php if ( $shopbar_desc ) : ?>
                <p class="blog-intro-desc"><?php echo esc_html( $shopbar_desc ); ?></p>
            <?php endif; ?>
        </header>

        <!-- Layout: Content + Sidebar -->
        <div class="blog-layout">

            <!-- Main Content -->
            <main class="blog-content">
                <?php
                if ( have_posts() ) :

                    // Category filter tabs (main blog + category archives only)
                    if ( $shopbar_is_main || $shopbar_is_cat ) :
                        $shopbar_tabs_cats = get_categories( array(
                            'orderby' => 'name',
                            'order'   => 'ASC',
                            'number'  => 8,
                        ) );
                        if ( ! empty( $shopbar_tabs_cats ) ) :
                        ?>
                        <div class="blog-tabs">
                            <a href="<?php echo esc_url( $shopbar_blog_home ); ?>" class="blog-tab <?php echo $shopbar_is_main ? 'active' : ''; ?>"><?php esc_html_e( 'All Articles', 'shopbar' ); ?></a>
                            <?php foreach ( $shopbar_tabs_cats as $shopbar_tab_cat ) : ?>
                                <a href="<?php echo esc_url( get_category_link( $shopbar_tab_cat->term_id ) ); ?>" class="blog-tab <?php echo ( $shopbar_current_cat === (int) $shopbar_tab_cat->term_id ) ? 'active' : ''; ?>"><?php echo esc_html( $shopbar_tab_cat->name ); ?></a>
                            <?php endforeach; ?>
                        </div>
                        <?php
                        endif;
                    endif;

                    // Featured post
                    $shopbar_featured_id = 0;
                    if ( $shopbar_is_main && $shopbar_paged <= 1 ) :
                        $shopbar_feat_q = new WP_Query( array(
                            'posts_per_page'      => 1,
                            'ignore_sticky_posts' => false,
                            'meta_key'            => '_thumbnail_id',
                            'fields'              => 'ids',
                        ) );
                        if ( $shopbar_feat_q->have_posts() ) :
                            $shopbar_featured_id = (int) $shopbar_feat_q->posts[0];
                            $shopbar_feat_post   = get_post( $shopbar_featured_id );
                            global $post;
                            $post = $shopbar_feat_post;
                            setup_postdata( $shopbar_feat_post );
                            get_template_part( 'template-parts/blog/blog-featured' );
                            wp_reset_postdata();
                        endif;
                    endif;

                    // Blog Grid
                    ?>
                    <div class="blog-grid">
                        <?php
                        while ( have_posts() ) :
                            the_post();
                            if ( get_the_ID() === $shopbar_featured_id ) {
                                continue;
                            }
                            get_template_part( 'template-parts/blog/content-archive' );
                        endwhile;
                        ?>
                    </div>

                    <!-- Pagination -->
                    <?php get_template_part( 'template-parts/blog/blog-pagination', null, array( 'paged' => $shopbar_paged ) ); ?>

                <?php else : ?>
                    <div class="no-posts-found">
                        <h2><?php esc_html_e( 'No posts found', 'shopbar' ); ?></h2>
                        <p><?php esc_html_e( 'It seems we can\'t find what you\'re looking for.', 'shopbar' ); ?></p>
                    </div>
                <?php
                endif;
                ?>
            </main>

            <?php get_template_part( 'template-parts/blog/blog-sidebar' ); ?>

        </div>
    </div>

    </div>
</div>
