<?php
/**
 * File Path: includes/blocks/class-mbs-block-book-teaser.php
 *
 * Book Teaser Block -- Render
 *
 * PHP render_callback for the mbs/book-teaser Gutenberg block.
 * Registered by MBS_Blocks_Registry::register_book_teaser_block().
 *
 * Renders the mbs_teaser postmeta blurb for a book, with an optional
 * cover thumbnail alongside. Delegates to the partials/teaser template
 * via mbs_get_template_part(). FREE tier.
 *
 * Attributes:
 *   bookId     (number)  -- Book post ID. Default 0 (none selected).
 *   show_cover (boolean) -- Prepend cover thumbnail. Default false.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Blocks
 * @since      1.0.0
 */

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

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

	public static function render( array $attributes ): string {
		if ( ! mbs_is_registered() ) {
			return '';
		}

		$post_id = max( 0, (int) ( $attributes['bookId'] ?? 0 ) );
		if ( ! $post_id || get_post_type( $post_id ) !== MBS_POST_TYPE ) {
			return '';
		}

		$output = '';

		if ( ! empty( $attributes['show_cover'] ?? false ) ) {
			$output .= MBS_Cover_Renderer::render( $post_id );
		}

		ob_start();
		mbs_get_template_part( 'partials/teaser', array( 'post_id' => $post_id ) );
		$output .= ob_get_clean();

		return $output;
	}
}
