<?php
/**
 * File Path: includes/layouts/class-mbs-layout-masonry.php
 *
 * Masonry Layout
 *
 * FREE masonry layout for book archives and shortcodes.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

class MBS_Layout_Masonry 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 '';   // ← LICENSE GATE (GP-T-1)
		}

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

		ob_start();
		echo '<div class="mbs-wrapper">';
		echo '<div class="mbs-layout mbs-layout-masonry">';

		while ( $query->have_posts() ) {
			$query->the_post();
			$post_id = get_the_ID();

			echo '<div class="mbs-book-card mbs-masonry-item">';
			echo '<div class="mbs-card-cover-wrap">';
			echo MBS_Cover_Renderer::render( $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML
			echo '<div class="mbs-card-overlay"><a class="mbs-card-overlay-btn" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'View Book', 'my-book-showroom' ) . '</a></div>';
			echo '</div>';
			echo '<h3><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></h3>';
			echo '</div>';
		}

		echo '</div>';
		echo '</div>';
		wp_reset_postdata();

		return ob_get_clean();
	}
}
