<?php
/**
 * Wand Features Class
 *
 * Manages magic wand features and actions
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Wand Features class
 */
class SmartyPress_AI_Wand_Features {

	/**
	 * Cached features array
	 *
	 * @var array|null
	 */
	private static $features_cache = null;

	/**
	 * Build features array with translated strings
	 *
	 * @return array Features array.
	 */
	private static function build_features() {
		return array(
			'generateContent'  => array(
				'label'          => __( 'Generate Content', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'With Custom Prompt', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'prompt', 'context' ),
				'where'          => 'blockContext',
				'mode'           => 'replace',
				'default_prompt' => __( 'Generate engaging content about: {TOPIC}', 'smartypress-ai-engine' ),
				'placeholder'    => __( 'Enter topic or context...', 'smartypress-ai-engine' ),
			),
			'enhanceText'      => array(
				'label'          => __( 'Enhance Text', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'Improve Quality', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'text', 'prompt' ),
				'where'          => 'blockContext',
				'mode'           => 'replace',
				'default_prompt' => __( "Enhance this text by improving readability and quality. Write in {LANGUAGE}. Return ONLY the enhanced text.\n\n{TEXT}", 'smartypress-ai-engine' ),
				'placeholder'    => __( 'Optional: Specify improvements...', 'smartypress-ai-engine' ),
			),
			'correctText'      => array(
				'label'          => __( 'Correct Text', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'Grammar & Spelling', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'text' ),
				'where'          => 'blockContext',
				'mode'           => 'replace',
				'default_prompt' => __( "Correct grammar and spelling mistakes. Write in {LANGUAGE}. Return ONLY the corrected text.\n\n{TEXT}", 'smartypress-ai-engine' ),
				'placeholder'    => __( 'Leave empty for auto-correct...', 'smartypress-ai-engine' ),
			),
			'longerText'       => array(
				'label'          => __( 'Longer Text', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'Expand Content', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'text' ),
				'where'          => 'blockContext',
				'mode'           => 'replace',
				'default_prompt' => __( "Expand this text to make it longer and more detailed. Write in {LANGUAGE}. Return ONLY the expanded text.\n\n{TEXT}", 'smartypress-ai-engine' ),
				'placeholder'    => '',
			),
			'shorterText'      => array(
				'label'          => __( 'Shorter Text', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'Condense Content', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'text' ),
				'where'          => 'blockContext',
				'mode'           => 'replace',
				'default_prompt' => __( "Condense this text while keeping key information. Write in {LANGUAGE}. Return ONLY the condensed text.\n\n{TEXT}", 'smartypress-ai-engine' ),
				'placeholder'    => '',
			),
			'translateText'    => array(
				'label'          => __( 'Translate Text', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'To Another Language', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'text', 'language' ),
				'where'          => 'blockContext',
				'mode'           => 'replace',
				'default_prompt' => __( 'Translate to {LANGUAGE}: {TEXT}', 'smartypress-ai-engine' ),
				'placeholder'    => __( 'Target language...', 'smartypress-ai-engine' ),
			),
			'generateTitle'    => array(
				'label'          => __( 'Generate Title', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'SEO-Optimized', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'prompt' ),
				'where'          => 'postActions',
				'mode'           => 'suggest',
				'results'        => 5,
				'default_prompt' => __( 'Generate SEO-optimized title in {LANGUAGE} for content about: {TOPIC}', 'smartypress-ai-engine' ),
				'placeholder'    => __( 'Enter topic...', 'smartypress-ai-engine' ),
			),
			'generateExcerpt'  => array(
				'label'          => __( 'Generate Excerpt', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'Short Description', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'content' ),
				'where'          => 'postActions',
				'mode'           => 'suggest',
				'results'        => 3,
				'default_prompt' => __( 'Generate concise excerpt (max 150 chars) in {LANGUAGE} for: {CONTENT}', 'smartypress-ai-engine' ),
				'placeholder'    => '',
			),
			'generateAlt'      => array(
				'label'          => __( 'Generate Alt Text', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'For Images', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'context' ),
				'where'          => 'mediaContext',
				'mode'           => 'replace',
				'default_prompt' => __( 'Generate descriptive alt text in {LANGUAGE} for image about: {CONTEXT}', 'smartypress-ai-engine' ),
				'placeholder'    => __( 'Describe the image...', 'smartypress-ai-engine' ),
			),
			'suggestSynonyms'  => array(
				'label'          => __( 'Suggest Synonyms', 'smartypress-ai-engine' ),
				'sublabel'       => __( 'For Selected Words', 'smartypress-ai-engine' ),
				'arguments'      => array( 'postId', 'text', 'selectedText' ),
				'where'          => 'blockContext',
				'mode'           => 'suggest',
				'results'        => 5,
				'default_prompt' => __( 'Provide 5 synonyms in {LANGUAGE} for: {SELECTED_TEXT}', 'smartypress-ai-engine' ),
				'placeholder'    => '',
			),
		);
	}

	/**
	 * Get all features
	 *
	 * @return array Features array.
	 */
	public static function get_features() {
		if ( null === self::$features_cache ) {
			self::$features_cache = self::build_features();
		}
		return apply_filters( 'smartypress_ai_wand_features', self::$features_cache );
	}

	/**
	 * Get specific feature
	 *
	 * @param string $action Action name.
	 * @return array|null Feature data or null.
	 */
	public static function get_feature( $action ) {
		$features = self::get_features();
		return isset( $features[ $action ] ) ? $features[ $action ] : null;
	}

	/**
	 * Get features by context
	 *
	 * @param string $where Context (blockContext, postActions, mediaContext).
	 * @return array Filtered features.
	 */
	public static function get_features_by_context( $where ) {
		$features = self::get_features();
		$filtered = array();

		foreach ( $features as $action => $feature ) {
			if ( isset( $feature['where'] ) && $feature['where'] === $where ) {
				$filtered[ $action ] = $feature;
			}
		}

		return $filtered;
	}

	/**
	 * Check if action is valid
	 *
	 * @param string $action Action name.
	 * @return bool True if valid.
	 */
	public static function is_valid_action( $action ) {
		$features = self::get_features();
		return isset( $features[ $action ] );
	}

	/**
	 * Get default prompt for action
	 *
	 * @param string $action Action name.
	 * @param array  $placeholders Placeholder values.
	 * @return string Prompt with placeholders replaced.
	 */
	public static function get_default_prompt( $action, $placeholders = array() ) {
		$feature = self::get_feature( $action );
		
		if ( ! $feature || ! isset( $feature['default_prompt'] ) ) {
			return '';
		}

		$prompt = $feature['default_prompt'];

		// Replace placeholders
		foreach ( $placeholders as $key => $value ) {
			$prompt = str_replace( '{' . strtoupper( $key ) . '}', $value, $prompt );
		}

		return $prompt;
	}
}

