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

#pragma once

#include "CoreMinimal.h"
#include "Graph/InworldGraphRuntimeData.h"
#include "InworldCharacterTypes.h"

#include "InworldGraphRuntimeData_RelationState.generated.h"

/**
 * @class UInworldGraphRuntimeData_RelationState
 * @brief Runtime data component for managing character relationship states.
 *
 * This runtime data component tracks and manages the relationship state between
 * a character and another entity (typically the player). Relationship states include
 * multi-dimensional metrics like trust, respect, familiarity, flirtation, and attraction
 * that influence how characters interact and respond in conversations.
 */
UCLASS(BlueprintType, meta = (DisplayName = "RelationState Data"))
class INWORLDCHARACTER_API UInworldGraphRuntimeData_RelationState : public UInworldGraphRuntimeData
{
	GENERATED_BODY()

public:
	/**
	 * @brief Default constructor for the relation state runtime data.
	 */
	UInworldGraphRuntimeData_RelationState();

	/**
	 * @brief Retrieves the relation state as a data handle for graph processing.
	 * @return Data handle containing the current relationship state.
	 */
	virtual FInworldDataHandle GetData() override;

	/**
	 * @brief Retrieves the complete relationship state.
	 * @return The full relationship state structure with all metrics.
	 */
	UFUNCTION(BlueprintCallable, Category = "Inworld|Relation State Runtime Data")
	FInworldRelation GetRelation() const
	{
		return Relation;
	}

	/**
	 * @brief Retrieves the primary relationship label derived from the metrics.
	 * @return The relationship label enum (e.g., FRIENDLY, HOSTILE, NEUTRAL).
	 */
	UFUNCTION(BlueprintCallable, Category = "Inworld|Relation State Runtime Data")
	EInworldRelationLabel GetRelationshipLabel() const
	{
		return Relation.GetLabel();
	}

	/**
	 * @brief Retrieves the relationship label as a human-readable string.
	 * @return String representation of the relationship label.
	 */
	UFUNCTION(BlueprintCallable, Category = "Inworld|Relation State Runtime Data")
	FString GetRelationshipLabelString() const
	{
		return Relation.GetLabelString();
	}

	/**
	 * @brief Updates the relationship state with new relation data.
	 * @param Relation The new relationship state to apply.
	 */
	UFUNCTION(BlueprintCallable, Category = "Inworld|Relation State Runtime Data")
	void UpdateRelation(const FInworldRelation& Relation);

	/**
	 * @brief Updates the relationship state by parsing an AI-generated text response.
	 * @param TextResponse The text response to parse for relationship information.
	 */
	void UpdateRelation(const FString& TextResponse);

private:
	/**
	 * @brief Parses a text response to extract relationship state information.
	 * @param TextResponse The text response to parse.
	 * @return Optional relationship state if successfully parsed, empty otherwise.
	 */
	TOptional<FInworldRelation> ParseRelationResponse(const FString& TextResponse) const;

	/**
	 * @brief Parses a single line to extract a relationship attribute and its value.
	 * @param Line The text line to parse.
	 * @return Optional pair of attribute name and value if successfully parsed, empty otherwise.
	 */
	TOptional<TPair<FString, int32>> ParseRelationAttribute(const FString& Line) const;

protected:
	/** The current relationship state including all dimensional metrics (trust, respect, etc.) */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Relation State Runtime Data",
		meta = (ExposeOnSpawn = "true",
			ToolTip = "The current relationship state including all dimensional metrics (trust, respect, etc.)"))
	FInworldRelation Relation;
};
