<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

get_header();

// Get sidebar layout settings
$sidebar_layout = paws_and_care_get_sidebar_layout();

// Get blog columns setting
$blog_columns = get_theme_mod('paws_and_care_archive_blog_columns', '2');
$column_class = 'col-md-6'; // Default

if ('1' === $blog_columns) {
    $column_class = 'col-12';
} elseif ('3' === $blog_columns) {
    $column_class = 'col-md-6 col-lg-4';
}
?>

    <main id="primary-content" class="site-main">

    <?php
    // Page Header with custom args for blog
    get_template_part( 'template-parts/page', 'header', array(
        'title'       => get_theme_mod('paws_and_care_blog_title', __( 'Pet Care Blog', 'paws-and-care' )),
        'subtitle'    => get_theme_mod('paws_and_care_blog_subtitle', __( 'Expert tips, advice, and insights for pet owners', 'paws-and-care' )),
        'breadcrumbs' => array(
            array( 'label' => __( 'Home', 'paws-and-care' ), 'url' => home_url( '/' ) ),
            array( 'label' => __( 'Blog', 'paws-and-care' ), 'url' => '' ),
        ),
    ) );
    ?>

    <!-- Blog Section -->
    <section class="py-5 blog-section">
        <div class="container">
            <div class="row">
                <div class="<?php echo esc_attr( trim( $sidebar_layout['content_class'] . ' ' . $sidebar_layout['content_order'] ) ); ?>">
                    <?php if ( have_posts() ) : ?>
                        <div class="row g-4">
                            <?php
                            while ( have_posts() ) :
                                the_post();
                                ?>
                                <div class="<?php echo esc_attr( $column_class ); ?>">
                                    <div class="blog-card card h-100">
                                        <?php if ( has_post_thumbnail() ) : ?>
                                            <a href="<?php the_permalink(); ?>">
                                                <?php the_post_thumbnail( 'medium_large', array( 'class' => 'card-img-top' ) ); ?>
                                            </a>
                                        <?php endif; ?>
                                        <div class="card-body">
                                            <div class="d-flex align-items-center mb-3">
                                                <?php
                                                $categories = get_the_category();
                                                if ( ! empty( $categories ) ) :
                                                    ?>
                                                    <span class="badge bg-primary me-2"><?php echo esc_html( $categories[0]->name ); ?></span>
                                                <?php endif; ?>
                                                <small class="text-muted"><i class="fa-regular fa-calendar me-1"></i> <?php echo esc_html( get_the_date() ); ?></small>
                                            </div>
                                            <h5 class="card-title">
                                                <a href="<?php the_permalink(); ?>" class="text-decoration-none text-dark"><?php the_title(); ?></a>
                                            </h5>
                                            <p class="card-text"><?php echo wp_trim_words( get_the_excerpt(), 20, '...' ); ?></p>
                                            <div class="d-flex align-items-center justify-content-between">
                                                <a href="<?php the_permalink(); ?>" class="btn btn-link text-primary p-0">
                                                    <?php esc_html_e( 'Read More', 'paws-and-care' ); ?> <i class="fa-solid fa-arrow-right ms-1"></i>
                                                </a>
                                                <small class="text-muted">
                                                    <i class="fa-regular fa-clock me-1"></i> <?php echo esc_html( paws_and_care_reading_time() ); ?>
                                                </small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        </div>

                        <!-- Pagination -->
                        <nav aria-label="Blog pagination" class="mt-5 blog-pagination">
                            <?php
                            the_posts_pagination( array(
                                'mid_size'  => 2,
                                'prev_text' => __( 'Previous', 'paws-and-care' ),
                                'next_text' => __( 'Next', 'paws-and-care' ),
                                'class'     => 'pagination justify-content-center',
                            ) );
                            ?>
                        </nav>

                    <?php else : ?>
                        <?php get_template_part( 'template-parts/content', 'none' ); ?>
                    <?php endif; ?>
                </div>

                <?php if ( $sidebar_layout['show_sidebar'] ) : ?>
                    <!-- Sidebar -->
                    <div class="<?php echo esc_attr( trim( $sidebar_layout['sidebar_class'] . ' ' . $sidebar_layout['sidebar_order'] ) ); ?>">
                        <?php get_template_part( 'template-parts/blog', 'sidebar' ); ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </section>

    </main>

<?php
get_footer();

