<?php
/**
 * File Path: templates/taxonomy-mbs-genre.php
 *
 * Taxonomy Archive — mbs_genre
 *
 * Displays all books belonging to a specific genre with proper license
 * gating, short section key compatibility, and consistent .mbs-wrapper
 * scoping.
 *
 * SHC-6: Layout Registry. The active layout is resolved via
 * MBS_Layout_Registry::resolve_slug_for_term() and ::get_layout() — honouring
 * the per-term override (PRO) or the global default setting.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

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; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global

// SHC-6: Resolve layout for this term (per-term PRO override or global default).
$layout_slug = MBS_Layout_Registry::resolve_slug_for_term( $term_id, 'grid' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
$layout_obj  = MBS_Layout_Registry::get_layout( $layout_slug ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
?>

<div class="mbs-wrapper mbs-taxonomy-archive mbs-taxonomy-archive-genre">
	<header class="mbs-archive-header">
		<h1 class="mbs-archive-title">
			<?php
			printf(
				/* translators: %s: genre name */
				esc_html__( 'Genre: %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
	// SHC-6: Layout Registry renders the full grid.
	// A-010 (Round 4): partial lives at templates/partials/book-card.php —
	// previously mbs_get_template_part( 'book-card' ) silently rendered nothing.
	// Layout Registry handles that partial call internally.
	global $wp_query;
	echo $layout_obj->render( $wp_query ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML
	if ( $wp_query->max_num_pages > 1 ) :
		the_posts_pagination( array(
			'mid_size'  => 2,
			'prev_text' => __( '← Previous', 'my-book-showroom' ),
			'next_text' => __( 'Next →', 'my-book-showroom' ),
		) );
	endif;
	?>
</div>

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