<?php
/**
 * Homepage template.
 *
 * Sections:
 *   1. Hero — full-width dark background with heading and CTA
 *   2. This Week — tabbed section with upcoming events
 *   3. Now Booking — grid of featured events
 *   4. About — brief intro with link to Our Story
 *   (Newsletter is in footer, rendered globally)
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

get_header();

// Fetch data for homepage sections.
$featured_events = northfield_get_events( array(
	'featured' => true,
	'orderby'  => 'date',
	'order'    => 'ASC',
	'limit'    => 6,
) );

// "This week" events — get the nearest upcoming events.
// Uses first available date range if nothing is literally this week.
$today       = gmdate( 'Y-m-d' );
$week_end    = gmdate( 'Y-m-d', strtotime( '+7 days' ) );
$this_week   = northfield_get_events_in_range( $today, $week_end );

if ( empty( $this_week ) ) {
	// No events this week — show the next available batch.
	$all_upcoming = northfield_get_events( array(
		'orderby' => 'date',
		'order'   => 'ASC',
		'limit'   => 4,
	) );
	$this_week = $all_upcoming;
} else {
	$this_week = array_slice( $this_week, 0, 4 );
}

// Free events for "Free things to do" tab.
$free_events = array_filter( northfield_get_events(), function ( $e ) {
	return $e['minPrice'] <= 0;
} );
$free_events = array_values( $free_events );
?>

<!-- ====================================================================
     1. HERO SECTION
     ==================================================================== -->
<?php
// Hero images for crossfade. Draws on the existing event hero images so
// we never ship a stale placeholder — and so the hero feels editorial,
// previewing actual programming.
$hero_images = array(
	'swan-lake-hero.jpg',
	'cinderella-hero.jpg',
	'one-man-hero.jpg',
);
$hero_images = array_values( array_filter( $hero_images, function ( $img ) {
	return file_exists( NORTHFIELD_DIR . '/assets/images/' . $img );
} ) );
?>
<section class="hero" aria-label="<?php esc_attr_e( 'Welcome to Northfield Theatre', 'northfield-theatre' ); ?>">
	<div class="hero__backdrop" id="hero-backdrop" aria-hidden="true">
		<?php foreach ( $hero_images as $i => $img ) : ?>
			<div class="hero__slide <?php echo $i === 0 ? 'hero__slide--active' : ''; ?>"
			     style="background-image: url('<?php echo esc_url( NORTHFIELD_URI . '/assets/images/' . $img ); ?>')"
			     data-index="<?php echo (int) $i; ?>"></div>
		<?php endforeach; ?>
	</div>
	<div class="hero__overlay"></div>
	<div class="hero__content container">
		<p class="hero__eyebrow"><?php echo esc_html( get_bloginfo( 'description' ) ); ?></p>
		<h1 class="hero__title">
			<?php esc_html_e( 'Extraordinary theatre,', 'northfield-theatre' ); ?><br>
			<span class="hero__title-accent"><?php esc_html_e( 'made in Yorkshire.', 'northfield-theatre' ); ?></span>
		</h1>
		<a href="<?php echo esc_url( home_url( '/whats-on/' ) ); ?>" class="btn btn--primary btn--lg hero__cta">
			<?php esc_html_e( 'Explore the season', 'northfield-theatre' ); ?>
		</a>
	</div>
</section>

<!-- ====================================================================
     2. THIS WEEK — Tabbed section
     ==================================================================== -->
<section class="this-week section" aria-labelledby="this-week-heading">
	<div class="container">
		<h2 id="this-week-heading" class="sr-only"><?php esc_html_e( 'Coming up at Northfield Theatre', 'northfield-theatre' ); ?></h2>

		<div class="this-week__tabs" role="tablist" aria-label="<?php esc_attr_e( 'What\'s happening', 'northfield-theatre' ); ?>">
			<button class="this-week__tab this-week__tab--active"
			        role="tab"
			        id="tab-coming-up"
			        aria-selected="true"
			        aria-controls="panel-coming-up"
			        tabindex="0">
				<?php esc_html_e( 'Coming up', 'northfield-theatre' ); ?>
			</button>
			<button class="this-week__tab"
			        role="tab"
			        id="tab-free"
			        aria-selected="false"
			        aria-controls="panel-free"
			        tabindex="-1">
				<?php esc_html_e( 'Free things to do', 'northfield-theatre' ); ?>
			</button>
			<button class="this-week__tab"
			        role="tab"
			        id="tab-visit"
			        aria-selected="false"
			        aria-controls="panel-visit"
			        tabindex="-1">
				<?php esc_html_e( 'Plan your visit', 'northfield-theatre' ); ?>
			</button>
		</div>

		<!-- Tab Panel: Coming Up -->
		<div class="this-week__panel"
		     role="tabpanel"
		     id="panel-coming-up"
		     aria-labelledby="tab-coming-up"
		     tabindex="0">
			<?php if ( ! empty( $this_week ) ) : ?>
				<div class="event-grid event-grid--3">
					<?php foreach ( array_slice( $this_week, 0, 3 ) as $event ) : ?>
						<?php get_template_part( 'template-parts/content', 'event-card', array( 'event' => $event ) ); ?>
					<?php endforeach; ?>
				</div>
			<?php else : ?>
				<p class="this-week__empty"><?php esc_html_e( 'No events scheduled this week. Check back soon!', 'northfield-theatre' ); ?></p>
			<?php endif; ?>
		</div>

		<!-- Tab Panel: Free Things -->
		<div class="this-week__panel this-week__panel--hidden"
		     role="tabpanel"
		     id="panel-free"
		     aria-labelledby="tab-free"
		     tabindex="0"
		     hidden>
			<?php if ( ! empty( $free_events ) ) : ?>
				<div class="event-grid event-grid--3">
					<?php foreach ( array_slice( $free_events, 0, 3 ) as $event ) : ?>
						<?php get_template_part( 'template-parts/content', 'event-card', array( 'event' => $event ) ); ?>
					<?php endforeach; ?>
				</div>
			<?php else : ?>
				<p class="this-week__empty"><?php esc_html_e( 'No free events at the moment. Visit our Foyer Bar for free jazz every Friday!', 'northfield-theatre' ); ?></p>
			<?php endif; ?>
		</div>

		<!-- Tab Panel: Plan Your Visit -->
		<div class="this-week__panel this-week__panel--hidden"
		     role="tabpanel"
		     id="panel-visit"
		     aria-labelledby="tab-visit"
		     tabindex="0"
		     hidden>
			<div class="visit-promo">
				<div class="visit-promo__grid">
					<div class="visit-promo__card fade-in-up">
						<h3 class="visit-promo__title"><?php esc_html_e( 'Getting Here', 'northfield-theatre' ); ?></h3>
						<p><?php esc_html_e( 'Find us in the heart of Northfield, West Yorkshire. We\'re a 10-minute walk from the train station with parking available nearby.', 'northfield-theatre' ); ?></p>
						<a href="<?php echo esc_url( home_url( '/visit/#getting-here' ) ); ?>" class="btn btn--outline btn--sm"><?php esc_html_e( 'Directions', 'northfield-theatre' ); ?></a>
					</div>
					<div class="visit-promo__card fade-in-up">
						<h3 class="visit-promo__title"><?php esc_html_e( 'Eat & Drink', 'northfield-theatre' ); ?></h3>
						<p><?php esc_html_e( 'Our Foyer Bar is open from 5pm on performance evenings, serving craft beers, wines, cocktails, and small plates.', 'northfield-theatre' ); ?></p>
						<a href="<?php echo esc_url( home_url( '/visit/#eat-drink' ) ); ?>" class="btn btn--outline btn--sm"><?php esc_html_e( 'See menus', 'northfield-theatre' ); ?></a>
					</div>
					<div class="visit-promo__card fade-in-up">
						<h3 class="visit-promo__title"><?php esc_html_e( 'Access', 'northfield-theatre' ); ?></h3>
						<p><?php esc_html_e( 'We\'re committed to making our programme accessible to everyone. Read about our facilities and accessible performances.', 'northfield-theatre' ); ?></p>
						<a href="<?php echo esc_url( home_url( '/access/' ) ); ?>" class="btn btn--outline btn--sm"><?php esc_html_e( 'Access info', 'northfield-theatre' ); ?></a>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<!-- ====================================================================
     3. NOW BOOKING — Featured events grid
     ==================================================================== -->
<section class="now-booking section section--cream" aria-labelledby="now-booking-heading">
	<div class="container">
		<div class="now-booking__header">
			<h2 id="now-booking-heading" class="now-booking__title"><?php esc_html_e( 'Now Booking', 'northfield-theatre' ); ?></h2>
			<a href="<?php echo esc_url( home_url( '/whats-on/' ) ); ?>" class="now-booking__view-all btn btn--outline btn--sm hide-mobile">
				<?php esc_html_e( 'View all events', 'northfield-theatre' ); ?>
			</a>
		</div>

		<?php if ( ! empty( $featured_events ) ) : ?>
			<div class="event-grid event-grid--3">
				<?php foreach ( $featured_events as $event ) : ?>
					<?php get_template_part( 'template-parts/content', 'event-card', array( 'event' => $event ) ); ?>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

		<div class="now-booking__cta show-mobile-only">
			<a href="<?php echo esc_url( home_url( '/whats-on/' ) ); ?>" class="btn btn--outline btn--full">
				<?php esc_html_e( 'View all events', 'northfield-theatre' ); ?>
			</a>
		</div>
	</div>
</section>

<!-- ====================================================================
     4. ABOUT — Brief introduction
     ==================================================================== -->
<section class="about-home section" aria-labelledby="about-home-heading">
	<div class="container">
		<div class="about-home__grid">
			<div class="about-home__content fade-in-up">
				<h2 id="about-home-heading" class="about-home__heading">
					<?php echo esc_html( get_bloginfo( 'name' ) ); ?>
				</h2>
				<p class="about-home__lead">
					<?php esc_html_e( 'A 1,200-seat receiving house in the heart of West Yorkshire, presenting the best in drama, comedy, music, dance, and family entertainment.', 'northfield-theatre' ); ?>
				</p>
				<p>
					<?php esc_html_e( 'Since opening our doors in 1932, Northfield Theatre has been at the centre of Yorkshire\'s cultural life. From world-class touring productions to intimate studio performances, from free Friday jazz to spectacular family shows, we believe that extraordinary theatre should be for everyone.', 'northfield-theatre' ); ?>
				</p>
				<a href="<?php echo esc_url( home_url( '/our-story/' ) ); ?>" class="btn btn--secondary about-home__cta">
					<?php esc_html_e( 'Read our story', 'northfield-theatre' ); ?>
				</a>
			</div>
			<div class="about-home__image fade-in-up">
				<?php
				$about_image = NORTHFIELD_DIR . '/assets/images/about-theatre.jpg';
				if ( file_exists( $about_image ) ) : ?>
					<img src="<?php echo esc_url( NORTHFIELD_URI . '/assets/images/about-theatre.jpg' ); ?>"
					     alt="<?php esc_attr_e( 'Inside the Northfield Theatre auditorium', 'northfield-theatre' ); ?>"
					     class="about-home__img"
					     loading="lazy"
					     decoding="async"
					     width="800"
					     height="600" />
				<?php else : ?>
					<div class="about-home__image-placeholder" aria-hidden="true">
						<span class="about-home__image-text"><?php esc_html_e( 'Northfield Theatre auditorium', 'northfield-theatre' ); ?></span>
					</div>
				<?php endif; ?>
			</div>
		</div>
	</div>
</section>

<?php
get_footer();
