<?php
/**
 * Base Controller for Description Generator
 *
 * Extends shared BulkGenerator infrastructure with description-specific
 * helpers: option sanitization, content type extraction.
 *
 * @package AIPT\Api\BulkGenerator\Desc\Controllers
 * @since 2.5.0
 */

namespace AIPT\Api\BulkGenerator\Desc\Controllers;

use AIPT\Api\BulkGenerator\BaseBulkGeneratorController;

/**
 * Abstract base for description generator controllers.
 */
abstract class BaseController extends BaseBulkGeneratorController {

	protected const RATE_LIMIT_SCOPE = 'description';

	/**
	 * Sanitize generation options from request.
	 *
	 * @param array $options Raw options from request.
	 * @return array Sanitized options
	 */
	protected function sanitize_generation_options( array $options ): array {
		$sanitized = array();

		$bool_keys = array(
			'generateShortDescription',
			'generateDescription',
			'generateTags',
			'improveExistingDescription',
			'keepExistingDescription',

			'keepExistingTags',
			'enable_multi_generation',
		);
		foreach ( $bool_keys as $key ) {
			if ( isset( $options[ $key ] ) ) {
				$sanitized[ $key ] = (bool) $options[ $key ];
			}
		}

		$string_keys = array(
			'provider',
			'model',
			'language',
			'writingStyle',
			'writing_style',
			'existingDescriptionPosition',

			'content_length_preset',
			'short_length_preset',
		);
		foreach ( $string_keys as $key ) {
			if ( isset( $options[ $key ] ) ) {
				$sanitized[ $key ] = sanitize_text_field( $options[ $key ] );
			}
		}

		$int_keys = array(
			'max_length',
			'max_short_length',
			'max_tags_count',
			'additionalTagsCount',
		);
		foreach ( $int_keys as $key ) {
			if ( isset( $options[ $key ] ) ) {
				$sanitized[ $key ] = absint( $options[ $key ] );
			}
		}

		if ( isset( $options['prompts'] ) && is_array( $options['prompts'] ) ) {
			$sanitized['prompts'] = array();
			foreach ( array( 'short', 'full' ) as $type ) {
				if ( isset( $options['prompts'][ $type ] ) && is_array( $options['prompts'][ $type ] ) ) {
					$sanitized['prompts'][ $type ] = array();
					if ( isset( $options['prompts'][ $type ]['system_prompt'] ) ) {
						$sanitized['prompts'][ $type ]['system_prompt'] = sanitize_textarea_field(
							$options['prompts'][ $type ]['system_prompt']
						);
					}
					if ( isset( $options['prompts'][ $type ]['user_prompt'] ) ) {
						$sanitized['prompts'][ $type ]['user_prompt'] = sanitize_textarea_field(
							$options['prompts'][ $type ]['user_prompt']
						);
					}
				}
			}
		}

		if ( isset( $options['beforeContentText'] ) ) {
			$sanitized['beforeContentText'] = sanitize_text_field( $options['beforeContentText'] );
		}
		if ( isset( $options['afterContentText'] ) ) {
			$sanitized['afterContentText'] = sanitize_text_field( $options['afterContentText'] );
		}

		return $sanitized;
	}

	/**
	 * Extract content types from options array.
	 *
	 * @param array $options Options with generation flags.
	 * @return array Content types to generate
	 */
	protected function extract_content_types( array $options ): array {
		$types = array();

		if ( ! empty( $options['generateShortDescription'] ) ) {
			$types[] = 'short_description';
		}
		if ( ! empty( $options['generateDescription'] ) ) {
			$types[] = 'description';
		}
		if ( ! empty( $options['generateTags'] ) ) {
			$types[] = 'tags';
		}

		return $types;
	}
}
