<?php
/**
 * Rating & Review Management Service
 *
 * Manages business ratings and review information for schema.org markup.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Rating {

	/**
	 * Get rating field definitions for form builder
	 *
	 * @return array Field definitions for rating section
	 */
	public static function get_field_definitions() {
		return array(
			array(
				'section'   => 'ratings_reviews',
				'name'      => 'rating_value',
				'label'     => __( 'Overall Rating', 'bw-schema' ),
				'type'      => 'number',
				'help'      => __( 'Average rating (0-5 scale, e.g., 4.5)', 'bw-schema' ),
				'sanitize'  => 'floatval',
				'validate'  => 'number',
			),
			array(
				'section'   => 'ratings_reviews',
				'name'      => 'rating_count',
				'label'     => __( 'Number of Ratings', 'bw-schema' ),
				'type'      => 'number',
				'help'      => __( 'Total number of ratings/reviews (e.g., 250)', 'bw-schema' ),
				'sanitize'  => 'absint',
				'validate'  => 'number',
			),
			array(
				'section'   => 'ratings_reviews',
				'name'      => 'rating_source',
				'label'     => __( 'Rating Source', 'bw-schema' ),
				'type'      => 'select',
				'options'   => array(
					''                  => __( '-- Select Source --', 'bw-schema' ),
					'google'            => __( 'Google My Business', 'bw-schema' ),
					'yelp'              => __( 'Yelp', 'bw-schema' ),
					'trustpilot'        => __( 'Trustpilot', 'bw-schema' ),
					'facebook'          => __( 'Facebook', 'bw-schema' ),
					'capterra'          => __( 'Capterra', 'bw-schema' ),
					'g2'                => __( 'G2', 'bw-schema' ),
					'custom'            => __( 'Custom/Internal', 'bw-schema' ),
				),
				'help'      => __( 'Where is the rating from?', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			),
			array(
				'section'   => 'ratings_reviews',
				'name'      => 'review_url',
				'label'     => __( 'Reviews URL', 'bw-schema' ),
				'type'      => 'url',
				'help'      => __( 'Link to your reviews page or profile (e.g., Google My Business URL)', 'bw-schema' ),
				'sanitize'  => 'esc_url_raw',
			),
		);
	}

	/**
	 * Format rating for schema.org AggregateRating
	 *
	 * @param array $org_data Organization data
	 * @return array AggregateRating schema or empty array
	 */
	public static function format_for_schema( $org_data ) {
		if ( empty( $org_data['rating_value'] ) || empty( $org_data['rating_count'] ) ) {
			return array();
		}

		$rating = floatval( $org_data['rating_value'] );
		$count = intval( $org_data['rating_count'] );

		// Validate rating range
		if ( $rating < 0 || $rating > 5 || $count < 1 ) {
			return array();
		}

		return array(
			'@type'         => 'AggregateRating',
			'ratingValue'   => $rating,
			'ratingCount'   => $count,
			'bestRating'    => 5,
			'worstRating'   => 1,
		);
	}

	/**
	 * Get rating stars HTML
	 *
	 * @param array $org_data Organization data
	 * @return string HTML stars representation
	 */
	public static function get_stars_html( $org_data ) {
		if ( empty( $org_data['rating_value'] ) ) {
			return '';
		}

		$rating = floatval( $org_data['rating_value'] );
		$full_stars = intval( $rating );
		$has_half = ( $rating - $full_stars ) >= 0.5;

		$html = '<div class="bw-schema-stars" title="' . esc_attr( $rating ) . ' out of 5">';

		// Full stars
		for ( $i = 0; $i < $full_stars; $i++ ) {
			$html .= '⭐';
		}

		// Half star
		if ( $has_half ) {
			$html .= '⭐';
		}

		$html .= ' ' . $rating . '/5';

		// Review count
		if ( ! empty( $org_data['rating_count'] ) ) {
			$html .= ' (' . intval( $org_data['rating_count'] ) . ' reviews)';
		}

		$html .= '</div>';

		return $html;
	}

	/**
	 * Validate rating value
	 *
	 * @param float $rating Rating value
	 * @return bool True if valid
	 */
	public static function is_valid_rating( $rating ) {
		$rating = floatval( $rating );
		return $rating >= 0 && $rating <= 5;
	}
}
