<?php
/**
 * File Path: layouts/class-mbs-layout-slider.php
 *
 * Slider Layout (PRO)
 *
 * Renders a horizontally scrollable book carousel with prev/next navigation
 * arrows. Uses CSS scroll-snap and native browser scrolling — no external library.
 *
 * 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_Slider 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: Slider layout requires PRO license -->';
		}

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

		$slider_id = 'mbs-slider-' . wp_unique_id();

		ob_start();
		?>
		<div class="mbs-wrapper">
		<div class="mbs-layout mbs-layout-slider" id="<?php echo esc_attr( $slider_id ); ?>">
			<div class="mbs-slider-viewport" role="region" aria-label="<?php esc_attr_e( 'Book carousel', 'my-book-showroom' ); ?>">
				<div class="mbs-slider-track">
					<?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 : '';
						?>
						<div class="mbs-slider-slide mbs-book-card" role="group" aria-label="<?php echo esc_attr( get_the_title() ); ?>">
							<?php echo MBS_Cover_Renderer::render( $post_id );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
							<div class="mbs-book-info">
								<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; ?>
								<a class="mbs-view-book" href="<?php echo esc_url( get_permalink() ); ?>"><?php esc_html_e( 'View Book', 'my-book-showroom' ); ?></a>
							</div>
						</div>
						<?php
					}
					wp_reset_postdata();
					?>
				</div>
			</div>

			<div class="mbs-slider-controls" aria-label="<?php esc_attr_e( 'Carousel navigation', 'my-book-showroom' ); ?>">
				<button type="button"
				        class="mbs-slider-prev"
				        aria-label="<?php esc_attr_e( 'Previous books', 'my-book-showroom' ); ?>"
				        data-slider="<?php echo esc_attr( $slider_id ); ?>">
					&#8249;
				</button>
				<button type="button"
				        class="mbs-slider-next"
				        aria-label="<?php esc_attr_e( 'Next books', 'my-book-showroom' ); ?>"
				        data-slider="<?php echo esc_attr( $slider_id ); ?>">
					&#8250;
				</button>
			</div>
		</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
