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

#pragma once

#include "CoreMinimal.h"
#include "Graph/InworldGraphRuntimeData.h"
#include "UObject/Interface.h"

#include "InworldConversationTarget.generated.h"

struct FInworldDataHandle;
class UInworldBasePlayerComponent;

/**
 * Delegate fired when a character sends a message in a conversation.
 * Provides the conversation target, character component, message ID, and message content.
 */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams(FOnInworldConversationCharacterMessage, TScriptInterface<IInworldConversationTarget>,
	ConversationTarget, class UInworldSimpleCharacterComponent*, CharacterComponent, const FString&, MessageId, const FString&,
	Message);

/** Native delegate fired when a character sends a message in a conversation */
DECLARE_MULTICAST_DELEGATE_FourParams(FOnInworldConversationCharacterMessageNative,
	TScriptInterface<IInworldConversationTarget> /*ConversationTarget*/,
	class UInworldSimpleCharacterComponent* /*CharacterComponent*/, const FString& /*MessageId*/, const FString& /*Message*/);

/**
 * Delegate fired when a player sends a message in a conversation.
 * Provides the conversation target, player profile, message ID, and message data handle.
 */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams(FOnInworldConversationPlayerMessage, TScriptInterface<IInworldConversationTarget>,
	ConversationTarget, const FInworldPlayerProfile&, PlayerProfile, const FString&, MessageId, const FInworldDataHandle&, Message);

/** Native delegate fired when a player sends a message in a conversation */
DECLARE_MULTICAST_DELEGATE_FourParams(FOnInworldConversationPlayerMessageNative,
	TScriptInterface<IInworldConversationTarget> /*ConversationTarget*/, const FInworldPlayerProfile& /*PlayerProfile*/,
	const FString& /*MessageId*/, const FInworldDataHandle& /*Message*/);

/**
 * Unreal Engine interface wrapper for IInworldConversationTarget.
 * This is required by Unreal's reflection system to expose the interface to Blueprint.
 */
UINTERFACE(MinimalAPI, Blueprintable)
class UInworldConversationTarget : public UInterface
{
	GENERATED_BODY()
};

/**
 * Interface for entities that can be targets of Inworld conversations.
 *
 * This interface defines the contract for any object that can participate in
 * Inworld conversations as a recipient of player messages and interactions.
 * Implementing classes must provide functionality for player targeting,
 * conversation control, and message handling.
 *
 * Key Responsibilities:
 * - Player target management (setting and removing player connections)
 * - Conversation interruption handling
 * - Audio and text message processing
 * - Integration with Inworld conversation system
 *
 * Typical Implementers:
 * - Individual character components (UInworldBaseCharacterComponent)
 * - Multi-character conversation groups (UInworldConversationGroup)
 * - Custom conversation handlers
 */
class INWORLDCHARACTER_API IInworldConversationTarget
{
	GENERATED_BODY()

public:
	/**
	 * Interrupts the current conversation or ongoing speech.
	 *
	 * This method should immediately stop any current speech synthesis,
	 * cancel pending responses, and prepare the conversation target for
	 * new input or interactions.
	 */
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Inworld")
	void Interrupt();

	/**
	 * Sends a message to this conversation target.
	 *
	 * The message contains data that should be processed by the
	 * conversation target's AI system to generate appropriate text or speech responses.
	 *
	 * @param Message A data handle holding the message
	 * @return An FString identifier for the sent message
	 */
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Inworld")
	FString SendMessage(const FInworldDataHandle& Message);

	/**
	 * Sends a message to this conversation target with player information.
	 *
	 * The message contains data that should be processed by the
	 * conversation target's AI system to generate appropriate text or speech responses.
	 *
	 * @param Message A data handle holding the message
	 * @param PlayerProfile Runtime data that holds information about the player that sent the message
	 * @return An FString identifier for the sent message
	 */
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Inworld")
	FString SendPlayerMessage(const FInworldDataHandle& Message, const FInworldPlayerProfile& PlayerProfile);

	/**
	 * Sends a message to this conversation target with runtime data.
	 *
	 * The message contains data that should be processed by the
	 * conversation target's AI system to generate appropriate text or speech responses.
	 *
	 * @param Message A data handle holding the message
	 * @param RuntimeData The runtime data
	 * @return An FString identifier for the sent message
	 */
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Inworld")
	FString SendDataMessage(const FInworldDataHandle& Message, const TMap<FName, FInworldDataHandle>& RuntimeData);
};
