<?php
/**
 * File Path: templates/partials/section-author-hero.php
 *
 * My Book Showroom — Author Hero Section
 *
 * Renders the enhanced author hero above the book grid on
 * taxonomy-mbs-author.php when the site is licensed PRO. Contains:
 *
 *   1. Author photo banner (from mbs_author_photo_id)
 *   2. Author bio             (from mbs_author_bio, allows wp_kses_post HTML)
 *   3. Social links row       (website + social platform badges from mbs_author_social_links JSON)
 *
 * Every field respects GP-UI-1 (Golden Rule): a link/block only renders
 * when its corresponding term_meta value is non-empty. A fully-empty
 * author produces no hero at all — the caller does the PRO gate and an
 * "at least one field populated" guard upstream.
 *
 * Args expected from the caller:
 *   int $author_term_id — the mbs_author term ID whose hero to render.
 *
 * This partial is loaded via mbs_get_template_part( 'partials/section-author-hero',
 * array( 'author_term_id' => $term_id ) ). The helper uses a static closure
 * to project the args into local variables without extract().
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

if ( ! mbs_is_registered() ) {
	return;
}

if ( ! isset( $author_term_id ) || ! $author_term_id ) {
	return;
}

$author_term_id = (int) $author_term_id;

// ── Gather fields ────────────────────────────────────────────────────────────

$photo_id  = (int) get_term_meta( $author_term_id, 'mbs_author_photo_id', true );
$photo_url = $photo_id ? wp_get_attachment_image_url( $photo_id, 'large' ) : ''; // R5-exempt: author profile photo (mbs_author_photo_id), not a book cover

$bio       = (string) get_term_meta( $author_term_id, 'mbs_author_bio', true );
if ( '' === $bio ) {
	$term_obj = get_term( $author_term_id );
	if ( $term_obj instanceof WP_Term && '' !== trim( $term_obj->description ) ) {
		$bio = $term_obj->description;
	}
}
$website   = (string) get_term_meta( $author_term_id, 'mbs_author_website_url', true );

// Social platforms — Sprint SOCIAL-LINKS: read from JSON blob via decode_social_links().
// Website is rendered separately (semantically distinct). Display order = user-added order.
$decoded_links = MBS_Author_Social_Links::decode_social_links( $author_term_id );
$active_socials = array();
foreach ( $decoded_links as $entry ) {
	$slug = sanitize_key( $entry['platform'] ?? '' );
	$url  = (string) ( $entry['url'] ?? '' );
	if ( '' === $slug || '' === $url ) {
		continue;
	}
	$pinfo = MBS_Author_Social_Links::get_platform_info( $slug );
	$label = ( 'custom' === $slug && ! empty( $entry['label'] ) )
		? sanitize_text_field( $entry['label'] )
		: $pinfo['label'];
	$active_socials[] = array(
		'slug'  => $slug,
		'label' => $label,
		'url'   => $url,
		'abbr'  => $pinfo['abbr'],
	);
}

$has_photo   = ( '' !== $photo_url );
$has_bio     = ( '' !== trim( wp_strip_all_tags( $bio ) ) );
$has_website = ( '' !== $website );
$has_socials = ! empty( $active_socials );

// If literally nothing to show, render nothing — the caller already decided
// this is PRO; we decide whether the hero has any content worth a container.
if ( ! $has_photo && ! $has_bio && ! $has_website && ! $has_socials ) {
	return;
}
?>

<section class="mbs-author-hero" aria-label="<?php esc_attr_e( 'About the author', 'my-book-showroom' ); ?>">

	<?php if ( $has_photo ) : ?>
		<div class="mbs-author-hero-photo-wrap">
			<img class="mbs-author-hero-photo"
			     src="<?php echo esc_url( $photo_url ); ?>"
			     alt=""
			     loading="lazy">
		</div>
	<?php else : ?>
		<?php
		$_mbs_author_term = get_term( $author_term_id );
		$_mbs_initial     = ( $_mbs_author_term instanceof WP_Term )
			? mb_strtoupper( mb_substr( $_mbs_author_term->name, 0, 1 ) )
			: '?';
		?>
		<div class="mbs-author-hero-photo-wrap mbs-author-hero-photo-placeholder">
			<svg class="mbs-author-hero-placeholder-svg"
			     xmlns="http://www.w3.org/2000/svg"
			     viewBox="0 0 120 120"
			     role="img"
			     aria-label="<?php esc_attr_e( 'Author photo placeholder', 'my-book-showroom' ); ?>">
				<circle cx="60" cy="60" r="60" fill="var(--mbs-accent, #4AABB8)" />
				<text x="60" y="60"
				      dy="0.35em"
				      text-anchor="middle"
				      font-family="sans-serif"
				      font-size="52"
				      font-weight="700"
				      fill="#ffffff"><?php echo esc_html( $_mbs_initial ); ?></text>
			</svg>
		</div>
	<?php endif; ?>

	<div class="mbs-author-hero-body">

		<?php if ( $has_bio ) : ?>
			<div class="mbs-author-hero-bio">
				<?php echo wp_kses_post( wpautop( $bio ) ); ?>
			</div>
		<?php endif; ?>

		<?php if ( $has_website || $has_socials ) : ?>
			<nav class="mbs-author-social-links"
			     aria-label="<?php esc_attr_e( 'Author links', 'my-book-showroom' ); ?>">
				<?php if ( $has_website ) : ?>
					<a class="mbs-author-social-link mbs-author-social-link-website"
					   href="<?php echo esc_url( $website ); ?>"
					   target="_blank"
					   rel="noopener noreferrer">
						<?php esc_html_e( 'Website', 'my-book-showroom' ); ?>
					</a>
				<?php endif; ?>
				<?php foreach ( $active_socials as $slink ) : ?>
					<a class="mbs-author-social-link mbs-author-social-link-<?php echo esc_attr( $slink['slug'] ); ?>"
					   href="<?php echo esc_url( $slink['url'] ); ?>"
					   target="_blank"
					   rel="noopener noreferrer">
						<span class="mbs-social-badge mbs-social-badge-<?php echo esc_attr( $slink['slug'] ); ?>" aria-hidden="true"><?php echo esc_html( $slink['abbr'] ); ?></span>
						<span class="mbs-social-badge-label"><?php echo esc_html( $slink['label'] ); ?></span>
					</a>
				<?php endforeach; ?>
			</nav>
		<?php endif; ?>

	</div>

</section>
