<?php
/**
 * The main template file.
 *
 * This is most generic template file in a WordPress theme
 * and one of two required files for a theme (the other is style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g. it puts together home page when no home.php file exists.
 *
 * @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 if ( is_search() ) : ?>

        <?php get_template_part( 'template-parts/page/content-search' ); ?>

    <?php else :

        $blog_heading = get_theme_mod( 'pawwell_blog_heading', __( 'Blog', 'pawwell' ) );
        $blog_subheading = get_theme_mod( 'pawwell_blog_subheading', __( 'Expert-written guides on nutrition, training, health, and the everyday joys of life with pets.', 'pawwell' ) );
        $blog_eyebrow = get_theme_mod( 'pawwell_blog_eyebrow', __( 'The PawCare Journal', 'pawwell' ) );
    ?>

    <!-- Page Title -->
    <section class="blog-page-header">
        <div class="page-title-blob page-title-blob-1"></div>
        <div class="page-title-blob page-title-blob-2"></div>
        <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>
                <span class="current"><?php echo esc_html( $blog_heading ); ?></span>
            </nav>
            <?php endif; ?>
            <div class="page-title-row">
                <div class="page-title-text">
                    <div class="eyebrow"><?php echo esc_html( $blog_eyebrow ); ?></div>
                    <?php
                    $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' ) );
                    }
                    ?>
                    <h1 class="blog-page-title font-display"><?php echo esc_html( $archive_title ); ?></h1>
                    <?php if ( ! empty( $blog_subheading ) && ! is_category() && ! is_tag() && ! is_author() && ! is_date() ) : ?>
                    <p class="blog-page-subtitle"><?php echo esc_html( $blog_subheading ); ?></p>
                    <?php endif; ?>
                </div>
                <?php
                $published_count = wp_count_posts( 'post' );
                $published_count = $published_count ? (int) $published_count->publish : 0;
                $contributors = count( get_users( array( 'role' => 'author', 'fields' => 'ID' ) ) );
                if ( ! $contributors ) {
                    $contributors = count( get_users( array( 'fields' => 'ID' ) ) );
                }
                ?>
                <div class="page-title-stats">
                    <div class="pt-stat">
                        <div class="pt-stat-ic s1">
                            <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="M4 5a2 2 0 0 1 2-2h12v16H6a2 2 0 0 0-2 2z"/><path d="M4 19a2 2 0 0 1 2-2h12"/></svg>
                        </div>
                        <div class="pt-stat-text">
                            <strong><?php echo esc_html( $published_count ); ?></strong>
                            <span><?php esc_html_e( 'articles published', 'pawwell' ); ?></span>
                        </div>
                    </div>
                    <div class="pt-stat">
                        <div class="pt-stat-ic s2">
                            <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="M5 4v5a4 4 0 0 0 8 0V4"/><path d="M5 4H3M13 4h2"/><path d="M9 17v-3"/><path d="M9 17a5 5 0 0 0 9 0v-2"/><circle cx="18" cy="13" r="2"/></svg>
                        </div>
                        <div class="pt-stat-text">
                            <strong><?php echo esc_html( max( $contributors, 1 ) ); ?></strong>
                            <span><?php esc_html_e( 'expert contributors', 'pawwell' ); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Featured + Blog Section -->
    <?php
    if ( have_posts() ) :

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

    <?php if ( $paged <= 1 ) : ?>
    <!-- Featured Article -->
    <section class="featured-section">
        <div class="container">
            <?php
            the_post();
            get_template_part( 'template-parts/blog/blog-featured' );
            ?>
        </div>
    </section>
    <?php endif; ?>

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

            <!-- 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;
                    }
                    $all_count = $published_count;
                    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 <span class="count">%d</span></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' ),
                        (int) $all_count
                    );
                    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 <span class="count">%d</span></a>',
                            esc_url( get_category_link( $cat->term_id ) ),
                            $active_cat === $cat->slug ? ' active' : '',
                            esc_html( $cat->name ),
                            (int) $cat->count
                        );
                    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>

        </div>
    </section>

    <?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;
    ?>

    <?php endif; ?>

    </div>
</div>

<?php get_footer();
