using Managing.Domain.Bots;
using Managing.Domain.Candles;
using Managing.Domain.Indicators;
using Managing.Domain.Trades;
using Managing.Domain.Users;

namespace Managing.Application.Bots;

/// <summary>
/// Orleans grain state for TradingBot.
/// This class represents the persistent state of a trading bot grain.
/// All properties must be serializable for Orleans state management.
/// </summary>
[GenerateSerializer]
public class TradingBotGrainState
{
    /// <summary>
    /// The trading bot configuration
    /// </summary>
    [Id(0)]
    public TradingBotConfig Config { get; set; } = new();

    /// <summary>
    /// Collection of trading signals generated by the bot
    /// </summary>
    [Id(1)]
    public Dictionary<string, LightSignal> Signals { get; set; } = new();

    /// <summary>
    /// Dictionary of trading positions opened by the bot, keyed by position identifier
    /// </summary>
    [Id(2)]
    public Dictionary<Guid, Position> Positions { get; set; } = new();

    /// <summary>
    /// Historical wallet balances tracked over time
    /// </summary>
    [Id(3)]
    public Dictionary<DateTime, decimal> WalletBalances { get; set; } = new();

    /// <summary>
    /// When the bot was started
    /// </summary>
    [Id(5)]
    public DateTime StartupTime { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// When the bot was created
    /// </summary>
    [Id(6)]
    public DateTime CreateDate { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// The user who owns this bot
    /// </summary>
    [Id(7)]
    public User User { get; set; }

    /// <summary>
    /// Bot execution counter
    /// </summary>
    [Id(8)]
    public long ExecutionCount { get; set; } = 0;

    /// <summary>
    /// Bot identifier/name
    /// </summary>
    [Id(9)]
    public Guid Identifier { get; set; } = Guid.Empty;

    /// <summary>
    /// Bot display name
    /// </summary>
    [Id(10)]
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// Preload start date for candles
    /// </summary>
    [Id(11)]
    public DateTime PreloadSince { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// Number of preloaded candles
    /// </summary>
    [Id(12)]
    public int PreloadedCandlesCount { get; set; } = 0;

    /// <summary>
    /// Timer interval for bot execution
    /// </summary>
    [Id(13)]
    public int Interval { get; set; } = 60000; // Default 1 minute

    /// <summary>
    /// Maximum number of signals to keep in memory
    /// </summary>
    [Id(14)]
    public int MaxSignals { get; set; } = 10;

    /// <summary>
    /// Indicates if the bot has been initialized
    /// </summary>
    [Id(15)]
    public bool IsInitialized { get; set; } = false;

    /// <summary>
    /// Last time the bot state was persisted
    /// </summary>
    [Id(16)]
    public DateTime LastBackupTime { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// Last time a position was closed (for cooldown period tracking)
    /// </summary>
    [Id(17)]
    public DateTime? LastPositionClosingTime { get; set; }

    /// <summary>
    /// The last candle data used for trading decisions
    /// </summary>
    [Id(18)]
    public Candle? LastCandle { get; set; }
}