<?php
/**
 * Reusable event card component.
 *
 * Displays an event as a card with image, category badge, title, dates,
 * price range, and availability. Used on Homepage, What's On, and search.
 *
 * Usage:
 *   get_template_part( 'template-parts/content', 'event-card', array(
 *       'event'   => $event_data,      // Required: event array from JSON
 *       'variant' => 'default',        // Optional: 'default', 'compact', 'featured'
 *   ) );
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

if ( ! isset( $args['event'] ) ) {
	return;
}

$event   = $args['event'];
$variant = isset( $args['variant'] ) ? $args['variant'] : 'default';

// Compute display values.
$event_url    = home_url( '/events/' . $event['slug'] . '/' );
$first_date   = northfield_get_event_first_date( $event['id'] );
$last_date    = northfield_get_event_last_date( $event['id'] );
$date_display = northfield_format_event_date_range( $first_date, $last_date );
$price_display = northfield_format_event_price_range( $event );

// Category-based gradient for when no image is available.
$category_gradients = array(
	'Drama'  => 'linear-gradient(135deg, #4A1942 0%, #1B4D3E 100%)',
	'Comedy' => 'linear-gradient(135deg, #92400E 0%, #E8A33D 100%)',
	'Music'  => 'linear-gradient(135deg, #312E81 0%, #6D28D9 100%)',
	'Dance'  => 'linear-gradient(135deg, #164E63 0%, #0891B2 100%)',
	'Family' => 'linear-gradient(135deg, #14532D 0%, #22C55E 100%)',
	'Other'  => 'linear-gradient(135deg, #78350F 0%, #D97706 100%)',
);

$gradient    = isset( $category_gradients[ $event['category'] ] ) ? $category_gradients[ $event['category'] ] : $category_gradients['Other'];
$has_image   = ! empty( $event['thumbnail'] ) && file_exists( NORTHFIELD_DIR . '/assets/images/' . $event['thumbnail'] );
$image_style = $has_image
	? 'background-image: url(' . esc_url( NORTHFIELD_URI . '/assets/images/' . $event['thumbnail'] ) . ')'
	: 'background: ' . $gradient;

// Running time display.
$run_time_display = '';
if ( ! empty( $event['runTime'] ) ) {
	$hours   = floor( $event['runTime'] / 60 );
	$minutes = $event['runTime'] % 60;
	if ( $hours > 0 && $minutes > 0 ) {
		$run_time_display = sprintf( '%dh %dm', $hours, $minutes );
	} elseif ( $hours > 0 ) {
		$run_time_display = sprintf( '%dh', $hours );
	} else {
		$run_time_display = sprintf( '%dm', $minutes );
	}
}
?>
<article class="event-card event-card--<?php echo esc_attr( $variant ); ?> hover-lift fade-in-up">
	<a href="<?php echo esc_url( $event_url ); ?>" class="event-card__link" aria-label="<?php echo esc_attr( $event['name'] ); ?>">
		<div class="event-card__image img-zoom" style="<?php echo esc_attr( $image_style ); ?>">
			<?php if ( $has_image ) : ?>
				<img src="<?php echo esc_url( NORTHFIELD_URI . '/assets/images/' . $event['thumbnail'] ); ?>"
				     alt=""
				     loading="lazy"
				     decoding="async" />
			<?php endif; ?>
			<span class="event-card__category badge badge--category">
				<?php echo esc_html( $event['category'] ); ?>
			</span>
			<?php
			$badge = northfield_get_event_badge( $event );
			if ( $badge ) : ?>
				<span class="event-card__availability badge badge--<?php echo esc_attr( $badge['variant'] ); ?>">
					<?php echo esc_html( $badge['label'] ); ?>
				</span>
			<?php endif; ?>
		</div>
		<div class="event-card__body">
			<h3 class="event-card__title"><?php echo esc_html( $event['name'] ); ?></h3>
			<?php if ( $date_display ) : ?>
				<p class="event-card__date">
					<?php echo esc_html( $date_display ); ?>
				</p>
			<?php endif; ?>
			<?php if ( 'compact' !== $variant ) : ?>
				<p class="event-card__summary">
					<?php echo esc_html( $event['summary'] ); ?>
				</p>
			<?php endif; ?>
			<div class="event-card__meta">
				<?php if ( $price_display ) : ?>
					<span class="event-card__price"><?php echo esc_html( $price_display ); ?></span>
				<?php endif; ?>
				<?php if ( $run_time_display && 'compact' !== $variant ) : ?>
					<span class="event-card__runtime"><?php echo esc_html( $run_time_display ); ?></span>
				<?php endif; ?>
			</div>
			<span class="event-card__cta btn btn--outline btn--sm">
				<?php esc_html_e( 'More info', 'northfield-theatre' ); ?>
			</span>
		</div>
	</a>
</article>
