<?php
/**
 * Utility functions used throughout the theme.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

/**
 * Return the SVG markup for a given icon name.
 *
 * @param string $name  Icon identifier.
 * @param int    $size  Icon size in pixels (square).
 * @param string $class Optional extra CSS class.
 * @return string SVG markup.
 */
function northfield_icon( $name, $size = 24, $class = '' ) {
	$aria  = 'aria-hidden="true" focusable="false"';
	$attrs = sprintf(
		'width="%1$d" height="%1$d" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" %2$s',
		(int) $size,
		$aria
	);

	if ( $class ) {
		$attrs .= sprintf( ' class="%s"', esc_attr( $class ) );
	}

	$icons = array(
		'search'   => '<circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/>',
		'basket'   => '<path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/>',
		'user'     => '<path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/>',
		'menu'     => '<line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="18" x2="21" y2="18"/>',
		'close'    => '<line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>',
		'facebook' => '<path d="M18 2h-3a5 5 0 00-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 011-1h3z"/>',
		'instagram' => '<rect x="2" y="2" width="20" height="20" rx="5" ry="5"/><path d="M16 11.37A4 4 0 1112.63 8 4 4 0 0116 11.37z"/><line x1="17.5" y1="6.5" x2="17.51" y2="6.5"/>',
		'twitter'  => '<path d="M22 4s-.7 2.1-2 3.4c1.6 10-9.4 17.3-18 11.6 2.2.1 4.4-.6 6-2C3 15.5.5 9.6 3 5c2.2 2.6 5.6 4.1 9 4-.9-4.2 4-6.6 7-3.8 1.1 0 3-1.2 3-1.2z"/>',
		'youtube'  => '<path d="M22.54 6.42a2.78 2.78 0 00-1.94-2C18.88 4 12 4 12 4s-6.88 0-8.6.46a2.78 2.78 0 00-1.94 2A29 29 0 001 11.75a29 29 0 00.46 5.33A2.78 2.78 0 003.4 19.1c1.72.46 8.6.46 8.6.46s6.88 0 8.6-.46a2.78 2.78 0 001.94-2 29 29 0 00.46-5.25 29 29 0 00-.46-5.33z"/><polygon points="9.75 15.02 15.5 11.75 9.75 8.48 9.75 15.02"/>',
		'chevron-down' => '<polyline points="6 9 12 15 18 9"/>',
		'x-close'  => '<path d="M18 6L6 18M6 6l12 12"/>',
		'mail'     => '<path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/>',
		'phone'    => '<path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72 12.84 12.84 0 00.7 2.81 2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45 12.84 12.84 0 002.81.7A2 2 0 0122 16.92z"/>',
		'alert'    => '<path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/>',
	);

	if ( ! isset( $icons[ $name ] ) ) {
		return '';
	}

	return sprintf( '<svg %s>%s</svg>', $attrs, $icons[ $name ] );
}

/**
 * Format a price in GBP.
 *
 * @param float $amount The price amount.
 * @return string Formatted price string.
 */
function northfield_format_price( $amount ) {
	return '£' . number_format( (float) $amount, 2 );
}

/**
 * Get a human-readable date string.
 *
 * @param string $datetime ISO 8601 date string.
 * @param string $format   PHP date format.
 * @return string Formatted date.
 */
function northfield_format_date( $datetime, $format = 'D j M Y' ) {
	$date = new DateTime( $datetime );
	return $date->format( $format );
}

/**
 * Get a human-readable time string.
 *
 * @param string $datetime ISO 8601 date string.
 * @return string Formatted time (e.g. "7:30pm").
 */
function northfield_format_time( $datetime ) {
	$date = new DateTime( $datetime );
	return $date->format( 'g:ia' );
}

/**
 * Map a blog-post slug to a relevant event hero image.
 *
 * Posts don't have WP featured images in the demo — this gives the news
 * archive real photography instead of placeholder letterforms.
 *
 * @param WP_Post|int $post Post or post ID.
 * @return string URL to an image in assets/images/, or empty string if none.
 */
function northfield_get_post_illustration_url( $post ) {
	$post = get_post( $post );
	if ( ! $post ) {
		return '';
	}

	// Prefer an actual featured image if one has been set.
	$thumb_id = get_post_thumbnail_id( $post );
	if ( $thumb_id ) {
		return wp_get_attachment_image_url( $thumb_id, 'large' );
	}

	$slug_map = array(
		'announcing-our-2026-27-season'       => 'midsummer-hero.jpg',
		'in-conversation-with-marcus-okonkwo' => 'hamlet-hero.jpg',
		'access-programme-spring-summer-2026' => 'swan-lake-hero.jpg',
		'review-midsummer-nights-dream'       => 'midsummer-hero.jpg',
		'meet-the-studio-new-writing-cohort'  => 'glass-menagerie-hero.jpg',
	);

	$file = $slug_map[ $post->post_name ] ?? 'jazz-foyer-hero.jpg';
	$path = NORTHFIELD_DIR . '/assets/images/' . $file;
	if ( ! file_exists( $path ) ) {
		return '';
	}

	return NORTHFIELD_URI . '/assets/images/' . $file;
}
