<?php
/**
 * The template for displaying archive pages (blog).
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package pawwell
 */

get_header();
?>

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

    <?php
    $blog_heading = get_theme_mod( 'pawwell_blog_heading', __( 'Blog', 'pawwell' ) );
    $archive_title = $blog_heading;
    if ( is_category() ) {
        $archive_title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $archive_title = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $archive_title = esc_html__( 'Author: ', 'pawwell' ) . esc_html( get_the_author() );
    } elseif ( is_day() ) {
        $archive_title = esc_html( get_the_date() );
    } elseif ( is_month() ) {
        $archive_title = esc_html( get_the_date( 'F Y' ) );
    } elseif ( is_year() ) {
        $archive_title = esc_html( get_the_date( 'Y' ) );
    }
    ?>

    <!-- Page Title -->
    <section class="blog-page-header">
        <div class="container">
            <?php if ( pawwell_breadcrumb_is_enabled() ) : ?>
            <nav class="page-title-crumb" aria-label="breadcrumb">
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 11 12 4l9 7"/><path d="M5 10v10h14V10"/><path d="M10 20v-6h4v6"/></svg>
                    <?php esc_html_e( 'Home', 'pawwell' ); ?>
                </a>
                <svg class="sep" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 6 6 6-6 6"/></svg>
                <a href="<?php echo esc_url( get_permalink( get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' ) ); ?>"><?php echo esc_html( $blog_heading ); ?></a>
                <svg class="sep" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 6 6 6-6 6"/></svg>
                <span class="current"><?php echo esc_html( $archive_title ); ?></span>
            </nav>
            <?php endif; ?>
            <div class="page-title-row">
                <div class="page-title-text">
                    <div class="eyebrow"><?php esc_html_e( 'Archive', 'pawwell' ); ?></div>
                    <h1 class="blog-page-title font-display"><?php echo esc_html( $archive_title ); ?></h1>
                    <?php if ( is_category() && category_description() ) : ?>
                        <p class="blog-page-subtitle"><?php echo wp_kses_post( wp_strip_all_tags( category_description() ) ); ?></p>
                    <?php elseif ( is_author() && get_the_author_meta( 'description' ) ) : ?>
                        <p class="blog-page-subtitle"><?php echo esc_html( get_the_author_meta( 'description' ) ); ?></p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

    <!-- Blog Content -->
    <section class="blog-content-section">
        <div class="container">

            <?php if ( have_posts() ) :

                $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
            ?>

            <!-- Category Filter -->
            <div class="category-bar">
                <div class="cat-pills">
                    <?php
                    $blog_cats = get_categories( array(
                        'orderby' => 'count',
                        'order'   => 'DESC',
                        'number'  => 5,
                    ) );
                    $active_cat = '';
                    if ( is_category() ) {
                        $active_cat = get_queried_object()->slug;
                    }
                    printf(
                        '<a href="%s" class="cat-pill%s"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></svg> %s</a>',
                        esc_url( get_permalink( get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' ) ),
                        '' === $active_cat ? ' active' : '',
                        esc_html__( 'All Articles', 'pawwell' )
                    );
                    foreach ( $blog_cats as $cat ) :
                        printf(
                            '<a href="%s" class="cat-pill%s"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 12V4a1 1 0 0 1 1-1h8l9 9-9 9-9-9z"/><circle cx="7.5" cy="7.5" r="1.4"/></svg> %s</a>',
                            esc_url( get_category_link( $cat->term_id ) ),
                            $active_cat === $cat->slug ? ' active' : '',
                            esc_html( $cat->name )
                        );
                    endforeach;
                    ?>
                </div>
            </div>

            <div class="blog-layout">
                <!-- Articles Grid -->
                <div class="blog-content">
                    <div class="blog-grid">
                        <?php
                        while ( have_posts() ) :
                            the_post();
                            get_template_part( 'template-parts/blog/content-archive' );
                        endwhile;
                        ?>
                    </div>

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

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

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

        </div>
    </section>

    </div>
</div>

<?php get_footer();
