<?php
/**
 * File Path: includes/shortcodes/class-mbs-shortcode-book.php
 *
 * [mbs_book] Shortcode
 *
 * Renders a full inline book display using the same partials as single-mbs-book.php.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

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

	/**
	 * Render the [mbs_book] shortcode (H-10).
	 */
	public static function render( array $atts = array() ): string {
		$atts = shortcode_atts( array(
			'id' => 0,
		), $atts, 'mbs_book' );

		$post_id = absint( $atts['id'] );
		if ( ! $post_id || get_post_type( $post_id ) !== MBS_POST_TYPE ) {
			return '<!-- MBS: Invalid book ID -->';
		}

		if ( ! mbs_is_registered() ) {
			return '<!-- MBS: License required -->';
		}

		ob_start();
		?>
		<div class="mbs-wrapper mbs-shortcode-book">
			<?php
			mbs_get_template_part( 'partials/cover', array( 'post_id' => $post_id ) );

			// Compact description (mbs_synopsis -> mbs_teaser fallback). Kept for
			// embed contexts (sidebars, page builders) where full section content
			// is too long. N-HIGH-001 fix: retained from original shortcode.
			// V8-CRIT-001 fix: two-arg form only.
			mbs_get_template_part( 'partials/teaser', array( 'post_id' => $post_id ) );

			$order             = MBS_Section_Order::get_order( $post_id );
			$available_keys    = array_keys( MBS_Section_Helper::get_available_section_data() );
			$mbs_skip_sections = array( 'social-share', 'suggested-reading-order', 'goodreads' );

			foreach ( $order as $section ) {
				if ( in_array( $section, $mbs_skip_sections, true ) ) {
					continue;
				}
				if ( ! in_array( $section, $available_keys, true ) ) {
					continue; // Filtered by requires_option (e.g. Universe disabled).
				}
				mbs_get_template_part( 'partials/section-' . $section, array( 'post_id' => $post_id ) );
			}
			?>
		</div>
		<?php

		return ob_get_clean();
	}
}
