<?php
/**
 * News / Blog listing template.
 *
 * WordPress uses home.php when a static front page is set and the blog
 * posts page is a separate page (as we do — front-page.php renders the
 * homepage, and this renders the /news/ archive).
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

get_header();

$feature_post = null;
$latest_query = new WP_Query( array(
	'post_type'      => 'post',
	'posts_per_page' => 1,
	'post_status'    => 'publish',
) );
if ( $latest_query->have_posts() ) {
	$feature_post = $latest_query->posts[0];
}

$list_query = new WP_Query( array(
	'post_type'      => 'post',
	'posts_per_page' => 9,
	'post_status'    => 'publish',
	'offset'         => 1,
	'paged'          => get_query_var( 'paged' ) ?: 1,
) );
?>

<article class="news-archive">

	<header class="news-archive__hero section--dark">
		<div class="container news-archive__hero-inner">
			<p class="news-archive__eyebrow"><?php esc_html_e( 'News', 'northfield-theatre' ); ?></p>
			<h1 class="news-archive__title"><?php esc_html_e( 'Dispatches from the theatre.', 'northfield-theatre' ); ?></h1>
			<p class="news-archive__lead">
				<?php esc_html_e( 'Season announcements, interviews, reviews, access updates, and the occasional essay.', 'northfield-theatre' ); ?>
			</p>
		</div>
	</header>

	<?php if ( $feature_post ) : ?>
		<section class="news-feature section">
			<div class="container">
				<?php
				$feature_cat = get_the_category( $feature_post->ID );
				$feature_cat_name = ! empty( $feature_cat ) ? $feature_cat[0]->name : '';
				?>
				<a href="<?php echo esc_url( get_permalink( $feature_post ) ); ?>" class="news-feature__link">
					<div class="news-feature__grid">
						<div class="news-feature__body">
							<?php if ( $feature_cat_name ) : ?>
								<span class="news-feature__category"><?php echo esc_html( $feature_cat_name ); ?></span>
							<?php endif; ?>
							<h2 class="news-feature__title"><?php echo esc_html( $feature_post->post_title ); ?></h2>
							<p class="news-feature__excerpt"><?php echo esc_html( $feature_post->post_excerpt ? $feature_post->post_excerpt : wp_trim_words( strip_tags( $feature_post->post_content ), 32 ) ); ?></p>
							<div class="news-feature__meta">
								<time datetime="<?php echo esc_attr( get_the_date( 'c', $feature_post ) ); ?>">
									<?php echo esc_html( get_the_date( 'j F Y', $feature_post ) ); ?>
								</time>
								<span class="news-feature__readmore"><?php esc_html_e( 'Read article', 'northfield-theatre' ); ?> →</span>
							</div>
						</div>
						<?php $feature_image = northfield_get_post_illustration_url( $feature_post ); ?>
						<div class="news-feature__media"
						     <?php if ( $feature_image ) : ?>style="background-image: url('<?php echo esc_url( $feature_image ); ?>')"<?php endif; ?>
						     aria-hidden="true"></div>
					</div>
				</a>
			</div>
		</section>
	<?php endif; ?>

	<section class="news-list section section--cream">
		<div class="container">
			<?php if ( $list_query->have_posts() ) : ?>
				<div class="news-list__grid">
					<?php while ( $list_query->have_posts() ) : $list_query->the_post(); ?>
						<?php
						$cats   = get_the_category();
						$cat    = ! empty( $cats ) ? $cats[0]->name : '';
						?>
						<article class="news-card fade-in-up">
							<a href="<?php the_permalink(); ?>" class="news-card__link">
								<?php if ( $cat ) : ?>
									<span class="news-card__category"><?php echo esc_html( $cat ); ?></span>
								<?php endif; ?>
								<h3 class="news-card__title"><?php the_title(); ?></h3>
								<p class="news-card__excerpt"><?php echo esc_html( get_the_excerpt() ); ?></p>
								<div class="news-card__meta">
									<time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
										<?php echo esc_html( get_the_date( 'j F Y' ) ); ?>
									</time>
									<span class="news-card__arrow" aria-hidden="true">→</span>
								</div>
							</a>
						</article>
					<?php endwhile; ?>
				</div>

				<?php
				// Pagination.
				$total_pages = $list_query->max_num_pages;
				if ( $total_pages > 1 ) {
					echo '<div class="news-list__pagination">';
					echo paginate_links( array(
						'total'     => $total_pages,
						'prev_text' => esc_html__( '← Previous', 'northfield-theatre' ),
						'next_text' => esc_html__( 'Next →', 'northfield-theatre' ),
					) );
					echo '</div>';
				}

				wp_reset_postdata();
				?>

			<?php else : ?>
				<p class="news-list__empty"><?php esc_html_e( 'No news to share just yet — check back soon.', 'northfield-theatre' ); ?></p>
			<?php endif; ?>
		</div>
	</section>

</article>

<?php
get_footer();
