<?php
/**
 * Single Course view (CPT: course) — renders the course detail page at
 * /courses/{slug}, mirroring brentwood.ca's course description: the title, its
 * taxonomy tags (subject / grade / category) plus the Advanced-Placement and
 * Elective flags, the description, and the "Ministry Credit Awarded" line.
 *
 * Like the staff/livestream views, this hooks the_content (priority 20) rather
 * than shipping a single-course.php template, so all of Kadence's chrome stays
 * intact. The body is the description, which we render ourselves, so we REPLACE
 * the content and suppress Kadence's own title (we render the H1).
 *
 * The pop-up modal used by the bw/course-table + bw/table-link blocks keeps its
 * own minimal body (title + description + credit) in inc/bw-course.php; this
 * single page is the richer, canonical view.
 *
 * No plugin changes, no DB writes.
 *
 * @package Kadence-Child
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Build the single-course markup: title, tag pills, description, credit line.
 *
 * @param WP_Post $post Course post.
 * @return string HTML (all values escaped).
 */
function bw_render_course( $post ) {
	$title  = get_the_title( $post );
	$body   = trim( (string) $post->post_content );
	$credit = trim( (string) get_post_meta( $post->ID, 'course_credit', true ) );
	$is_ap  = '1' === (string) get_post_meta( $post->ID, 'course_ap', true );
	$is_elv = '1' === (string) get_post_meta( $post->ID, 'course_elective', true );

	// Taxonomy tags, in display order: subject, grade (prefixed), category.
	$tags = array();
	foreach ( array(
		'course_subject'  => '',
		'course_grade'    => 'Grade ',
		'course_category' => '',
	) as $tax => $prefix ) {
		$terms = get_the_terms( $post, $tax );
		if ( $terms && ! is_wp_error( $terms ) ) {
			foreach ( $terms as $t ) {
				$tags[] = array( 'label' => $prefix . $t->name, 'mod' => '' );
			}
		}
	}
	// ACF flags as highlighted pills.
	if ( $is_ap ) {
		$tags[] = array( 'label' => __( 'Advanced Placement', 'kadence-child' ), 'mod' => ' bw-course__tag--ap' );
	}
	if ( $is_elv ) {
		$tags[] = array( 'label' => __( 'Elective', 'kadence-child' ), 'mod' => '' );
	}

	$out = '<section class="bw-course">';
	$out .= '<h1 class="bw-course__title">' . esc_html( $title ) . '</h1>';

	if ( $tags ) {
		$out .= '<div class="bw-course__tags">';
		foreach ( $tags as $tag ) {
			$out .= '<span class="bw-course__tag' . esc_attr( $tag['mod'] ) . '">' . esc_html( $tag['label'] ) . '</span>';
		}
		$out .= '</div>';
	}

	if ( '' !== $body ) {
		// wpautop + kses (not the_content) so we don't re-run do_blocks here.
		$out .= '<div class="bw-course__body">' . wp_kses_post( wpautop( $body ) ) . '</div>';
	}

	if ( '' !== $credit ) {
		$out .= '<div class="bw-course__credit">'
			. '<span class="bw-course__credit-label">' . esc_html__( 'Ministry Credit Awarded:', 'kadence-child' ) . '</span> '
			. '<span class="bw-course__credit-value">' . esc_html( $credit ) . '</span>'
			. '</div>';
	}

	$out .= '</section>';

	return $out;
}

/**
 * Replace the content on single course pages with the course view (the body is
 * the description, which bw_render_course already includes).
 */
add_filter(
	'the_content',
	function ( $content ) {
		if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
			return $content;
		}
		if ( ! is_singular( 'course' ) ) {
			return $content;
		}
		$post = get_post( get_queried_object_id() );
		if ( ! $post instanceof WP_Post ) {
			return $content;
		}
		// Hero is off by default on course (acf/load_value forces 'none'); renders
		// above the course view only when an editor sets one. '' when unset.
		return bw_render_hero( $post ) . bw_render_course( $post );
	},
	20
);

/**
 * Suppress Kadence's own entry title on single course pages — we render the
 * title as the H1. (single-entry.php gates the entry header on
 * show_in_content_title(); a non-'normal'/'above' title value disables both it
 * and the hero title.)
 */
add_filter(
	'kadence_post_layout',
	function ( $layout ) {
		if ( is_array( $layout ) && is_singular( 'course' ) ) {
			$layout['title'] = 'hide';
		}
		return $layout;
	}
);

/**
 * Make the "Course Layout" section appear in the Kadence Customizer.
 *
 * Kadence hardcodes a list of post-type slugs to skip in its Customizer
 * (get_post_types_to_ignore()) — these are LMS/forum slugs it handles with
 * dedicated sections. That list includes 'course' (LifterLMS's courses CPT
 * slug). Our migrated Courses CPT reuses the 'course' slug, so Kadence skips it
 * and no "Course Layout" section is generated — unlike staff/livestream/landing.
 *
 * LifterLMS is not installed here, so nothing else claims 'course'. Remove it
 * from Kadence's ignore list (via the provided filter) so Courses get the same
 * Customizer layout/archive controls as the other CPTs.
 */
add_filter(
	'kadence_customizer_post_type_ignore_array',
	function ( $ignore ) {
		return is_array( $ignore ) ? array_values( array_diff( $ignore, array( 'course' ) ) ) : $ignore;
	}
);

/**
 * Front-end stylesheet for the single course view.
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		if ( ! is_singular( 'course' ) ) {
			return;
		}
		$dir = get_stylesheet_directory();
		$uri = get_stylesheet_directory_uri();
		if ( file_exists( $dir . '/assets/course.css' ) ) {
			wp_enqueue_style(
				'bw-course',
				$uri . '/assets/course.css',
				array(),
				filemtime( $dir . '/assets/course.css' )
			);
		}
	}
);
