/**
 * Copyright 2022-2025 Theai, Inc. dba Inworld AI
 */

#pragma once

#include "CoreMinimal.h"
#include "InworldNode_Custom.h"
#include "Primitives/Intent/InworldIntentTypes.h"

namespace inworld
{
class TextEmbedderInterface;
class LLMInterface;
class vector_float;
namespace graph
{
struct CompileContext;
}	 // namespace graph
}	 // namespace inworld

class UInworldGraphRuntimeData_Intent;

#include "InworldNode_Intent.generated.h"

/**
 * @class UInworldNode_Intent
 * @brief A node that matches user input against predefined intents.
 *
 * This node takes text input and matches it against a set of predefined intents
 * to determine the user's intended action or query. It inherits from TypedNode
 * with MatchedIntents as output and Text as input.
 *
 * @input FInworldData_Text (Input to extract intents from)
 * @output FInworldData_MatchedIntents
 */
UCLASS()
class INWORLDRUNTIME_API UInworldNode_Intent : public UInworldNode_Custom
{
	GENERATED_BODY()

public:
	/**
	 * @brief Default constructor for the intent matching node.
	 *
	 * Initializes the node with default settings for intent recognition processing.
	 */
	UInworldNode_Intent();

	/**
	 * @brief Native utility function. Creates a new Intent node instance with the specified configuration
	 * @param Outer The UObject that will own this node
	 * @param NodeName The name to assign to the node
	 * @param CreationConfig Configuration for creating the node
	 * @param ExecutionConfig Configuration for executing the node
	 * @return Newly created Intent node instance
	 */
	static UInworldNode_Intent* CreateNative(class UObject* Outer, const FString& NodeName,
		const FInworldIntentNodeCreationConfig& CreationConfig, const FInworldIntentNodeExecutionConfig& ExecutionConfig);

	/** Configuration settings for intent node creation including embedder component and LLM matcher settings */
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Inworld",
		Meta = (ToolTip = "Configuration settings for intent node creation including embedder component and LLM matcher settings"))
	FInworldIntentNodeCreationConfig CreationConfig;

	/** Configuration settings for intent node execution including similarity thresholds and top-N results */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Intent Runtime Data",
		Meta = (ToolTip = "Configuration settings for intent node execution including similarity thresholds and top-N results"))
	FInworldIntentNodeExecutionConfig ExecutionConfig;

protected:
	/**
	 * @brief Performs strict text matching against intent phrases.
	 *
	 * This function matches the input text directly against predefined intent phrases
	 * using exact string matching. It's used for cases where precise text matching
	 * is preferred over similarity-based approaches.
	 *
	 * @param TextInput The input text to match against intent phrases
	 * @param IntentData The intent data containing predefined phrases and metadata
	 * @param OutMatches Output array of intent matches with confidence scores
	 */
	void StrictMatch(
		const FString& TextInput, const FInworldIntentsData& IntentData, TArray<FInworldIntentMatch>& OutMatches) const;

	/**
	 * @brief Performs semantic matching using text embeddings.
	 *
	 * This function uses text embeddings to find semantically similar intents
	 * based on vector similarity calculations. It provides more flexible matching
	 * that can handle paraphrasing and similar concepts.
	 *
	 * @param InputEmbedding The text embedding vector of the input text
	 * @param IntentData The intent data containing embeddings and metadata
	 * @param OutMatches Output array of intent matches with similarity scores
	 */
	void MatchWithEmbeddings(
		const TArray<float>& InputEmbedding, const FInworldIntentsData& IntentData, TArray<FInworldIntentMatch>& OutMatches) const;

private:
	/**
	 * @brief Processes text input to identify matching intents using available intent data.
	 *
	 * This function performs the core intent matching logic by analyzing the input text
	 * against the provided intent database. It uses both strict matching and embedding-based
	 * semantic matching to identify the most relevant intents.
	 *
	 * @param ProcessContext Runtime context containing execution state and runtime data
	 * @param Text The input text to analyze for intent matching
	 * @param Intents The intent database containing phrases, embeddings, and metadata
	 * @return Data structure containing matched intents with confidence scores
	 */
	UFUNCTION()
	virtual UPARAM(meta = (DisplayName = "Matched Intents")) FInworldData_MatchedIntents Process_Default(
		const UInworldProcessContext* ProcessContext, const FInworldData_Text& Text, const FInworldIntentsData& Intents) const;

	/**
	 * @brief Generates JSON configuration for the intent matching node.
	 *
	 * This override generates the JSON configuration needed for the intent node's
	 * processing operations, including embedder settings, LLM matcher configuration,
	 * and similarity thresholds.
	 *
	 * @param GraphJson The JSON object to populate with the node's configuration data
	 * @return Error code indicating success or failure of configuration generation
	 */
	virtual EInworldRuntimeErrors GetJsonConfig(const TSharedRef<FJsonObject>& GraphJson) override;
};
