<?php
/**
 * File Path: templates/single-mbs-book.php
 *
 * Single Book Template
 *
 * Renders the full book page with a two-column hero header (cover + details),
 * a full-width buy-links zone, a conditional external-links zone, tab navigation,
 * and all section/tab content panels.
 *
 * Hero column structure (COVER-001):
 *   Left  -- .mbs-book-hero-cover  : book cover image
 *   Right -- .mbs-book-hero-details: title, author (linked to taxonomy),
 *                                    badges, teaser, format tabs,
 *                                    per-format meta, social share
 *
 * Below hero (full-width):
 *   .mbs-buy-links-zone     : retailer buy buttons (format-switchable via JS)
 *   .mbs-external-links-zone: Goodreads / StoryGraph / BookBub (conditional)
 *
 * @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() ) {
	echo '<!-- MBS: License required to view book content -->';
	return;
}

get_header();

$post_id = get_the_ID();
$authors = wp_get_post_terms( $post_id, MBS_TAX_AUTHOR );
?>

<div class="mbs-wrapper">
<div class="mbs-single-book">

	<?php mbs_get_template_part( 'partials/breadcrumbs' ); ?>

	<div class="mbs-book-hero">

		<div class="mbs-book-hero-cover">
			<?php mbs_get_template_part( 'partials/cover' ); ?>
		</div>

		<div class="mbs-book-hero-details">

			<h1 class="mbs-book-title"><?php echo esc_html( get_the_title() ); ?></h1>

			<?php if ( ! is_wp_error( $authors ) && ! empty( $authors ) ) : ?>
			<p class="mbs-book-author">
				<?php esc_html_e( 'By', 'my-book-showroom' ); ?>
				<?php
				$_mbs_author_links = array();
				foreach ( $authors as $_mbs_author ) {
					$_mbs_author_url = get_term_link( $_mbs_author );
					if ( is_wp_error( $_mbs_author_url ) ) {
						$_mbs_author_links[] = esc_html( $_mbs_author->name );
					} else {
						$_mbs_author_links[] = '<a href="' . esc_url( $_mbs_author_url ) . '">'
										. esc_html( $_mbs_author->name ) . '</a>';
					}
				}
				echo implode( ', ', $_mbs_author_links ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each link individually escaped above
				?>
			</p>
			<?php endif; ?>

			<?php mbs_get_template_part( 'partials/coming-soon-badge' ); ?>
			<?php mbs_get_template_part( 'partials/bestseller-badge' ); // DISPLAY-2 ?>
			<?php mbs_get_template_part( 'partials/lifecycle-state-label' ); ?>

			<?php
			// FE-012: Tagline (mbs_teaser / Short Description) is always rendered
			// independently in the hero when non-empty — it is not a fallback for synopsis.
			$_mbs_tagline = trim( (string) get_post_meta( $post_id, 'mbs_teaser', true ) );
			if ( '' !== $_mbs_tagline ) :
			?>
			<div class="mbs-teaser"><?php echo wp_kses_post( wpautop( $_mbs_tagline ) ); ?></div>
			<?php endif; ?>
			<?php
			// Back Cover / Flap Copy (mbs_synopsis, PRO) — rendered below tagline when set.
			if ( mbs_is_pro() ) :
				$_mbs_synopsis = trim( (string) get_post_meta( $post_id, 'mbs_synopsis', true ) );
				if ( '' !== $_mbs_synopsis ) :
			?>
			<div class="mbs-synopsis"><?php echo wp_kses_post( wpautop( $_mbs_synopsis ) ); ?></div>
			<?php
				endif;
			endif;
			?>

			<?php mbs_get_template_part( 'partials/format-selector' ); ?>
			<?php mbs_get_template_part( 'partials/per-format-details' ); ?>
			<?php mbs_get_template_part( 'partials/section-social-share', array( 'is_hero' => true ) ); ?>

		</div><!-- /.mbs-book-hero-details -->

	</div><!-- /.mbs-book-hero -->

	<?php
	// Buy Links zone -- full-width below the two-column hero.
	// The #mbs-buy-links container ID inside buy-links.php is required by
	// mbs-format-tabs.js / MBS.BuyLinks for format-switch rebuilding.
	?>
	<div class="mbs-buy-links-zone">
		<?php mbs_get_template_part( 'partials/buy-links' ); ?>
	</div>

	<?php
	// External links zone -- Reader Platforms (Goodreads, StoryGraph, BookBub, etc.).
	// section-reader-platforms.php self-suppresses (GP-UI-1) when no platform URLs are set.
	?>
	<div class="mbs-external-links-zone">
		<?php mbs_get_template_part( 'partials/section-reader-platforms' ); ?>
	</div>

	<?php
	// Section-ordered content -- visible sections only (DAO-filtered by is_visible).
	// reader-platforms  -- rendered in the external-links zone above.
	// social-share      -- rendered in hero (UX-004/UX-005); skip in section loop.
	// series-order      -- rendered by section-series-order.php via the section loop.
	$order             = MBS_Section_Order::get_order( get_the_ID() );
	$available_keys    = array_keys( MBS_Section_Helper::get_available_section_data() );
	$mbs_skip_sections = array( 'social-share', 'suggested-reading-order', 'goodreads' );
	foreach ( $order as $section ) {
		if ( in_array( $section, $mbs_skip_sections, true ) ) {
			continue;
		}
		if ( ! in_array( $section, $available_keys, true ) ) {
			continue; // Filtered by requires_option (e.g. Universe disabled).
		}
		mbs_get_template_part( 'partials/section-' . $section );
	}
	?>

	<?php
	// GC3-7: Newsletter / after-display hook.
	echo apply_filters( 'mbs_after_book_display', '', get_the_ID() ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound, WordPress.Security.EscapeOutput.OutputNotEscaped -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix
	?>

	<?php
	// INSIGHTS-3: Press Kit public download button (PRO + public option enabled).
	if ( mbs_is_pro() && (int) get_option( 'mbs_press_kit_public', 0 ) ) :
		$_mbs_pk_url = add_query_arg(
			array(
				'action'  => 'mbs_generate_press_kit',
				'post_id' => get_the_ID(),
				'nonce'   => wp_create_nonce( 'mbs_generate_press_kit_' . get_the_ID() ),
			),
			admin_url( 'admin-ajax.php' )
		);
		?>
		<div class="mbs-press-kit-wrap">
			<a href="<?php echo esc_url( $_mbs_pk_url ); ?>"
			   class="mbs-press-kit-download-btn"
			   download>
				<?php esc_html_e( 'Download Press Kit', 'my-book-showroom' ); ?>
			</a>
		</div>
	<?php endif; ?>

</div><!-- /.mbs-single-book -->
</div><!-- /.mbs-wrapper -->

<?php mbs_get_template_part( 'partials/power-badge' ); ?>

<?php get_footer(); ?>
