<?php
/**
 * File Path: templates/taxonomy-mbs-universe.php
 *
 * Taxonomy Archive -- mbs_universe
 *
 * Displays all books belonging to a specific universe / shared world.
 *
 * Sprint RO-Display (Group 7): All archive rendering delegated to
 * MBS_Archive_Reader::render_ordered(). Display mode (order / reading_order /
 * both) resolved via MBS_Archive_Reader::resolve_display_mode() from per-term
 * meta, global option, and toggle gate. PRO gate applied inside render_ordered().
 *
 * merge_missing: true -- the universe reading-order junction table may be stale
 * (D-16): books added since the last reading-order save are appended at the end.
 *
 * SHC-6: Layout resolved via MBS_Layout_Registry::resolve_slug_for_archive()
 * (archive context -- Hero excluded).
 *
 * Toggle-gated: the Universe taxonomy is opt-in via Settings -> Advanced.
 * This template renders regardless of the toggle (WordPress selects it
 * based on taxonomy slug); the toggle governs metabox registration and
 * admin menu visibility only.
 *
 * @package    My_Book_Showroom
 * @subpackage Templates
 * @since      1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

if ( ! mbs_is_registered() ) {
	get_header();
	echo '<p>' . esc_html__( 'This feature requires a registered license.', 'my-book-showroom' ) . '</p>';
	get_footer();
	exit;
}

get_header();

$term    = get_queried_object();
$term_id = ( $term instanceof WP_Term ) ? (int) $term->term_id : 0;

// Sprint RO-Display (Group 7): resolve display mode from per-term meta, global option, toggle.
$display_mode = MBS_Archive_Reader::resolve_display_mode(
	$term_id,
	'mbs_universe_archive_display_mode',
	'mbs_setting_universe_archive_display_mode',
	'mbs_setting_show_universe_reading_order'
);

// SHC-6: Layout resolved for archive context (Hero excluded).
$layout_slug = MBS_Layout_Registry::resolve_slug_for_archive( $term_id, 'mbs_universe' );
$layout_obj  = MBS_Layout_Registry::get_layout( $layout_slug );
?>

<div class="mbs-wrapper mbs-taxonomy-archive mbs-taxonomy-archive-universe">

	<?php mbs_get_template_part( 'partials/breadcrumbs' ); ?>

	<header class="mbs-archive-header">
		<h1 class="mbs-archive-title">
			<?php
			printf(
				/* translators: %s: universe name */
				esc_html__( 'Universe: %s', 'my-book-showroom' ),
				esc_html( single_term_title( '', false ) )
			);
			?>
		</h1>

		<?php if ( $term instanceof WP_Term && ! empty( $term->description ) ) : ?>
			<div class="mbs-term-description">
				<?php echo wp_kses_post( wpautop( $term->description ) ); ?>
			</div>
		<?php endif; ?>
	</header>

	<?php
	// F-BNR-U: Universe banner image -- PRO only.
	if ( ( mbs_is_pro() || mbs_is_pub() ) && $term_id ) :
		$banner_id = (int) get_term_meta( $term_id, 'mbs_universe_banner_id', true );
		if ( $banner_id ) :
			?>
			<div class="mbs-universe-banner">
				<?php echo wp_get_attachment_image( // R5-exempt: universe banner image
					$banner_id,
					'full',
					false,
					array(
						'class' => 'mbs-universe-banner-img',
						'alt'   => $term->name,
					)
				); ?>
			</div>
			<?php
		endif;
	endif;
	?>

	<?php
	// Sprint RO-Display (Group 7): delegated to MBS_Archive_Reader.
	// display_mode resolved above; PRO gate applied inside render_ordered().
	// merge_missing: true handles stale junction-table rows (D-16).
	$_mbs_ro_html = MBS_Archive_Reader::render_ordered(
		MBS_Universe_Reading_Order_DAO::class,
		$term_id,
		$layout_obj,
		$layout_slug,
		array(
			'display_mode'  => $display_mode,
			'merge_missing' => true,
			'context'       => 'universe',
		)
	);
	if ( '' === $_mbs_ro_html ) :
		?>
		<p><?php esc_html_e( 'No books found in this universe.', 'my-book-showroom' ); ?></p>
	<?php else : ?>
		<?php echo $_mbs_ro_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted HTML from MBS_Archive_Reader (layout class owns its own escaping) ?>
	<?php endif; ?>

</div>

<?php
mbs_get_template_part( 'partials/power-badge' );
get_footer();
