<?php
/**
 * Event Archive fallback.
 *
 * Redirects to the What's On page since that's our primary event listing.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

$whats_on_url = home_url( '/whats-on/' );

// If a What's On page exists, redirect to it.
$whats_on_page = get_page_by_path( 'whats-on' );
if ( $whats_on_page ) {
	wp_safe_redirect( get_permalink( $whats_on_page ), 301 );
	exit;
}

// Otherwise, show a basic archive.
get_header();
?>

<div class="container">
	<header class="page-header">
		<h1 class="page-header__title"><?php esc_html_e( 'Events', 'northfield-theatre' ); ?></h1>
	</header>

	<?php if ( have_posts() ) : ?>
		<div class="event-grid event-grid--3">
			<?php while ( have_posts() ) : the_post();
				$json_id = get_post_meta( get_the_ID(), '_nt_event_json_id', true );
				$event = $json_id ? northfield_get_event( $json_id ) : null;
				if ( $event ) :
					get_template_part( 'template-parts/content', 'event-card', array( 'event' => $event ) );
				endif;
			endwhile; ?>
		</div>
		<?php the_posts_pagination(); ?>
	<?php else : ?>
		<p><?php esc_html_e( 'No events found.', 'northfield-theatre' ); ?></p>
	<?php endif; ?>
</div>

<?php
get_footer();
