<?php
/**
 * Product Rating Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Product_Rating $widget The widget instance.
 */
function ggmc_product_rating_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();

	// Get product data (includes preview product support).
	$product_data = $widget->get_product_data();

	// Default values for editor preview.
	$average_rating = 0;
	$rating_count   = 0;

	if ( $product_data ) {
		$average_rating = isset( $product_data->average_rating ) ? (float) $product_data->average_rating : 0;
		$rating_count   = isset( $product_data->rating_count ) ? (int) $product_data->rating_count : 0;
	}

	// In editor mode, show placeholder if no real data.
	if ( ggmc_is_editor_mode() && 0 === $rating_count ) {
		$average_rating = 4.5;
		$rating_count   = 24;
	}

	// Hide if no reviews and setting is enabled.
	if ( 'yes' === $settings['hide_if_no_reviews'] && 0 === $rating_count ) {
		if ( ! ggmc_is_editor_mode() ) {
			return;
		}
	}

	// Build the rating HTML.
	$show_empty = 'yes' === $settings['show_empty_stars'];
	$stars_html = ggmc_product_rating_render_stars( $average_rating, $show_empty, $settings['star_style'] );

	// Build text based on display style.
	$text_html = '';
	switch ( $settings['display_style'] ) {
		case 'stars_count':
			if ( $rating_count > 0 ) {
				$text_html = sprintf(
					'<span class="ggmc-rating-text">(%d %s)</span>',
					$rating_count,
					_n( 'review', 'reviews', $rating_count, 'ggm-commerce' )
				);
			} elseif ( ! empty( $settings['no_reviews_text'] ) ) {
				$text_html = sprintf(
					'<span class="ggmc-rating-text">(%s)</span>',
					esc_html( $settings['no_reviews_text'] )
				);
			}
			break;

		case 'stars_average':
			if ( $rating_count > 0 ) {
				$text_html = sprintf(
					'<span class="ggmc-rating-average">%s</span>',
					number_format( $average_rating, 1 )
				);
			}
			break;

		case 'stars_full':
			if ( $rating_count > 0 ) {
				$text_html = sprintf(
					'<span class="ggmc-rating-average">%s</span> <span class="ggmc-rating-text">(%d %s)</span>',
					number_format( $average_rating, 1 ),
					$rating_count,
					_n( 'review', 'reviews', $rating_count, 'ggm-commerce' )
				);
			} elseif ( ! empty( $settings['no_reviews_text'] ) ) {
				$text_html = sprintf(
					'<span class="ggmc-rating-text">%s</span>',
					esc_html( $settings['no_reviews_text'] )
				);
			}
			break;
	}

	// Wrap in container.
	$output  = '<div class="ggmc-product-rating">';
	$output .= '<span class="ggmc-stars-wrapper">' . $stars_html . '</span>';
	$output .= $text_html;
	$output .= '</div>';

	// Wrap with link if enabled.
	if ( 'yes' === $settings['link_to_reviews'] && ! empty( $settings['reviews_anchor'] ) ) {
		$anchor = '#' . sanitize_html_class( $settings['reviews_anchor'] );
		$output = sprintf(
			'<a href="%s" class="ggmc-product-rating-link">%s</a>',
			esc_attr( $anchor ),
			$output
		);
	}

	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $output is built from escaped components and safe HTML
	echo $output;
}

/**
 * Render star rating HTML.
 *
 * @param float  $rating     Rating value (0-5).
 * @param bool   $show_empty Show empty stars.
 * @param string $style      Star style (filled or outline).
 * @return string HTML stars.
 */
function ggmc_product_rating_render_stars( $rating, $show_empty = true, $style = 'filled' ) {
	$rating     = max( 0, min( 5, (float) $rating ) );
	$full_stars = floor( $rating );
	$half_star  = ( $rating - $full_stars ) >= 0.5;
	$empty_stars = 5 - $full_stars - ( $half_star ? 1 : 0 );

	// Star characters.
	$filled_char = '&#9733;'; // Filled star.
	$empty_char  = '&#9734;'; // Empty star.

	$html = '<span class="ggmc-stars">';

	// Full stars.
	for ( $i = 0; $i < $full_stars; $i++ ) {
		$html .= '<span class="ggmc-star ggmc-star-full">' . $filled_char . '</span>';
	}

	// Half star (using CSS gradient or just showing as full for simplicity).
	if ( $half_star ) {
		$html .= '<span class="ggmc-star ggmc-star-half">' . $filled_char . '</span>';
	}

	// Empty stars.
	if ( $show_empty ) {
		for ( $i = 0; $i < $empty_stars; $i++ ) {
			$html .= '<span class="ggmc-star ggmc-star-empty">' . $empty_char . '</span>';
		}
	}

	$html .= '</span>';

	return $html;
}
