<?php
/**
 * Review Validator
 *
 * @package Ugcify\Review
 */

namespace Ugcify\Review;

use WP_Error;

/**
 * Class ReviewValidator
 */
class ReviewValidator {
	/**
	 * Valid statuses
	 *
	 * @var array
	 */
	private $valid_statuses = array( 'pending', 'approved', 'rejected', 'spam' );

	/**
	 * Valid types
	 *
	 * @var array
	 */
	private $valid_types = array( 'text', 'image', 'video', 'mixed' );

	/**
	 * Validate review data
	 *
	 * @param array $data Review data to validate.
	 * @return true|WP_Error True if valid, WP_Error if invalid.
	 */
	public function validate( array $data ) {
		// Validate product_id.
		if ( ! isset( $data['product_id'] ) || ! is_numeric( $data['product_id'] ) ) {
			return new WP_Error(
				'invalid_product_id',
				__( 'Product ID is required and must be numeric.', 'ugcify' )
			);
		}

		if ( $data['product_id'] <= 0 ) {
			return new WP_Error(
				'invalid_product_id',
				__( 'Product ID must be a positive number.', 'ugcify' )
			);
		}

		// Validate user_id or guest details.
		$has_user_id = ! empty( $data['user_id'] ) && is_numeric( $data['user_id'] );
		
		if ( ! $has_user_id ) {
			// Guest user - require name and email.
			if ( empty( $data['guest_name'] ) ) {
				return new WP_Error(
					'missing_guest_name',
					__( 'Guest name is required for non-logged-in users.', 'ugcify' )
				);
			}

			if ( empty( $data['guest_email'] ) ) {
				return new WP_Error(
					'missing_guest_email',
					__( 'Guest email is required for non-logged-in users.', 'ugcify' )
				);
			}

			// Validate email format.
			if ( ! is_email( $data['guest_email'] ) ) {
				return new WP_Error(
					'invalid_guest_email',
					__( 'Please provide a valid email address.', 'ugcify' )
				);
			}
		}

		// Validate rating.
		if ( isset( $data['rating'] ) ) {
			if ( ! is_numeric( $data['rating'] ) ) {
				return new WP_Error(
					'invalid_rating',
					__( 'Rating must be numeric.', 'ugcify' )
				);
			}

			$rating = (int) $data['rating'];
			if ( $rating < 1 || $rating > 5 ) {
				return new WP_Error(
					'invalid_rating',
					__( 'Rating must be between 1 and 5.', 'ugcify' )
				);
			}
		}

		// Validate status.
		if ( isset( $data['status'] ) && ! in_array( $data['status'], $this->valid_statuses, true ) ) {
			return new WP_Error(
				'invalid_status',
				sprintf(
					/* translators: %s: valid status values */
					__( 'Status must be one of: %s.', 'ugcify' ),
					implode( ', ', $this->valid_statuses )
				)
			);
		}

		// Validate type (if provided - will be auto-determined if not).
		if ( isset( $data['type'] ) && ! in_array( $data['type'], $this->valid_types, true ) ) {
			return new WP_Error(
				'invalid_type',
				sprintf(
					/* translators: %s: valid type values */
					__( 'Type must be one of: %s.', 'ugcify' ),
					implode( ', ', $this->valid_types )
				)
			);
		}

		// Validate content (optional - can be empty for media-only reviews).
		// No content validation needed as per new requirements.

		return true;
	}

	/**
	 * Validate rating value
	 *
	 * @param int $rating Rating value.
	 * @return true|WP_Error
	 */
	public function validate_rating( $rating ) {
		if ( ! is_numeric( $rating ) ) {
			return new WP_Error(
				'invalid_rating',
				__( 'Rating must be numeric.', 'ugcify' )
			);
		}

		$rating = (int) $rating;
		if ( $rating < 1 || $rating > 5 ) {
			return new WP_Error(
				'invalid_rating',
				__( 'Rating must be between 1 and 5.', 'ugcify' )
			);
		}

		return true;
	}

	/**
	 * Validate status value
	 *
	 * @param string $status Status value.
	 * @return true|WP_Error
	 */
	public function validate_status( $status ) {
		if ( ! in_array( $status, $this->valid_statuses, true ) ) {
			return new WP_Error(
				'invalid_status',
				sprintf(
					/* translators: %s: valid status values */
					__( 'Status must be one of: %s.', 'ugcify' ),
					implode( ', ', $this->valid_statuses )
				)
			);
		}

		return true;
	}
}
