<?php
/**
 * Individual Review Schema Management
 *
 * Manages individual Review items for detailed review schema.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Review {

	/**
	 * Get review field definitions for form builder
	 *
	 * @return array Field definitions for reviews section
	 */
	public static function get_field_definitions() {
		$fields = array();

		// Add fields for up to 3 featured reviews
		for ( $i = 1; $i <= 3; $i++ ) {
			$fields[] = array(
				'section'   => 'reviews_schema',
				'name'      => 'review_author_' . $i,
				'label'     => sprintf( __( 'Review Author %d', 'bw-schema' ), $i ),
				'type'      => 'text',
				'help'      => __( 'Name of the reviewer (e.g., "John Smith")', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			);

			$fields[] = array(
				'section'   => 'reviews_schema',
				'name'      => 'review_rating_' . $i,
				'label'     => sprintf( __( 'Review Rating %d', 'bw-schema' ), $i ),
				'type'      => 'number',
				'help'      => __( 'Rating 1-5 (e.g., "5")', 'bw-schema' ),
				'sanitize'  => 'floatval',
			);

			$fields[] = array(
				'section'   => 'reviews_schema',
				'name'      => 'review_text_' . $i,
				'label'     => sprintf( __( 'Review Text %d', 'bw-schema' ), $i ),
				'type'      => 'textarea',
				'help'      => __( 'The review content (max 5000 characters)', 'bw-schema' ),
				'sanitize'  => 'sanitize_textarea_field',
				'rows'      => 3,
			);

			$fields[] = array(
				'section'   => 'reviews_schema',
				'name'      => 'review_date_' . $i,
				'label'     => sprintf( __( 'Review Date %d', 'bw-schema' ), $i ),
				'type'      => 'date',
				'help'      => __( 'Date published (YYYY-MM-DD)', 'bw-schema' ),
				'sanitize'  => 'sanitize_text_field',
			);
		}

		return $fields;
	}

	/**
	 * Format reviews for schema.org Review
	 *
	 * @param array $org_data Organization data
	 * @return array Array of Review schemas
	 */
	public static function format_for_schema( $org_data ) {
		$reviews = array();

		// Collect all reviews
		for ( $i = 1; $i <= 3; $i++ ) {
			$author_key = 'review_author_' . $i;
			$rating_key = 'review_rating_' . $i;
			$text_key = 'review_text_' . $i;
			$date_key = 'review_date_' . $i;

			if ( ! empty( $org_data[ $author_key ] ) && ! empty( $org_data[ $rating_key ] ) ) {
				$rating = floatval( $org_data[ $rating_key ] );

				// Validate rating
				if ( $rating < 1 || $rating > 5 ) {
					continue;
				}

				$review = array(
					'@type'        => 'Review',
					'author'       => array(
						'@type' => 'Person',
						'name'  => $org_data[ $author_key ],
					),
					'reviewRating' => array(
						'@type'       => 'Rating',
						'ratingValue' => $rating,
						'bestRating'  => 5,
						'worstRating' => 1,
					),
				);

				// Add review text if available
				if ( ! empty( $org_data[ $text_key ] ) ) {
					$review['reviewBody'] = sanitize_textarea_field( $org_data[ $text_key ] );
				}

				// Add date if available
				if ( ! empty( $org_data[ $date_key ] ) ) {
					$review['datePublished'] = $org_data[ $date_key ];
				}

				$reviews[] = $review;
			}
		}

		return $reviews;
	}

	/**
	 * Get review count
	 *
	 * @param array $org_data Organization data
	 * @return int Number of reviews configured
	 */
	public static function count_reviews( $org_data ) {
		$count = 0;
		for ( $i = 1; $i <= 3; $i++ ) {
			if ( ! empty( $org_data[ 'review_author_' . $i ] ) ) {
				$count++;
			}
		}
		return $count;
	}
}
