<?php
/**
 * File Path: layouts/class-mbs-layout-flip.php
 *
 * Flip Layout (PRO)
 *
 * Renders a grid of books with CSS 3D flip animation on hover/focus.
 * Front: cover. Back: title, author, excerpt, Order Now button.
 *
 * Gates: mbs_is_registered() + mbs_is_pro() (GP-T-1).
 * Output: single .mbs-wrapper (R4 rule).
 * Cover: MBS_Cover_Renderer::render() (R5 rule).
 *
 */

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

class MBS_Layout_Flip implements MBS_Layout { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	public function render( WP_Query $query, array $options = [] ): string {
		if ( ! mbs_is_registered() ) {
			return '';
		}

		if ( ! mbs_is_pro() ) {
			// SV-16: Return HTML comment only — no visible element to site visitors (GP-T-3).
			return '<!-- MBS: Flip layout requires PRO license -->';
		}

		if ( ! $query->have_posts() ) {
			return '<p>' . esc_html__( 'No books found.', 'my-book-showroom' ) . '</p>';
		}

		ob_start();
		?>
		<div class="mbs-wrapper">
		<div class="mbs-layout mbs-layout-flip">
			<?php
			while ( $query->have_posts() ) {
				$query->the_post();
				$post_id = get_the_ID();
				// V2-A-004: get_the_terms() reads from WP_Query's pre-warmed term cache (no extra DB query).
				$_mbs_author_terms = get_the_terms( $post_id, MBS_TAX_AUTHOR );
				$author            = ( $_mbs_author_terms && ! is_wp_error( $_mbs_author_terms ) ) ? $_mbs_author_terms[0]->name : '';
				$teaser  = get_post_meta( $post_id, 'mbs_teaser', true );
				$excerpt = $teaser ?: wp_trim_words( get_the_excerpt(), 18 );
				?>
				<div class="mbs-flip-card mbs-book-card"
				     tabindex="0"
				     role="button"
				     aria-label="<?php echo esc_attr(
				         sprintf(
				             /* translators: %s: book title */
				             __( 'Flip card for %s — press Enter to view details', 'my-book-showroom' ),
				             get_the_title()
				         )
				     ); ?>">
					<div class="mbs-flip-inner">
						<!-- Front face: book cover -->
						<div class="mbs-flip-front" aria-hidden="true">
							<?php echo MBS_Cover_Renderer::render( $post_id );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
						</div>

						<!-- Back face: book info -->
						<div class="mbs-flip-back">
							<div class="mbs-flip-back-content">
								<h3 class="mbs-book-title">
									<a href="<?php echo esc_url( get_permalink() ); ?>">
										<?php echo esc_html( get_the_title() ); ?>
									</a>
								</h3>
								<?php if ( $author ) : ?>
									<p class="mbs-book-author"><?php echo esc_html( $author ); ?></p>
								<?php endif; ?>
								<?php if ( $excerpt ) : ?>
									<p class="mbs-book-excerpt"><?php echo esc_html( $excerpt ); ?></p>
								<?php endif; ?>
								<?php echo MBS_Buy_Link_Renderer::render_order_now( $post_id );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
							</div>
						</div>
					</div>
				</div>
				<?php
			}
			wp_reset_postdata();
			?>
		</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
