<?php
/**
 * File Path: includes/blocks/class-mbs-block-book-cover.php
 *
 * Book Cover Block -- Render
 *
 * PHP render_callback for the mbs/book-cover Gutenberg block.
 * Registered by MBS_Blocks_Registry::register_book_cover_block().
 *
 * Renders a book's cover image using the authoritative 4-level hierarchy
 * (spinning video PRO, format-specific PRO, default cover, placeholder).
 * Delegates to MBS_Cover_Renderer::render(). Optional permalink wrap.
 * FREE tier (PRO enhancements: spinning video, format-specific covers).
 *
 * Attributes:
 *   bookId       (number)  -- Book post ID. Default 0 (none selected).
 *   link_to_book (boolean) -- Wrap cover in permalink. Default true.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Blocks
 * @since      1.0.0
 */

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

class MBS_Block_Book_Cover { // 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 '';
		}

		$cover = MBS_Cover_Renderer::render( $post_id );

		if ( ! empty( $attributes['link_to_book'] ?? true ) ) {
			$url = get_permalink( $post_id );
			if ( $url ) {
				$cover = '<a href="' . esc_url( $url ) . '" class="mbs-cover-link">' . $cover . '</a>';
			}
		}

		return $cover;
	}
}
