<?php
/**
 * Prompt Data Validator Trait
 *
 * Shared validation logic for prompt template data.
 * Used by both PromptTemplateController (free) and PremiumPromptTemplateService (premium).
 *
 * @package AIPT\Api\Traits
 * @since   2.6.0
 */

namespace AIPT\Api\Traits;

use WP_Error;

trait PromptDataValidatorTrait {

	/**
	 * Allowed generator type identifiers.
	 *
	 * @var string[]
	 */
	private $allowed_generator_types = [
		'description',
		'standard',
		'title',
		'faq',
		'seo_meta',
		'taxonomy',
	];

	/**
	 * Check if the given generator type is valid.
	 *
	 * @param string $generator_type Generator type to validate.
	 * @return bool
	 */
	private function is_valid_generator_type( string $generator_type ): bool {
		return in_array( $generator_type, $this->allowed_generator_types, true );
	}

	/**
	 * Validate and normalize prompt_data based on generator type.
	 *
	 * - description/standard: require system_prompt + user_prompt (additional_prompt optional)
	 * - title/faq/seo_meta: require additional_prompt only (system + user are built-in)
	 *
	 * @param mixed  $prompt_data    Raw prompt data from the request.
	 * @param string $generator_type Generator type for schema selection.
	 * @return array<string,string>|WP_Error Normalized prompt data or error.
	 */
	private function validate_prompt_data( $prompt_data, string $generator_type = '' ) {
		if ( ! is_array( $prompt_data ) ) {
			return new WP_Error(
				'invalid_prompt_data',
				__( 'prompt_data must be a JSON object.', 'ai-product-tools' ),
				[ 'status' => 400 ]
			);
		}

		$system_prompt     = $prompt_data['system_prompt'] ?? null;
		$user_prompt       = $prompt_data['user_prompt'] ?? null;
		$additional_prompt = $prompt_data['additional_prompt'] ?? null;

		$uses_additional_only = in_array( $generator_type, [ 'title', 'faq', 'seo_meta', 'taxonomy' ], true );

		if ( $uses_additional_only ) {

			if ( ! is_string( $additional_prompt ) || '' === trim( $additional_prompt ) ) {
				return new WP_Error(
					'invalid_prompt_schema',
					__( 'additional_prompt must not be empty for title/faq/seo_meta templates.', 'ai-product-tools' ),
					[ 'status' => 400 ]
				);
			}

			$normalized = [
				'system_prompt'     => is_string( $system_prompt ) ? $system_prompt : '',
				'user_prompt'       => is_string( $user_prompt ) ? $user_prompt : '',
				'additional_prompt' => $additional_prompt,
			];
		} else {

			if ( ! is_string( $system_prompt ) || ! is_string( $user_prompt ) ) {
				return new WP_Error(
					'invalid_prompt_schema',
					__( 'prompt_data must include system_prompt and user_prompt as strings.', 'ai-product-tools' ),
					[ 'status' => 400 ]
				);
			}

			if ( '' === trim( $system_prompt ) && '' === trim( $user_prompt ) ) {
				return new WP_Error(
					'invalid_prompt_schema',
					__( 'At least one prompt field (system or user) must have content for this generator type.', 'ai-product-tools' ),
					[ 'status' => 400 ]
				);
			}

			if ( null !== $additional_prompt && ! is_string( $additional_prompt ) ) {
				return new WP_Error(
					'invalid_prompt_schema',
					__( 'additional_prompt must be a string when provided.', 'ai-product-tools' ),
					[ 'status' => 400 ]
				);
			}

			$normalized = [
				'system_prompt' => $system_prompt,
				'user_prompt'   => $user_prompt,
			];

			if ( null !== $additional_prompt ) {
				$normalized['additional_prompt'] = $additional_prompt;
			}
		}

		$encoded = wp_json_encode( $normalized );
		if ( ! is_string( $encoded ) || strlen( $encoded ) > 102400 ) {
			return new WP_Error(
				'prompt_data_too_large',
				__( 'prompt_data exceeds the maximum allowed size of 100KB.', 'ai-product-tools' ),
				[ 'status' => 400 ]
			);
		}

		return $normalized;
	}
}
