<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
/**
 * Custom Product Rating Template
 * 
 * Displays custom UGC Commerce rating in WooCommerce single product summary.
 *
 * @package Ugcify
 * 
 * Available variables:
 * @var float $avg_rating    Average rating (0-5)
 * @var int   $total_reviews Total number of approved reviews
 * @var int   $product_id    Current product ID
 */

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

<div class="woocommerce-product-rating ugcify-product-rating">
	<div class="ugcify-rating-wrapper">
		<?php
		/* translators: %s: Average product rating value (e.g. 4.5). */
		$ugcify_rating_label = sprintf( __( 'Rated %s out of 5', 'ugcify' ), number_format( $avg_rating, 1 ) );
		?>
		<div class="ugcify-stars-display" role="img" aria-label="<?php echo esc_attr( $ugcify_rating_label ); ?>">
			<?php
			for ( $i = 1; $i <= 5; $i++ ) {
				$star_type = 'empty';
				if ( $i <= floor( $avg_rating ) ) {
					$star_type = 'full';
				} elseif ( $i <= ceil( $avg_rating ) && ( $avg_rating - floor( $avg_rating ) >= 0.5 ) ) {
					$star_type = 'half';
				}
				echo '<span class="ugcify-star-display ' . esc_attr( $star_type ) . '" aria-hidden="true"></span>';
			}
			?>
		</div>
		<div class="ugcify-rating-text">
			<span class="ugcify-rating-average"><?php echo number_format( $avg_rating, 1 ); ?></span>
			<a href="#reviews" class="woocommerce-review-link ugcify-review-link" rel="nofollow">
				<?php
				printf(
					/* translators: %s: number of reviews */
					esc_html( _n( '(%s review)', '(%s reviews)', $total_reviews, 'ugcify' ) ),
					'<span class="count">' . esc_html( number_format_i18n( $total_reviews ) ) . '</span>'
				);
				?>
			</a>
		</div>
	</div>
</div>
