using Codex.Dal.Enums;

namespace Codex.Dal.Entities;

/// <summary>
/// Represents a single execution of an agent, tracking performance, tokens, and tool usage.
/// </summary>
public class AgentExecution
{
    /// <summary>
    /// Unique identifier for this execution
    /// </summary>
    public Guid Id { get; set; }

    /// <summary>
    /// Foreign key to the agent that was executed
    /// </summary>
    public Guid AgentId { get; set; }

    /// <summary>
    /// Foreign key to the conversation (if part of a conversation). Nullable for standalone executions.
    /// </summary>
    public Guid? ConversationId { get; set; }

    /// <summary>
    /// The user's input prompt
    /// </summary>
    public string UserPrompt { get; set; } = string.Empty;

    /// <summary>
    /// Additional input context or parameters (stored as JSON if needed)
    /// </summary>
    public string? Input { get; set; }

    /// <summary>
    /// The agent's generated output/response
    /// </summary>
    public string Output { get; set; } = string.Empty;

    /// <summary>
    /// When the execution started
    /// </summary>
    public DateTime StartedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// When the execution completed (null if still running)
    /// </summary>
    public DateTime? CompletedAt { get; set; }

    /// <summary>
    /// Total execution time in milliseconds
    /// </summary>
    public long? ExecutionTimeMs { get; set; }

    /// <summary>
    /// Number of tokens in the input/prompt
    /// </summary>
    public int? InputTokens { get; set; }

    /// <summary>
    /// Number of tokens in the output/response
    /// </summary>
    public int? OutputTokens { get; set; }

    /// <summary>
    /// Total tokens used (input + output)
    /// </summary>
    public int? TotalTokens { get; set; }

    /// <summary>
    /// Estimated cost in USD (null for Ollama/local models)
    /// </summary>
    public decimal? EstimatedCost { get; set; }

    /// <summary>
    /// Tool calls made during execution (stored as JSON array)
    /// </summary>
    public string? ToolCalls { get; set; }

    /// <summary>
    /// Results from tool executions (stored as JSON array for debugging)
    /// </summary>
    public string? ToolCallResults { get; set; }

    /// <summary>
    /// Current status of the execution
    /// </summary>
    public ExecutionStatus Status { get; set; } = ExecutionStatus.Running;

    /// <summary>
    /// Error message if execution failed
    /// </summary>
    public string? ErrorMessage { get; set; }

    // Navigation properties
    /// <summary>
    /// The agent that was executed
    /// </summary>
    public Agent Agent { get; set; } = null!;

    /// <summary>
    /// The conversation this execution belongs to (if applicable)
    /// </summary>
    public Conversation? Conversation { get; set; }

    /// <summary>
    /// Messages generated during this execution
    /// </summary>
    public ICollection<ConversationMessage> Messages { get; set; } = new List<ConversationMessage>();
}
