<?php
/**
 * Related Kurse Template
 *
 * Displays related kurse cards at the bottom of single kurs pages.
 *
 * @package Kurs_Booking
 */

defined( 'ABSPATH' ) || exit;

$related_kurse = $related_kurse ?? array();
$current_id    = $current_id ?? 0;

if ( empty( $related_kurse ) ) {
	return;
}

// Get module settings.
$modules             = get_option( 'kurs_booking_modules', array() );
$show_category       = isset( $modules['show_category'] ) ? (bool) $modules['show_category'] : true;
$related_kurse_title = ! empty( $modules['related_kurse_title'] ) ? $modules['related_kurse_title'] : __( 'Ähnliche Kurse', 'kurs-booking' );
?>

<section class="kb-related-kurse">
	<h2 class="kb-related-kurse__title">
		<?php echo esc_html( $related_kurse_title ); ?>
	</h2>

	<div class="kb-cards-grid kb-cards-grid--3-cols">
		<?php
		foreach ( $related_kurse as $kurs ) :
			$kurs_id = $kurs->ID;

			// Get meta data.
			$start_date  = Kurs_Booking_Kurs::get_meta( $kurs_id, 'start_date', '' );
			$end_date    = Kurs_Booking_Kurs::get_meta( $kurs_id, 'end_date', '' );
			$produktart  = Kurs_Booking_Kurs::get_meta( $kurs_id, 'produktart', 'kurs' );
			$varianten   = Kurs_Booking_Kurs::get_varianten( $kurs_id );

			// Format date.
			$date_display = '';
			if ( $start_date ) {
				$start_ts = strtotime( $start_date );
				$end_ts   = $end_date ? strtotime( $end_date ) : $start_ts;

				if ( $start_date === $end_date || ! $end_date ) {
					$date_display = wp_date( 'j. M Y', $start_ts );
				} else {
					$date_display = wp_date( 'j.', $start_ts ) . '-' . wp_date( 'j. M Y', $end_ts );
				}
			} elseif ( 'videokurs' === $produktart ) {
				$date_display = __( 'Sofort verfuegbar', 'kurs-booking' );
			}

			// Get category.
			$terms         = get_the_terms( $kurs_id, 'kurs_category' );
			$category_name = '';
			$category_url  = '';
			if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
				$category_name = $terms[0]->name;
				$category_url  = get_term_link( $terms[0] );
				if ( is_wp_error( $category_url ) ) {
					$category_url = '';
				}
			}

			// Check availability.
			$max_capacity = (int) Kurs_Booking_Kurs::get_meta( $kurs_id, 'max_participants', 0 );
			$booked_count = Kurs_Booking_Kurs::get_bookings_count( $kurs_id );
			$is_sold_out  = $max_capacity > 0 && $booked_count >= $max_capacity;

			// Produktart labels.
			$produktart_labels = array(
				'kurs'            => __( 'Kurs', 'kurs-booking' ),
				'workshop'        => __( 'Workshop', 'kurs-booking' ),
				'webinar'         => __( 'Webinar', 'kurs-booking' ),
				'gruppencoaching' => __( 'Gruppencoaching', 'kurs-booking' ),
				'einzelcoaching'  => __( 'Einzelcoaching', 'kurs-booking' ),
				'videokurs'       => __( 'Video-Kurs', 'kurs-booking' ),
			);
			$produktart_label = $produktart_labels[ $produktart ] ?? $produktart_labels['kurs'];

			// Price calculation.
			$min_price = PHP_INT_MAX;
			$max_price = 0;
			if ( ! empty( $varianten ) ) {
				foreach ( $varianten as $variante ) {
					$price = floatval( $variante['price'] ?? 0 );
					if ( $price > 0 ) {
						$min_price = min( $min_price, $price );
						$max_price = max( $max_price, $price );
					}
				}
			}
			if ( PHP_INT_MAX === $min_price ) {
				$min_price = 0;
			}

			$price_display = '';
			if ( $min_price > 0 ) {
				if ( $min_price === $max_price ) {
					$price_display = number_format( $min_price, 0, ',', '.' ) . ' EUR';
				} else {
					$price_display = 'ab ' . number_format( $min_price, 0, ',', '.' ) . ' EUR';
				}
			}

			// Featured image.
			$thumbnail = '';
			if ( has_post_thumbnail( $kurs_id ) ) {
				$thumbnail = get_the_post_thumbnail_url( $kurs_id, 'medium_large' );
			}
			?>

			<article class="kb-card">
				<?php if ( $thumbnail ) : ?>
					<div class="kb-card__image">
						<a href="<?php echo esc_url( get_permalink( $kurs_id ) ); ?>">
							<img src="<?php echo esc_url( $thumbnail ); ?>"
								 alt="<?php echo esc_attr( get_the_title( $kurs_id ) ); ?>"
								 loading="lazy">
						</a>

						<div class="kb-card__badges">
							<?php if ( $is_sold_out ) : ?>
								<span class="kb-card__badge kb-card__badge--soldout">
									<?php esc_html_e( 'Ausgebucht', 'kurs-booking' ); ?>
								</span>
							<?php endif; ?>

							<span class="kb-card__type-badge kb-card__type-badge--<?php echo esc_attr( $produktart ); ?>">
								<?php echo esc_html( $produktart_label ); ?>
							</span>
						</div>
					</div>
				<?php endif; ?>

				<div class="kb-card__content">
					<?php if ( $show_category && ! empty( $category_name ) ) : ?>
						<?php if ( ! empty( $category_url ) ) : ?>
							<a href="<?php echo esc_url( $category_url ); ?>" class="kb-card__category"><?php echo esc_html( $category_name ); ?></a>
						<?php else : ?>
							<span class="kb-card__category"><?php echo esc_html( $category_name ); ?></span>
						<?php endif; ?>
					<?php endif; ?>

					<h3 class="kb-card__title">
						<a href="<?php echo esc_url( get_permalink( $kurs_id ) ); ?>"><?php echo esc_html( get_the_title( $kurs_id ) ); ?></a>
					</h3>

					<div class="kb-card__meta">
						<?php if ( ! empty( $date_display ) ) : ?>
							<div class="kb-card__meta-item">
								<span class="dashicons dashicons-calendar-alt"></span>
								<?php echo esc_html( $date_display ); ?>
							</div>
						<?php endif; ?>
					</div>

					<div class="kb-card__footer">
						<?php if ( $price_display ) : ?>
							<span class="kb-card__price"><?php echo esc_html( $price_display ); ?></span>
						<?php endif; ?>

						<a href="<?php echo esc_url( get_permalink( $kurs_id ) ); ?>" class="kb-card__btn">
							<?php esc_html_e( 'Details', 'kurs-booking' ); ?>
							<span class="dashicons dashicons-arrow-right-alt2"></span>
						</a>
					</div>
				</div>
			</article>

		<?php endforeach; ?>
	</div>
</section>
