<?php
/**
 * File Path: templates/single-mbs-book-landing.php
 *
 * Book Landing Page Template — My Book Showroom
 *
 * Conversion-optimized single-book layout with a large cover hero and
 * prominent buy buttons. Activated per-book via `mbs_landing_page_mode` postmeta.
 *
 * Tier: PRO (template_loader gate enforces this — MBS_Book_Display::template_loader()
 * only routes here when mbs_is_pro() and mbs_landing_page_mode === '1').
 *
 * Theme override support (same as single-mbs-book.php):
 *   child theme  → my-book-showroom/single-mbs-book-landing.php
 *   parent theme → my-book-showroom/single-mbs-book-landing.php
 *   plugin       → templates/single-mbs-book-landing.php  (this file)
 *
 * All output is scoped to `.mbs-landing-wrapper` to prevent CSS bleed
 * into the theme's regular single-book template. CSS lives in mbs-front.css
 * under the "S14-8 — Book Landing Page" CSS section.
 *
 * Helpers used:
 *   MBS_Cover_Renderer::render()             — featured image with lazy-load
 *   MBS_Buy_Link_Renderer::render_order_now() — buy buttons / retailer list
 *   mbs_get_template_part()                   — MBS template loader
 *
 * Story 14-8 — Sprint 14.
 *
 * @package    My_Book_Showroom
 * @subpackage Templates
 * @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() ) {
	get_header();
	echo '<p>' . esc_html__( 'This feature requires a registered license.', 'my-book-showroom' ) . '</p>';
	get_footer();
	exit;
}

get_header();

while ( have_posts() ) :
	the_post();
	$post_id     = get_the_ID();
	$book_title  = get_the_title();
	$book_excerpt = get_the_excerpt();
	$authors     = get_the_terms( $post_id, MBS_TAX_AUTHOR );
	$author_name = '';
	if ( $authors && ! is_wp_error( $authors ) ) {
		$author_names = wp_list_pluck( $authors, 'name' );
		$author_name  = implode( ', ', $author_names );
	}
	?>

	<div class="mbs-landing-wrapper" itemscope itemtype="https://schema.org/Book">

		<!-- ── Hero: cover + core CTA ──────────────────────────────────── -->
		<section class="mbs-landing-hero" aria-label="<?php esc_attr_e( 'Book hero', 'my-book-showroom' ); ?>">

			<div class="mbs-landing-cover" itemprop="image">
				<?php echo MBS_Cover_Renderer::render( $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
			</div>

			<div class="mbs-landing-hero-copy">

				<?php if ( '' !== $author_name ) : ?>
					<p class="mbs-landing-author" itemprop="author" itemscope itemtype="https://schema.org/Person">
						<span itemprop="name"><?php echo esc_html( $author_name ); ?></span>
					</p>
				<?php endif; ?>

				<h1 class="mbs-landing-title" itemprop="name">
					<?php echo esc_html( $book_title ); ?>
				</h1>

				<?php if ( '' !== $book_excerpt ) : ?>
					<div class="mbs-landing-excerpt" itemprop="description">
						<?php echo wp_kses_post( wpautop( $book_excerpt ) ); ?>
					</div>
				<?php endif; ?>

				<!-- Primary CTA: buy buttons -->
				<div class="mbs-landing-cta-primary">
					<?php
					// render_order_now() outputs the "Order Now" button with modal / accordion buy links.
					echo MBS_Buy_Link_Renderer::render_order_now( $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput
					?>
				</div>

			</div><!-- .mbs-landing-hero-copy -->

		</section><!-- .mbs-landing-hero -->

		<!-- ── Book description ─────────────────────────────────────────── -->
		<?php if ( '' !== get_the_content() ) : ?>
			<section class="mbs-landing-description" aria-label="<?php esc_attr_e( 'About the book', 'my-book-showroom' ); ?>">
				<h2 class="mbs-landing-section-title"><?php esc_html_e( 'About the Book', 'my-book-showroom' ); ?></h2>
				<div class="mbs-landing-content" itemprop="description">
					<?php the_content(); ?>
				</div>
			</section>
		<?php endif; ?>

		<!-- ── Second CTA block (repeat buy buttons after description) ──── -->
		<section class="mbs-landing-cta-secondary" aria-label="<?php esc_attr_e( 'Get the book', 'my-book-showroom' ); ?>">
			<h2 class="mbs-landing-section-title"><?php esc_html_e( 'Get Your Copy', 'my-book-showroom' ); ?></h2>
			<div class="mbs-landing-cta-buttons">
				<?php echo MBS_Buy_Link_Renderer::render_order_now( $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
			</div>
		</section>

		<!-- ── Endorsements (if any) ────────────────────────────────────── -->
		<?php mbs_get_template_part( 'partials/section-endorsements', array( 'post_id' => $post_id ) ); ?>

	</div><!-- .mbs-landing-wrapper -->

	<?php
endwhile;

get_footer();
