<?php
/**
 * Template for displaying single posts
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

get_header();

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

// Build breadcrumbs
$breadcrumbs = array(
    array( 'label' => __( 'Home', 'paws-and-care' ), 'url' => home_url( '/' ) ),
);
$blog_page_id = get_option( 'page_for_posts' );
if ( $blog_page_id ) {
    $breadcrumbs[] = array( 'label' => __( 'Blog', 'paws-and-care' ), 'url' => get_permalink( $blog_page_id ) );
}
$breadcrumbs[] = array( 'label' => get_the_title() );

// Page Header
get_template_part( 'template-parts/page-header', null, array(
    'title'       => get_the_title(),
    'subtitle'    => has_excerpt() ? get_the_excerpt() : '',
    'breadcrumbs' => $breadcrumbs,
) );
?>

    <!-- Blog Post Content with Sidebar -->
    <section id="primary-content" class="py-5">
        <div class="container">
            <div class="row">
                <!-- Main Content -->
                <div class="<?php echo esc_attr( trim( $sidebar_layout['content_class'] . ' ' . $sidebar_layout['content_order'] ) ); ?>">
                    <?php
                    while ( have_posts() ) :
                        the_post();
                        ?>
                        <article id="post-<?php the_ID(); ?>" <?php post_class( 'blog-post' ); ?>>
                            <div class="post-meta mb-4">
                                <?php
                                $categories = get_the_category();
                                if ( ! empty( $categories ) ) :
                                    foreach ( $categories as $category ) :
                                        ?>
                                        <span class="badge bg-primary me-2"><?php echo esc_html( $category->name ); ?></span>
                                        <?php
                                    endforeach;
                                endif;
                                ?>
                                <small class="text-muted">
                                    <i class="fa-regular fa-calendar me-1"></i> <?php echo esc_html( get_the_date() ); ?>
                                    <i class="fa-regular fa-user ms-3 me-1"></i> <?php the_author(); ?>
                                    <i class="fa-regular fa-clock ms-3 me-1"></i> <?php echo esc_html( paws_and_care_reading_time() ); ?>
                                </small>
                            </div>

                            <?php if ( has_post_thumbnail() ) : ?>
                                <div class="post-thumbnail mb-4">
                                    <?php the_post_thumbnail( 'large', array( 'class' => 'img-fluid rounded' ) ); ?>
                                </div>
                            <?php endif; ?>

                            <div class="post-content">
                                <?php the_content(); ?>
                            </div>

                            <div class="post-footer mt-5 pt-4 border-top">
                                <div class="post-tags">
                                    <?php
                                    $tags = get_the_tags();
                                    if ( $tags ) :
                                        ?>
                                        <span class="me-2"><?php esc_html_e( 'Tags:', 'paws-and-care' ); ?></span>
                                        <?php foreach ( $tags as $tag ) : ?>
                                            <a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" class="badge bg-light text-dark me-1"><?php echo esc_html( $tag->name ); ?></a>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </article>

                        <!-- Author Bio -->
                        <div class="author-bio card border-0 shadow-sm mb-5 p-4">
                            <div class="d-flex align-items-center">
                                <div class="flex-shrink-0">
                                    <?php echo get_avatar( get_the_author_meta( 'ID' ), 90, '', '', array( 'class' => 'rounded-circle' ) ); ?>
                                </div>
                                <div class="flex-grow-1 ms-4">
                                    <h5 class="mt-0 font-weight-bold mb-1"><?php echo esc_html( get_the_author() ); ?></h5>
                                    <p class="mb-0 text-muted"><?php echo esc_html( get_the_author_meta( 'description' ) ); ?></p>
                                </div>
                            </div>
                        </div>

                        <!-- Post Navigation -->
                        <div class="post-navigation my-5">
                            <div class="row">
                                <div class="col-6">
                                    <?php
                                    $prev_post = get_previous_post();
                                    if ( $prev_post ) :
                                        ?>
                                        <a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>" class="btn btn-outline-primary">
                                            <i class="fa-solid fa-arrow-left me-2"></i> <?php echo esc_html( wp_trim_words( get_the_title( $prev_post->ID ), 5, '...' ) ); ?>
                                        </a>
                                    <?php endif; ?>
                                </div>
                                <div class="col-6 text-end">
                                    <?php
                                    $next_post = get_next_post();
                                    if ( $next_post ) :
                                        ?>
                                        <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>" class="btn btn-outline-primary">
                                            <?php echo esc_html( wp_trim_words( get_the_title( $next_post->ID ), 5, '...' ) ); ?> <i class="fa-solid fa-arrow-right ms-2"></i>
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <?php
                        // Comments
                        if ( comments_open() || get_comments_number() ) :
                            comments_template();
                        endif;

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

    <!-- Related Posts -->
    <?php
    $categories = get_the_category();
    if ( ! empty( $categories ) ) :
        $category_ids = array();
        foreach ( $categories as $category ) {
            $category_ids[] = $category->term_id;
        }

        $related_posts = new WP_Query( array(
            'category__in'        => $category_ids,
            'post__not_in'        => array( get_the_ID() ),
            'posts_per_page'      => 2,
            'ignore_sticky_posts' => true,
        ) );

        if ( $related_posts->have_posts() ) :
            ?>
            <section class="py-5 bg-light">
                <div class="container">
                    <div class="row justify-content-center">
                        <div class="col-lg-8">
                            <h3 class="mb-4"><?php esc_html_e( 'Related Posts', 'paws-and-care' ); ?></h3>
                            <div class="row">
                                <?php
                                while ( $related_posts->have_posts() ) :
                                    $related_posts->the_post();
                                    ?>
                                    <div class="col-md-6">
                                        <div class="card border-0 shadow-sm mb-4">
                                            <?php if ( has_post_thumbnail() ) : ?>
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php the_post_thumbnail( 'medium', array( 'class' => 'card-img-top' ) ); ?>
                                                </a>
                                            <?php endif; ?>
                                            <div class="card-body">
                                                <div class="mb-2">
                                                    <?php
                                                    $post_categories = get_the_category();
                                                    if ( ! empty( $post_categories ) ) :
                                                        ?>
                                                        <span class="badge bg-primary me-2"><?php echo esc_html( $post_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(), 12, '...' ); ?></p>
                                                <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>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            <?php
            wp_reset_postdata();
        endif;
    endif;
    ?>

<?php
get_footer();

