<?php
/**
 * File Path: templates/partials/section-reviews.php
 *
 * My Book Showroom — Reader Reviews Section
 *
 * Renders reader reviews and the submission form.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

if ( ! mbs_is_registered() ) {
	return;
}

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

// Per-book toggle — section hidden unless explicitly enabled by the author.
if ( ! get_post_meta( $post_id, 'mbs_enable_reader_reviews', true ) ) {
	return;
}

$comments = get_comments( array(
	'post_id' => $post_id,
	'status'  => 'approve',
	'type'    => 'mbs_review',
) );

$stats    = MBS_Book_Stats_DAO::get_stats( $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
$average  = $stats ? (float) $stats->review_avg   : 0.0; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
$count    = $stats ? (int)   $stats->review_count : 0; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
?>

<div class="mbs-wrapper mbs-section mbs-section-reviews">
	<h2><?php echo esc_html__( 'Reader Reviews', 'my-book-showroom' ); ?></h2>

	<?php if ( $average && $count ) : ?>
		<div class="mbs-review-summary">
			<?php 
			if ( class_exists( 'MBS_Star_Rating' ) ) {
				echo MBS_Star_Rating::render_read_only( (float) $average, (int) $count );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML
			} else {
				echo esc_html( number_format( (float) $average, 1 ) ) . ' ★';
			}
			?>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $comments ) ) : ?>
		<div class="mbs-review-list">
			<?php 
			wp_list_comments( array(
				'style'       => 'div',
				'short_ping'  => true,
				'callback'    => function( $comment ) {
					$rating = get_comment_meta( $comment->comment_ID, 'mbs_review_rating', true );
					$title  = get_comment_meta( $comment->comment_ID, 'mbs_review_title', true );
					?>
					<div class="mbs-review-item">
						<?php if ( $rating ) : ?>
							<div class="mbs-review-stars">
								<?php 
								if ( class_exists( 'MBS_Star_Rating' ) ) {
									echo MBS_Star_Rating::render_read_only( (float) $rating, null );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML
								} else {
									echo esc_html( number_format( (float) $rating, 1 ) ) . ' ★';
								}
								?>
							</div>
						<?php endif; ?>
						<?php if ( $title ) : ?>
							<h4 class="mbs-review-title"><?php echo esc_html( $title ); ?></h4>
						<?php endif; ?>
						<div class="mbs-review-text">
							<?php comment_text(); ?>
						</div>
						<div class="mbs-review-meta">
							<span class="mbs-review-author"><?php echo esc_html( get_comment_author() ); ?></span>
							<span class="mbs-review-date"><?php echo esc_html( get_comment_date() ); ?></span>
						</div>
					</div>
					<?php
				},
			), $comments );
			?>
		</div>
	<?php endif; ?>

	<?php 
	// Review submission form would go here in a future sprint
	// Currently placeholder per current scope (Sprint 8/9)
	?>
	<p class="mbs-review-note"><?php echo esc_html__( 'Reviews are moderated and appear after approval.', 'my-book-showroom' ); ?></p>
</div>
