<?php
/**
 * File Path: templates/taxonomy-mbs-series.php
 *
 * Taxonomy Archive — mbs_series
 *
 * 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().
 *
 * SHC-6: Layout resolved via MBS_Layout_Registry::resolve_slug_for_archive()
 * (archive context — Hero excluded).
 *
 * PRO tier (F-011 — series banner):
 *   · If `mbs_series_banner_id` term_meta is set, a full-width banner image
 *     is displayed between the series header and the book grid.
 *
 * @package MyBookShowroom
 * @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_series_archive_display_mode',
	'mbs_setting_series_archive_display_mode',
	'mbs_setting_show_series_reading_order'
);

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

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

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

	<header class="mbs-archive-header">
		<h1 class="mbs-archive-title">
			<?php
			printf(
				/* translators: %s: series name */
				esc_html__( 'Series: %s', 'my-book-showroom' ),
				esc_html( single_term_title( '', false ) )
			);
			?>
		</h1>
		<?php mbs_get_template_part( 'partials/series-complete-badge', array( 'series_term_id' => $term_id ) ); ?>
		<?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-011 (Session 9): Series banner image — PRO only. Unchanged.
	if ( ( mbs_is_pro() || mbs_is_pub() ) && $term_id ) :
		$banner_id = (int) get_term_meta( $term_id, 'mbs_series_banner_id', true );
		if ( $banner_id ) :
			?>
			<div class="mbs-series-banner">
				<?php echo wp_get_attachment_image( // R5-exempt: series banner image
					$banner_id,
					'full',
					false,
					array(
						'class' => 'mbs-series-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().
	$_mbs_ro_html = MBS_Archive_Reader::render_ordered(
		MBS_Series_Reading_Order_DAO::class,
		$term_id,
		$layout_obj,
		$layout_slug,
		array(
			'display_mode'  => $display_mode,
			'merge_missing' => false,
			'context'       => 'series',
		)
	);
	if ( '' === $_mbs_ro_html ) :
		?>
		<p><?php esc_html_e( 'No books found in this series.', '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();
