<?php
/**
 * File Path: templates/partials/section-more-in-collection.php
 *
 * My Book Showroom - More in This Collection Section
 *
 * FREE tier. Renders other published books in the same collection,
 * capped at 6. Uses the primary (first) collection term (D-003 fix).
 * Excludes the current book via post__not_in (D-004 fix).
 * Includes a "View all" link to the collection archive.
 *
 * GP-UI-1: Returns with zero output when:
 *  - No collection term is assigned to this book.
 *  - No other published books are in the collection.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

if ( ! mbs_is_registered() ) {
	return;
}

$post_id = isset( $post_id ) ? (int) $post_id : get_the_ID();

// 1. Fetch collection term(s) for this book.
$terms = wp_get_post_terms( $post_id, MBS_TAX_COLLECTION ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
if ( is_wp_error( $terms ) || empty( $terms ) ) {
	return; // GP-UI-1: no collection assigned.
}

// 2. Use the primary (first) term.
$primary_term = $terms[0]; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global

// 3. Query other books in this collection, excluding the current book.
$query = new WP_Query( array( // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
	'post_type'      => MBS_POST_TYPE,
	'post_status'    => 'publish',
	'posts_per_page' => 6,
	'post__not_in'   => array( $post_id ), // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- D-004: exclude current book; standard WP query pattern; VIP rule does not apply to this plugin
	'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- standard WP query pattern; VIP rule does not apply to this plugin
		array(
			'taxonomy' => MBS_TAX_COLLECTION,
			'field'    => 'term_id',
			'terms'    => $primary_term->term_id,
		),
	),
	'no_found_rows'  => true, // GP-UI-1: no pagination needed.
) );

if ( ! $query->have_posts() ) {
	wp_reset_postdata();
	return; // GP-UI-1: no other books in this collection.
}
?>

<div class="mbs-wrapper mbs-section mbs-section-more-in-collection">
	<h3>
		<?php
		printf(
			/* translators: %s: collection name */
			esc_html__( 'More in %s', 'my-book-showroom' ),
			esc_html( $primary_term->name )
		);
		?>
	</h3>
	<div class="mbs-collection-grid">
		<?php while ( $query->have_posts() ) : $query->the_post(); ?>
			<div class="mbs-collection-book">
				<?php echo MBS_Cover_Renderer::render( get_the_ID() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- MBS_Cover_Renderer::render() returns fully escaped HTML ?>
				<h4><a href="<?php echo esc_url( get_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a></h4>
			</div>
		<?php endwhile; ?>
	</div>
	<p class="mbs-view-all">
		<a href="<?php echo esc_url( get_term_link( $primary_term, MBS_TAX_COLLECTION ) ); ?>">
			<?php
			printf(
				/* translators: %s: collection name */
				esc_html__( 'View all in %s', 'my-book-showroom' ),
				esc_html( $primary_term->name )
			);
			?>
		</a>
	</p>
</div>
<?php wp_reset_postdata(); ?>
