<?php
/**
 * File Path: includes/blocks/class-mbs-block-featured-book.php
 *
 * Featured Book Block — Render
 *
 * PHP render_callback for the mbs/featured-book Gutenberg block.
 * Registered by MBS_Blocks_Registry::register_featured_book_block().
 *
 * Renders: book cover image, title, author byline, and Order Now button.
 * Uses MBS_Cover_Renderer (R5 rule) and MBS_Buy_Link_Renderer.
 *
 * @package    My_Book_Showroom
 * @since      1.0.0
 */

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

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

	/**
	 * Render the Featured Book block.
	 *
	 * Called by WordPress as the register_block_type render_callback.
	 * Returns an empty string for an invalid or missing post ID so the
	 * block produces no output rather than a broken shell.
	 *
	 * @param array $attributes Block attributes from the editor.
	 * @return string Rendered HTML.
	 */
	public static function render( array $attributes ): string {
		if ( ! mbs_is_registered() ) {
			return '';
		}

		$post_id = ! empty( $attributes['bookId'] ) ? (int) $attributes['bookId'] : 0;
		if ( ! $post_id ) {
			return '';
		}

		ob_start();
		?>
		<div class="mbs-block-featured-book">
			<?php echo MBS_Cover_Renderer::render( $post_id );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
			<h3><?php echo esc_html( get_the_title( $post_id ) ); ?></h3>
			<?php
			$authors = wp_get_post_terms( $post_id, MBS_TAX_AUTHOR, array( 'fields' => 'names' ) );
			if ( ! empty( $authors ) && ! is_wp_error( $authors ) ) {
				echo '<div class="mbs-book-author">' . esc_html( implode( ', ', $authors ) ) . '</div>';
			}
			?>
			<?php echo MBS_Buy_Link_Renderer::render_order_now( $post_id, null, array( 'context' => 'catalog' ) );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
		</div>
		<?php
		return ob_get_clean();
	}
}
