#nullable enable
using Managing.Common;

namespace Managing.Application.Bots.Models
{
    [GenerateSerializer]
    public class AgentGrainState
    {
        [Id(0)] public string AgentName { get; set; } = string.Empty;
        [Id(1)] public HashSet<Guid> BotIds { get; set; } = new HashSet<Guid>();

        /// <summary>
        /// Tracks if a swap operation is currently in progress to prevent multiple simultaneous swaps
        /// </summary>
        [Id(2)]
        public bool IsSwapInProgress { get; set; } = false;

        /// <summary>
        /// Timestamp of the last swap operation to implement cooldown period
        /// </summary>
        [Id(3)]
        public DateTime? LastSwapTime { get; set; } = null;

        /// <summary>
        /// Cached balance data to reduce external API calls
        /// </summary>
        [Id(4)]
        public CachedBalanceData? CachedBalanceData { get; set; } = null;

        /// <summary>
        /// Total fees paid by this agent across all positions
        /// </summary>
        [Id(5)]
        public decimal TotalFees { get; set; } = 0;
    }

    /// <summary>
    /// Cached balance data to avoid repeated external API calls
    /// </summary>
    [GenerateSerializer]
    public class CachedBalanceData
    {
        /// <summary>
        /// When the balance was last fetched
        /// </summary>
        [Id(0)]
        public DateTime LastFetched { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// The account name this balance data is for
        /// </summary>
        [Id(1)]
        public string AccountName { get; set; } = string.Empty;

        /// <summary>
        /// ETH balance in USD
        /// </summary>
        [Id(2)]
        public decimal EthValueInUsd { get; set; } = 0;

        /// <summary>
        /// USDC balance value
        /// </summary>
        [Id(3)]
        public decimal UsdcValue { get; set; } = 0;

        /// <summary>
        /// Whether the cached data is still valid (less than 1 minute old)
        /// </summary>
        public bool IsValid => DateTime.UtcNow - LastFetched < TimeSpan.FromMinutes(1);
    }

    /// <summary>
    /// Result of a balance check operation
    /// </summary>
    [GenerateSerializer]
    public class BalanceCheckResult
    {
        /// <summary>
        /// Whether the balance check was successful
        /// </summary>
        [Id(0)]
        public bool IsSuccessful { get; set; }

        /// <summary>
        /// The reason for failure if not successful
        /// </summary>
        [Id(1)]
        public Enums.BalanceCheckFailureReason FailureReason { get; set; }

        /// <summary>
        /// Additional details about the result
        /// </summary>
        [Id(2)]
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// Whether the bot should stop due to this result
        /// </summary>
        [Id(3)]
        public bool ShouldStopBot { get; set; }
    }
}