<?php
/**
 * File Path: templates/partials/section-awards-accolades.php
 *
 * My Book Showroom -- Awards & Accolades Section (CE-1)
 *
 * Renders the awards and accolades section on the single-book page.
 * No PRO gate -- ALL tiers. Reads the mbs_awards post meta JSON array.
 *
 * @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;
}

if ( ! isset( $post_id ) ) {
	$post_id = get_the_ID();
}

$mbs_awards_rows = MBS_Book_Awards_DAO::get( $post_id ); // Phase 2: read from custom table

// Guard: render nothing when the array is empty.
if ( empty( $mbs_awards_rows ) ) {
	return;
}

// Human-readable status labels -- mirrors MBS_Book_Header_Extended_Meta::award_statuses().
$mbs_award_status_labels = array(
	'winner'            => __( 'Winner',            'my-book-showroom' ),
	'finalist'          => __( 'Finalist',          'my-book-showroom' ),
	'nominee'           => __( 'Nominee',           'my-book-showroom' ),
	'honorable_mention' => __( 'Honorable Mention', 'my-book-showroom' ),
	'longlist'          => __( 'Longlist',          'my-book-showroom' ),
);
?>

<div class="mbs-wrapper mbs-section mbs-section-awards">
	<h2><?php echo esc_html__( 'Awards &amp; Accolades', 'my-book-showroom' ); ?></h2>
	<ul class="mbs-awards-list">
		<?php foreach ( $mbs_awards_rows as $mbs_award ) :
			$mbs_award_name   = isset( $mbs_award['name'] )   ? $mbs_award['name']   : '';
			$mbs_award_year   = isset( $mbs_award['year'] )   ? $mbs_award['year']   : '';
			$mbs_award_status = isset( $mbs_award['status'] ) ? $mbs_award['status'] : '';
			if ( '' === $mbs_award_name ) {
				continue;
			}
			$mbs_status_label = isset( $mbs_award_status_labels[ $mbs_award_status ] )
				? $mbs_award_status_labels[ $mbs_award_status ]
				: '';
			$mbs_winner_class = ( 'winner' === $mbs_award_status ) ? ' mbs-award-status--winner' : '';
		?>
		<li>
			<strong class="mbs-award-name"><?php echo esc_html( $mbs_award_name ); ?></strong>
			<?php if ( '' !== $mbs_award_year ) : ?>
				<span class="mbs-award-year">(<?php echo esc_html( $mbs_award_year ); ?>)</span>
			<?php endif; ?>
			<?php if ( '' !== $mbs_status_label ) : ?>
				<span class="mbs-award-status<?php echo esc_attr( $mbs_winner_class ); ?>">
					&mdash; <?php echo esc_html( $mbs_status_label ); ?>
				</span>
			<?php endif; ?>
		</li>
		<?php endforeach; ?>
	</ul>
</div>
