<?php

while ( have_posts() ) : 
    the_post();
    $featured_img_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );
    ?>

    <div class="post-wrapper">
        <?php aurora_lite_breadcrumbs();
            
        if ( has_post_thumbnail() ) {
            echo '<img src="' . esc_url($featured_img_url) . '" alt="' . esc_attr(get_the_title()) . '" class="post-featured-image">';
        }        
        ?>

        <div class="post-header">
            <span class="post-categories"><?php echo wp_kses_post(get_the_category_list(' ')); ?></span>
            <h1 class="entry-title"><?php the_title();?></h1>
            <?php if (has_excerpt()) {
                echo '<p class="post-excerpt">' . wp_kses_post(get_the_excerpt()) . '</p>';
            } ?>
            <div class="post-meta">
                <span class="author-block">
                    <?php echo get_avatar(get_the_author_meta('ID'), 23);
                    echo esc_html(get_the_author()); ?>
                </span>
                <span class="divider">•</span>
                <span class="post-date"><?php echo get_the_date(aurora_lite_get_date_format()); ?></span>
                <span class="divider">•</span>
                <span class="comment-count"><?php aurora_lite_show_comment_number(); ?></span>
                <span class="divider">•</span>
                <?php aurora_lite_reading_time(); ?>
            </div>
            
        </div>

        <div class="post-container">
            <div class="plain-post-content">            
                <div class="aurora-lite-post-content post-content">
                    <?php the_content();
                        wp_link_pages([
                            'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'aurora-lite' ),
                            'after'  => '</div>',
                        ]);
                    ?>
                </div>

                <div class="tags-and-share">
                    <?php
                        $tags = get_the_tags();
                        if ($tags) {
                            echo '<div class="post-tags">';
                            foreach ($tags as $tag) {
                                echo '<a href="' . get_tag_link($tag->term_id) . '" class="post-tag">#' . esc_html($tag->name) . '</a> ';
                            }
                            echo '</div>';
                        }
                    ?>

                    <div class="share-icons tail-div">
                        <?php aurora_lite_display_share_buttons() ?>
                    </div>
                </div>

                <div class="post-navigation">
                    <?php 
                        $prev_post = get_previous_post();
                        $next_post = get_next_post();
                    ?>

                    <?php if ($prev_post) : ?>
                        <div>
                            <span><i class="fa-solid fa-left-long"></i> <?php esc_html_e('Prev post', 'aurora-lite');?></span>
                            <a href="<?php echo get_permalink($prev_post->ID); ?>">
                                <?php echo esc_html(get_the_title($prev_post->ID)); ?>
                            </a>               
                        </div>
                    <?php endif; ?>

                    <?php if ($next_post) : ?>
                        <div class="tail-div">
                            <span><?php esc_html_e('Next post', 'aurora-lite');?> <i class="fa-solid fa-right-long"></i></span>
                            <a href="<?php echo get_permalink($next_post->ID); ?>">
                                <?php echo esc_html(get_the_title($next_post->ID)); ?>
                            </a>                 
                        </div>
                    <?php endif; ?>
                </div>


                <div class="post-author post-div">
                    <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"
                        title="<?php printf(esc_attr__('See more posts by %1$s', 'aurora-lite'), esc_html(get_the_author())); ?>">
                        <?php echo get_avatar(get_the_author_meta('ID'), 100); ?>
                    </a>

                    <span><?php esc_html_e('Author', 'aurora-lite');?></span>

                    <h4>
                        <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"
                        title="<?php printf(esc_attr__('See more posts by %1$s', 'aurora-lite'), esc_html(get_the_author())); ?>">
                            <?php echo esc_html(get_the_author()); ?>
                        </a>
                    </h4>

                    <?php echo '<p class="author-bio">' . esc_html(get_the_author_meta('description')) . '</p>';?>
                </div>

                <?php
                    $related_posts = new WP_Query(array(
                        'category__in'   => wp_get_post_categories(get_the_ID()),
                        'post__not_in'   => array(get_the_ID()),
                        'posts_per_page' => 6,
                    ));

                    if ($related_posts->have_posts()) : ?>
                        <div class="related-post post-div">
                            <h3 class="section-title"><?php esc_html_e('Related posts', 'aurora-lite');?></h3>
                            <div class="related-posts">
                                <?php while ($related_posts->have_posts()) : $related_posts->the_post(); ?>
                                    <div class="related-article">
                                        <a href="<?php the_permalink(); ?>">
                                            <div class="article-thumbnail" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID(), 'large'); ?>');"></div>
                                        </a>
                                        <a href="<?php the_permalink(); ?>" class="article-title"><?php the_title(); ?></a>
                                    </div>
                                <?php endwhile; ?>
                            </div>
                        </div>
                        <?php wp_reset_postdata();
                    endif; ?>
                    
                    <?php
                        if ( comments_open() || get_comments_number() ) : ?>
                            <div class="post-comments post-div">
                                <h3 class="section-title"><?php aurora_lite_show_comment_number(); ?></h3>
                                <?php comments_template(); ?>
                            </div>
                        <?php endif;
                    ?>

            </div>

            <?php if ( get_theme_mod( 'aurora_lite_show_sidebar', true ) ) : ?>
                <?php get_sidebar(); ?>
            <?php endif; ?>
            
        </div>
  
</div>
<?php 
endwhile;