<?php
/**
 * Enqueue scripts and styles.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

function northfield_scripts() {
	wp_enqueue_style(
		'northfield-main',
		NORTHFIELD_URI . '/assets/css/main.css',
		array(),
		NORTHFIELD_VERSION
	);

	wp_enqueue_style(
		'northfield-header',
		NORTHFIELD_URI . '/assets/css/header.css',
		array( 'northfield-main' ),
		NORTHFIELD_VERSION
	);

	wp_enqueue_style(
		'northfield-footer',
		NORTHFIELD_URI . '/assets/css/footer.css',
		array( 'northfield-main' ),
		NORTHFIELD_VERSION
	);

	wp_enqueue_style(
		'northfield-event-card',
		NORTHFIELD_URI . '/assets/css/event-card.css',
		array( 'northfield-main' ),
		NORTHFIELD_VERSION
	);

	wp_enqueue_style(
		'northfield-pages',
		NORTHFIELD_URI . '/assets/css/pages.css',
		array( 'northfield-main' ),
		NORTHFIELD_VERSION
	);

	wp_enqueue_style(
		'northfield-responsive',
		NORTHFIELD_URI . '/assets/css/responsive.css',
		array( 'northfield-main', 'northfield-header', 'northfield-footer' ),
		NORTHFIELD_VERSION
	);

	// Conditionally load homepage assets.
	if ( is_front_page() ) {
		wp_enqueue_style(
			'northfield-homepage',
			NORTHFIELD_URI . '/assets/css/homepage.css',
			array( 'northfield-main', 'northfield-event-card' ),
			NORTHFIELD_VERSION
		);

		wp_enqueue_script(
			'northfield-homepage',
			NORTHFIELD_URI . '/assets/js/homepage.js',
			array(),
			NORTHFIELD_VERSION,
			array( 'strategy' => 'defer' )
		);
	}

	// Conditionally load What's On assets.
	if ( is_page_template( 'page-whats-on.php' ) || is_page( 'whats-on' ) ) {
		wp_enqueue_style(
			'northfield-whats-on',
			NORTHFIELD_URI . '/assets/css/whats-on.css',
			array( 'northfield-main', 'northfield-event-card' ),
			NORTHFIELD_VERSION
		);

		wp_enqueue_script(
			'northfield-whats-on-filter',
			NORTHFIELD_URI . '/assets/js/whats-on-filter.js',
			array(),
			NORTHFIELD_VERSION,
			array( 'strategy' => 'defer' )
		);
	}

	// Conditionally load Single Event assets.
	if ( is_singular( 'nt_event' ) || is_page( 'events' ) ) {
		wp_enqueue_style(
			'northfield-single-event',
			NORTHFIELD_URI . '/assets/css/single-event.css',
			array( 'northfield-main', 'northfield-event-card' ),
			NORTHFIELD_VERSION
		);

		wp_enqueue_script(
			'northfield-single-event',
			NORTHFIELD_URI . '/assets/js/single-event.js',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION,
			array( 'strategy' => 'defer' )
		);
	}

	// Conditionally load Basket page assets.
	if ( is_page( 'basket' ) || is_page( 'checkout' ) ) {
		wp_enqueue_style(
			'northfield-basket',
			NORTHFIELD_URI . '/assets/css/basket.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);

		wp_enqueue_script(
			'northfield-basket',
			NORTHFIELD_URI . '/assets/js/basket.js',
			array(),
			NORTHFIELD_VERSION,
			array( 'strategy' => 'defer' )
		);
	}

	// Seating plan assets.
	if ( is_page_template( 'page-seating.php' ) || is_page( 'seating-plan' ) ) {
		wp_enqueue_style(
			'northfield-seating',
			NORTHFIELD_URI . '/assets/css/seating.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);

		wp_enqueue_script(
			'northfield-seating',
			NORTHFIELD_URI . '/assets/js/seating.js',
			array(),
			NORTHFIELD_VERSION,
			array( 'strategy' => 'defer' )
		);
	}

	// Our Story assets.
	if ( is_page_template( 'page-our-story.php' ) || is_page( 'our-story' ) ) {
		wp_enqueue_style(
			'northfield-our-story',
			NORTHFIELD_URI . '/assets/css/our-story.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);
	}

	// Contact page assets.
	if ( is_page_template( 'page-contact.php' ) || is_page( 'contact' ) ) {
		wp_enqueue_style(
			'northfield-contact',
			NORTHFIELD_URI . '/assets/css/contact.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);

		wp_enqueue_script(
			'northfield-contact-form',
			NORTHFIELD_URI . '/assets/js/contact-form.js',
			array(),
			NORTHFIELD_VERSION,
			array( 'strategy' => 'defer' )
		);
	}

	// Take Part page assets.
	if ( is_page_template( 'page-take-part.php' ) || is_page( 'take-part' ) ) {
		wp_enqueue_style(
			'northfield-take-part',
			NORTHFIELD_URI . '/assets/css/take-part.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);
	}

	// Hire page assets.
	if ( is_page_template( 'page-hire.php' ) || is_page( 'hire' ) ) {
		wp_enqueue_style(
			'northfield-hire',
			NORTHFIELD_URI . '/assets/css/hire.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);
	}

	// Moorside microsite assets.
	if ( is_page_template( 'page-moorside.php' ) || is_page( 'moorside' ) ) {
		wp_enqueue_style(
			'northfield-moorside',
			NORTHFIELD_URI . '/assets/css/moorside.css',
			array( 'northfield-main', 'northfield-event-card' ),
			NORTHFIELD_VERSION
		);
	}

	// News / blog assets (archive + single).
	if ( is_home() || is_singular( 'post' ) || is_archive() || is_search() ) {
		wp_enqueue_style(
			'northfield-news',
			NORTHFIELD_URI . '/assets/css/news.css',
			array( 'northfield-main' ),
			NORTHFIELD_VERSION
		);
	}

	wp_enqueue_script(
		'northfield-main',
		NORTHFIELD_URI . '/assets/js/main.js',
		array(),
		NORTHFIELD_VERSION,
		array( 'strategy' => 'defer' )
	);

	// Basket summary — runs on all pages to update header basket icon.
	wp_enqueue_script(
		'northfield-basket-summary',
		NORTHFIELD_URI . '/assets/js/basket-summary.js',
		array(),
		NORTHFIELD_VERSION,
		array( 'strategy' => 'defer' )
	);

	// Search autocomplete — drives the header search dropdown on all pages.
	wp_enqueue_script(
		'northfield-search-autocomplete',
		NORTHFIELD_URI . '/assets/js/search-autocomplete.js',
		array( 'northfield-main' ),
		NORTHFIELD_VERSION,
		array( 'strategy' => 'defer' )
	);

	// Cookie consent.
	wp_enqueue_style(
		'northfield-cookie-consent',
		NORTHFIELD_URI . '/assets/css/cookie-consent.css',
		array( 'northfield-main' ),
		NORTHFIELD_VERSION
	);

	// Elevation layer — MUST load last so it overrides earlier declarations.
	wp_enqueue_style(
		'northfield-elevate',
		NORTHFIELD_URI . '/assets/css/elevate.css',
		array( 'northfield-main', 'northfield-header', 'northfield-footer', 'northfield-event-card', 'northfield-pages', 'northfield-responsive' ),
		NORTHFIELD_VERSION
	);

	wp_enqueue_script(
		'northfield-cookie-consent',
		NORTHFIELD_URI . '/assets/js/cookie-consent.js',
		array(),
		NORTHFIELD_VERSION,
		array( 'strategy' => 'defer' )
	);

	wp_localize_script(
		'northfield-main',
		'northfieldData',
		array(
			'restUrl'  => esc_url_raw( rest_url( 'spektrix-mock/v1/' ) ),
			'nonce'    => wp_create_nonce( 'wp_rest' ),
			'themeUrl' => NORTHFIELD_URI,
			'siteUrl'  => home_url( '/' ),
		)
	);
}
add_action( 'wp_enqueue_scripts', 'northfield_scripts' );

function northfield_preload_fonts() {
	$font_dir = NORTHFIELD_URI . '/assets/fonts/';
	$fonts    = array(
		'inter-latin.woff2',
		'playfair-latin.woff2',
	);

	foreach ( $fonts as $font ) {
		printf(
			'<link rel="preload" href="%s" as="font" type="font/woff2" crossorigin>' . "\n",
			esc_url( $font_dir . $font )
		);
	}
}
add_action( 'wp_head', 'northfield_preload_fonts', 1 );
