<?php
/**
 * Single Staff view (CPT: staff) — renders the faculty profile on its own page
 * at /staff/{slug}, reusing the SAME markup as the pop-up modal so the two can
 * never drift apart. The shared renderer (bw_staff_profile_html) lives in
 * inc/bw-staff-profile.php and is also used by the bw/team-modal and
 * bw/instructor blocks (the listing grids whose tiles open the modal).
 *
 * Like the livestream/hero views, this hooks the_content (priority 20, after
 * do_blocks/wpautop) rather than shipping a single-staff.php template, so all of
 * Kadence's header/footer/layout chrome stays intact. The profile markup already
 * contains the name, role, credentials, photo and bio, so we REPLACE the content
 * (the post body is the bio — rendering both would duplicate it) and suppress
 * Kadence's own title + featured image.
 *
 * No plugin changes, no DB writes.
 *
 * @package Kadence-Child
 */

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

require_once get_stylesheet_directory() . '/inc/bw-staff-profile.php';

/**
 * Two-column single-staff layout aligned to the 33% red accent line:
 *   • LEFT (≈33%, left of the line): the featured photo, as a widget.
 *   • RIGHT (≈67%, right of the line, padded): the H1 name (with its underline),
 *     role, credentials and bio, closed by the shared shield footer.
 *
 * This is a page-specific layout (the modal/grid blocks keep the floated-photo
 * layout in bw_staff_profile_html); it still uses the team-modal text classes
 * (.bw-tm__p-name / -role / -cred / -bio, .bw-tm__footer) so the typography and
 * footer stay identical to the modal.
 *
 * @param WP_Post $post Staff post.
 * @return string HTML (all values escaped).
 */
function bw_render_staff( $post ) {
	$name = get_the_title( $post );
	$role = trim( (string) get_post_meta( $post->ID, 'staff_position', true ) );
	$cred = trim( (string) get_post_meta( $post->ID, 'staff_credentials', true ) );
	$bio  = trim( (string) $post->post_content );

	// Mortarboard icon + Brentwood shield — same assets the modal uses.
	$cap_icon = '<svg class="bw-tm__cap" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">'
		. '<path d="M12 3 1 9l11 6 9-4.91V17h2V9L12 3zM5 13.18v3.82c0 .73 3.13 3 7 3s7-2.27 7-3v-3.82l-7 3.82-7-3.82z"/></svg>';
	$shield = get_stylesheet_directory_uri() . '/blocks/team-modal/images/icon_gray.svg';

	$out = '<section class="bw-staff">';

	// Left widget: featured photo.
	if ( has_post_thumbnail( $post ) ) {
		$out .= '<div class="bw-staff__photo">'
			// 'large', not 'medium': `medium` is only 225x300 for a 3:4 portrait
			// and visibly pixelates on 2x/3x displays.
			. get_the_post_thumbnail( $post, 'large', array( 'class' => 'bw-staff__img', 'loading' => 'lazy' ) )
			. '</div>';
	}

	// Right column: title, role, credentials, bio, footer.
	$out .= '<div class="bw-staff__body">';
	$out .= '<h1 class="bw-tm__p-name">' . esc_html( $name ) . '</h1>';
	if ( '' !== $role ) {
		$out .= '<p class="bw-tm__p-role">' . esc_html( $role ) . '</p>';
	}
	if ( '' !== $cred ) {
		$out .= '<p class="bw-tm__p-cred">' . $cap_icon . '<span>' . esc_html( $cred ) . '</span></p>';
	}
	$out .= '<div class="bw-tm__p-bio">' . wp_kses_post( wpautop( $bio ) ) . '</div>';
	$out .= '<div class="bw-tm__footer"><span class="bw-tm__rule"></span>'
		. '<img class="bw-tm__shield" src="' . esc_url( $shield ) . '" alt="Brentwood Shield" /></div>';
	$out .= '</div>'; // .bw-staff__body

	$out .= '</section>';

	return $out;
}

/**
 * Replace the content on single staff pages with the profile view. We replace
 * (not prepend) because bw_staff_profile_html already renders the bio from the
 * post body — appending $content would show the bio twice.
 */
add_filter(
	'the_content',
	function ( $content ) {
		if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
			return $content;
		}
		if ( ! is_singular( 'staff' ) ) {
			return $content;
		}
		$post = get_post( get_queried_object_id() );
		if ( ! $post instanceof WP_Post ) {
			return $content;
		}
		// Hero is off by default on staff (acf/load_value forces 'none'); when an
		// editor explicitly sets one it renders above the profile. bw_render_hero
		// returns '' when unset, so the default case is unchanged.
		return bw_render_hero( $post ) . bw_render_staff( $post );
	},
	20
);

/**
 * Suppress Kadence's own entry title AND featured-image area on single staff
 * pages — the profile renders the name (as the h1) and the photo (floated)
 * itself. We do this through the layout filter, NOT post_thumbnail_html:
 * - 'title' set to a value that is neither 'normal' nor 'above' makes both
 *   show_in_content_title() and show_hero_title() false (single-entry.php gates
 *   the entry header on show_in_content_title()).
 * - 'feature' set to anything but 'show' makes show_feature_above()/_below()
 *   false, so Kadence skips entry_thumbnail. Doing it here (rather than
 *   filtering post_thumbnail_html) leaves get_the_post_thumbnail() working
 *   inside the profile, so the floated photo still renders.
 */
add_filter(
	'kadence_post_layout',
	function ( $layout ) {
		if ( is_array( $layout ) && is_singular( 'staff' ) ) {
			$layout['title']   = 'hide';
			$layout['feature'] = 'hide';
		}
		return $layout;
	}
);

/**
 * Front-end assets for the single staff view. The profile markup reuses the
 * team-modal stylesheet (`.bw-tm__*`, registered on init); we enqueue it here
 * (it's normally only auto-loaded when the block is on the page) plus a small
 * stylesheet for the page wrapper.
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		if ( ! is_singular( 'staff' ) ) {
			return;
		}
		if ( wp_style_is( 'bw-team-modal', 'registered' ) ) {
			wp_enqueue_style( 'bw-team-modal' );
		}
		$dir = get_stylesheet_directory();
		$uri = get_stylesheet_directory_uri();
		if ( file_exists( $dir . '/assets/staff.css' ) ) {
			wp_enqueue_style(
				'bw-staff',
				$uri . '/assets/staff.css',
				array( 'bw-team-modal' ),
				filemtime( $dir . '/assets/staff.css' )
			);
		}
	}
);
