<?php
/**
 * File Path: templates/partials/book-card.php
 *
 * Book Card Partial
 *
 * Minimal reusable card used by taxonomy archive templates
 * (taxonomy-mbs-author.php, taxonomy-mbs-series.php, taxonomy-mbs-genre.php).
 *
 * Renders a cover image, book title as a link, and a short teaser excerpt.
 * Called via mbs_get_template_part( 'partials/book-card' ) from inside
 * a standard WordPress loop (have_posts() / the_post() already called).
 *
 * Expected context: inside a standard WP_Query loop on mbs_book post type.
 * Optional context variable: $format_row (object|null) — passed only when
 * a specific format row is known; otherwise falls back to the book default cover.
 *
 * @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() ) {
	return;
}

$post_id   = get_the_ID();
$permalink = get_permalink( $post_id );
$title     = get_the_title( $post_id );
$teaser    = get_post_meta( $post_id, 'mbs_teaser', true );

// Author taxonomy terms for display
$author_terms = get_the_terms( $post_id, MBS_TAX_AUTHOR );
$author_name  = '';
if ( $author_terms && ! is_wp_error( $author_terms ) ) {
	$author_name = $author_terms[0]->name;
}
?>
<div class="mbs-wrapper mbs-book-card">

	<a href="<?php echo esc_url( $permalink ); ?>" class="mbs-book-card__cover-link" aria-hidden="true" tabindex="-1">
		<?php echo MBS_Cover_Renderer::render( $post_id, $format_row ?? null );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML ?>
	</a>

	<div class="mbs-book-card__info">

		<h3 class="mbs-book-card__title">
			<a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
		</h3>

		<?php mbs_get_template_part( 'partials/coming-soon-badge' ); ?>
		<?php mbs_get_template_part( 'partials/bestseller-badge' ); // DISPLAY-2 ?>

		<?php if ( $author_name ) : ?>
			<p class="mbs-book-card__author">
				<?php echo esc_html( $author_name ); ?>
			</p>
		<?php endif; ?>

		<?php if ( $teaser ) : ?>
			<p class="mbs-book-card__teaser">
				<?php echo esc_html( wp_trim_words( $teaser, 20, '…' ) ); ?>
			</p>
		<?php endif; ?>

	</div>

</div>
