<?php
/**
 * The template for displaying all single posts
 */

get_header(); ?>

<main id="primary" class="site-main container my-5">
	<?php
	while (have_posts()):
		the_post();
		?>
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<header class="entry-header mb-4 mx-auto px-3" style="max-width: 680px;">
				<?php the_title('<h1 class="entry-title display-4 fw-bold font-serif lh-sm mb-4">', '</h1>'); ?>

				<div class="d-flex align-items-center mb-4 pb-4 border-bottom">
					<div class="me-3">
						<?php echo get_avatar(get_the_author_meta('ID'), 48, '', '', array('class' => 'rounded-circle')); ?>
					</div>
					<div>
						<div class="fw-bold font-sans text-dark"><?php the_author(); ?></div>
						<div class="entry-meta text-muted small font-sans d-flex align-items-center flex-wrap gap-2">
							<span><?php echo wisdom1_reading_time(); ?></span>
							<span>•</span>
							<span class="entry-date"><?php echo get_the_date('M j, Y'); ?></span>
							<?php if (get_comments_number() > 0): ?>
								<span>•</span>
								<span><?php comments_number('0', '1 Response', '% Responses'); ?></span>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</header>

			<?php if (has_post_thumbnail()): ?>
				<div class="post-thumbnail-full mb-5 mx-auto" style="max-width: 1000px;">
					<?php the_post_thumbnail('full', array(
						'class' => 'img-fluid w-100 rounded',
						'style' => 'aspect-ratio: 16/9; object-fit: cover;'
					)); ?>
				</div>
			<?php endif; ?>

			<div class="entry-content fs-5 lh-lg font-sans text-dark mx-auto px-3" style="max-width: 680px;">
				<?php
				the_content();

				wp_link_pages(
					array(
						'before' => '<div class="page-links mt-5"><strong>' . esc_html__('Pages:', 'wisdom1') . '</strong>',
						'after' => '</div>',
					)
				);
				?>
			</div><!-- .entry-content -->

			<footer class="entry-footer mt-5 pt-4 border-top mx-auto px-3" style="max-width: 680px;">
				<div class="entry-tags d-flex flex-wrap gap-2">
					<?php
					the_tags('<span class="badge bg-light text-dark text-decoration-none fw-normal p-2">', '</span><span class="badge bg-light text-dark text-decoration-none fw-normal p-2">', '</span>');

					$series_terms = get_the_terms(get_the_ID(), 'series');
					if ($series_terms && !is_wp_error($series_terms)) {
						foreach ($series_terms as $term) {
							echo '<a href="' . esc_url(get_term_link($term)) . '" class="badge bg-secondary text-white text-decoration-none fw-bold p-2"><i class="bi bi-collection"></i> ' . esc_html($term->name) . '</a>';
						}
					}
					?>
				</div>
			</footer><!-- .entry-footer -->
			<div class="mx-auto px-3 mb-3" style="max-width: 680px;">
				<?php
				// If comments are open or we have at least one comment, load up the comment template inside the constrained column.
				if (comments_open() || get_comments_number()):
					comments_template();
				endif;
				?>
			</div>

			<?php
			$prev_post = get_previous_post();
			$next_post = get_next_post();
			if (!empty($prev_post) || !empty($next_post)):
				?>
				<div class="mt-5 pt-5 border-top">
					<h3 class="h4 font-serif fw-bold mb-4 text-center">More Articles</h3>
					<div class="row justify-content-center gx-4 gy-4">
						<?php
						global $post;
						$original_post = $post;

						if (!empty($prev_post)) {
							$post = $prev_post;
							setup_postdata($post);
							get_template_part('template-parts/content', 'card');
						}
						if (!empty($next_post)) {
							$post = $next_post;
							setup_postdata($post);
							get_template_part('template-parts/content', 'card');
						}

						$post = $original_post;
						wp_reset_postdata();
						?>
					</div>
				</div>
			<?php endif; ?>

		<?php endwhile; // End of the loop. ?>
</main><!-- #main -->

<?php get_template_part('template-parts/newsletter'); ?>

<?php
get_footer();
