<?php
/**
 * File Path: templates/partials/bestseller-badge.php
 *
 * Bestseller Badge Partial -- DISPLAY-2
 *
 * Renders a #1 Bestseller badge when the current book's best ASIN has a
 * sales_rank <= the configured threshold. PRO-gated.
 *
 * Call via:
 *   mbs_get_template_part( 'partials/bestseller-badge' );
 *
 * Relies on get_the_ID() being set (inside a WP loop or singular template).
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

if ( ! mbs_is_pro() ) {
	echo '<!-- MBS: bestseller-badge requires PRO license -->';
	return;
}

$post_id   = get_the_ID();
$threshold = (int) get_option( 'mbs_setting_bestseller_rank_threshold', 100 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global

// Resolve best sales_rank across all ASINs for this book.
$best_rank = MBS_PA_API_Cache_DAO::get_best_sales_rank_for_post( $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
if ( null === $best_rank || $best_rank <= 0 || $best_rank > $threshold ) {
	return;
}

$label = apply_filters( // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
	'mbs_bestseller_badge_label', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix
	__( '#1 Bestseller', 'my-book-showroom' ),
	$post_id,
	$best_rank
);

?>
<span class="mbs-bestseller-badge" aria-label="<?php echo esc_attr( $label ); ?>">
	<?php echo esc_html( $label ); ?>
</span>
