<?php
global $post;

use Prodigy\Includes\Frontend\Pages\Prodigy_Product_Page;
use Prodigy\Includes\Helpers\Prodigy_Formatting;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

if ( ! comments_open() ) {
	return;
}

?>

<div class="prodigy-reviews-ratings">
	<div class="d-flex align-items-center">
		<div class="prodigy-reviews-ratings__rating">
			<p class="review-rating-title"><?php echo esc_html( $rating_title ); ?></p>
			<div class="d-flex align-items-center flex-wrap">
				<?php if ( get_option( 'pg_product_rating' ) ) : ?>
					<?php echo wp_kses( pg_get_rating_html( $average ), Prodigy_Formatting::get_allowed_html() ); ?>
				<?php endif; ?>

				<?php if ( comments_open() ) : ?>
					<?php if ( get_option( 'pg_product_review' ) ) : ?>
						<div class="d-flex align-items-center">
							<span class="prodigy-reviews-ratings__average">
								<?php echo esc_html( $average ); ?>
							</span>
							<span class="prodigy-reviews-ratings__count">
									<?php /* translators: %s: number of reviews */ ?>
									<?php printf( esc_attr( _n( '%s review', '%s reviews', $review_count, 'prodigy-commerce' ) ), '' . esc_html( $review_count ) ); ?>
							</span>
						</div>
					<?php endif ?>
				<?php endif ?>
			</div>
		</div>
	</div>

	<button class="prodigy-main-button prodigy-reviews-ratings__btn" type="button" data-toggle="modal"
			data-target="#reviewModal"
			aria-label="<?php esc_attr_e( 'Write a review', 'prodigy-commerce' ); ?>"><?php esc_html_e( 'Write a review', 'prodigy-commerce' ); ?></button>
</div>

<div class="prodigy-reviews">
	<ul class="prodigy-review prodigy-comments">

		<?php if ( $review_count ) : ?>
			<?php

			$comments = get_comments(
				array( 'post_id' => $post->ID )
			);

			wp_list_comments(
				apply_filters(
					'prodigy_product_review_list_args',
					array(
						'callback' => 'prodigy_comments',
						'per_page' => Prodigy_Product_Page::COUNT_COMMENTS_ON_PAGE_PRODUCT,
						'page'     => 1,
					)
				),
				$comments
			);
			?>

			<?php
			if ( get_comment_pages_count() > 1 ) :
				echo '<nav class="prodigy-pagination">';
				paginate_comments_links(
					apply_filters(
						'prodigy_comment_pagination_args',
						array(
							'prev_text' => '&larr;',
							'next_text' => '&rarr;',
							'type'      => 'list',
						)
					)
				);
				echo '</nav>';
			endif;
			?>
		<?php endif; ?>
		<?php
		// Compute whether a second page of reviews exists — avoids an AJAX round-trip on every product page load.
		$prodigy_review_total = (int) get_comments( array( 'post_id' => get_the_ID(), 'count' => true ) );
		$prodigy_has_more     = $prodigy_review_total > Prodigy_Product_Page::COUNT_COMMENTS_ON_PAGE_PRODUCT;
		?>
		<div class="d-flex align-items-center justify-content-center">
			<div class="<?php echo $prodigy_has_more ? 'd-flex' : 'd-none'; ?> align-items-center link-show-more-reviews-js">
				<button class="d-flex align-items-center prodigy-more-reviews__button" type="button" aria-label="<?php esc_attr_e( 'Show more reviews', 'prodigy-commerce' ); ?>">
					<span data-page="2" class="per-page-js"><?php esc_html_e( 'Show more reviews', 'prodigy-commerce' ); ?></span>
					<i class="icon icon-arrow-down" role="presentation" aria-hidden="true"></i>
				</button>
			</div>
		</div>
	</ul>
</div>
