using Codex.Dal.Enums;

namespace Codex.Dal.Entities;

/// <summary>
/// Represents a single message in a conversation.
/// Messages are stored permanently for audit trail, with IsInActiveWindow for efficient memory management.
/// </summary>
public class ConversationMessage
{
    /// <summary>
    /// Unique identifier for the message
    /// </summary>
    public Guid Id { get; set; }

    /// <summary>
    /// Foreign key to the conversation
    /// </summary>
    public Guid ConversationId { get; set; }

    /// <summary>
    /// Role of the message sender
    /// </summary>
    public MessageRole Role { get; set; }

    /// <summary>
    /// Content of the message
    /// </summary>
    public string Content { get; set; } = string.Empty;

    /// <summary>
    /// Tool calls made in this message (stored as JSON array if applicable)
    /// </summary>
    public string? ToolCalls { get; set; }

    /// <summary>
    /// Tool results from this message (stored as JSON array if applicable)
    /// </summary>
    public string? ToolResults { get; set; }

    /// <summary>
    /// Order of the message in the conversation (0-indexed)
    /// </summary>
    public int MessageIndex { get; set; }

    /// <summary>
    /// Whether this message is within the active conversation window for LLM context.
    /// System messages are always in the active window.
    /// For user/assistant/tool messages, only the last N are in the window (N = Agent.ConversationWindowSize).
    /// </summary>
    public bool IsInActiveWindow { get; set; } = true;

    /// <summary>
    /// Estimated token count for this message
    /// </summary>
    public int? TokenCount { get; set; }

    /// <summary>
    /// Foreign key to the execution that generated this message (nullable for user messages)
    /// </summary>
    public Guid? ExecutionId { get; set; }

    /// <summary>
    /// When this message was created
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    // Navigation properties
    /// <summary>
    /// The conversation this message belongs to
    /// </summary>
    public Conversation Conversation { get; set; } = null!;

    /// <summary>
    /// The execution that generated this message (if applicable)
    /// </summary>
    public AgentExecution? Execution { get; set; }
}
