using Managing.Common;

namespace Managing.Api.Models.Responses
{
    /// <summary>
    /// Summary of agent performance and activity
    /// </summary>
    public class AgentSummaryViewModel
    {
        /// <summary>
        /// AgentName of the agent
        /// </summary>
        public string AgentName { get; set; }

        /// <summary>
        /// Total profit and loss in USD (gross, before fees)
        /// </summary>
        public decimal TotalPnL { get; set; }

        /// <summary>
        /// Net profit and loss in USD (after fees)
        /// </summary>
        public decimal NetPnL { get; set; }

        /// <summary>
        /// Total return on investment as a percentage
        /// </summary>
        public decimal TotalROI { get; set; }

        /// <summary>
        /// Number of winning trades
        /// </summary>
        public int Wins { get; set; }

        /// <summary>
        /// Number of losing trades
        /// </summary>
        public int Losses { get; set; }


        /// <summary>
        /// Number of active strategies for this agent
        /// </summary>
        public int ActiveStrategiesCount { get; set; }

        /// <summary>
        /// Total volume traded by this agent in USD
        /// </summary>
        public decimal TotalVolume { get; set; }

        /// <summary>
        /// Total balance including USDC and open position values (without leverage, including PnL)
        /// </summary>
        public decimal TotalBalance { get; set; }

        /// <summary>
        /// Total fees paid by this agent across all positions
        /// </summary>
        public decimal TotalFees { get; set; }

        /// <summary>
        /// Total number of backtests run by this agent
        /// </summary>
        public int BacktestCount { get; set; }
    }

    /// <summary>
    /// Platform-wide statistics without individual agent details
    /// </summary>
    public class PlatformSummaryViewModel
    {
        /// <summary>
        /// When the data was last updated
        /// </summary>
        public required DateTime LastUpdated { get; set; }

        /// <summary>
        /// When the last 24-hour snapshot was taken
        /// </summary>
        public required DateTime LastSnapshot { get; set; }

        /// <summary>
        /// Whether there are pending changes to be saved
        /// </summary>
        public required bool HasPendingChanges { get; set; }

        // Current metrics
        /// <summary>
        /// Total number of agents on the platform
        /// </summary>
        public required int TotalAgents { get; set; }

        /// <summary>
        /// Total number of active strategies across all agents
        /// </summary>
        public required int TotalActiveStrategies { get; set; }

        /// <summary>
        /// Total platform-wide profit and loss in USD
        /// </summary>
        public required decimal TotalPlatformPnL { get; set; }

        /// <summary>
        /// Total volume traded across all agents in USD
        /// </summary>
        public required decimal TotalPlatformVolume { get; set; }

        /// <summary>
        /// Total open interest across all positions in USD
        /// </summary>
        public required decimal OpenInterest { get; set; }

        /// <summary>
        /// Total number of open positions across all strategies
        /// </summary>
        public required int TotalPositionCount { get; set; }

        /// <summary>
        /// Total platform-wide fees paid in USD
        /// </summary>
        public required decimal TotalPlatformFees { get; set; }

        // Historical snapshots
        /// <summary>
        /// Daily snapshots for the last 60 days
        /// </summary>
        public required List<DailySnapshot> DailySnapshots { get; set; }

        // Volume breakdown by asset
        /// <summary>
        /// Volume breakdown by asset/ticker
        /// </summary>
        public required Dictionary<Enums.Ticker, decimal> VolumeByAsset { get; set; }

        // Position count breakdown
        /// <summary>
        /// Position count breakdown by asset/ticker
        /// </summary>
        public required Dictionary<Enums.Ticker, int> PositionCountByAsset { get; set; }

        /// <summary>
        /// Position count breakdown by direction (Long/Short)
        /// </summary>
        public required Dictionary<Enums.TradeDirection, int> PositionCountByDirection { get; set; }
    }

    /// <summary>
    /// Daily snapshot of platform metrics
    /// </summary>
    public class DailySnapshot
    {
        /// <summary>
        /// Date of the snapshot
        /// </summary>
        public required DateTime Date { get; set; }

        /// <summary>
        /// Total number of agents on this date
        /// </summary>
        public required int TotalAgents { get; set; }

        /// <summary>
        /// Total number of active strategies on this date
        /// </summary>
        public required int TotalStrategies { get; set; }

        /// <summary>
        /// Total volume traded on this date in USD
        /// </summary>
        public required decimal TotalVolume { get; set; }

        /// <summary>
        /// Total PnL on this date in USD (gross, before fees)
        /// </summary>
        public required decimal TotalPnL { get; set; }

        /// <summary>
        /// Net PnL on this date in USD (after fees)
        /// </summary>
        public required decimal NetPnL { get; set; }

        /// <summary>
        /// Total open interest on this date in USD
        /// </summary>
        public required decimal TotalOpenInterest { get; set; }

        /// <summary>
        /// Total number of positions on this date
        /// </summary>
        public required int TotalPositionCount { get; set; }
    }

    /// <summary>
    /// Response model containing a list of agent summaries for the agent index
    /// </summary>
    public class AgentIndexViewModel
    {
        /// <summary>
        /// List of agent summaries sorted by performance
        /// </summary>
        public List<AgentSummaryViewModel> AgentSummaries { get; set; } = new List<AgentSummaryViewModel>();

        /// <summary>
        /// Time filter applied to the data
        /// </summary>
        public string TimeFilter { get; set; } = "Total";
    }
}