using Managing.Application.Abstractions.Models;
using Orleans;
using static Managing.Common.Enums;

namespace Managing.Application.Abstractions.Grains;

/// <summary>
/// Grain interface for managing platform-wide summary metrics
/// </summary>
public interface IPlatformSummaryGrain : IGrainWithStringKey
{
    /// <summary>
    /// Gets the current platform summary data
    /// </summary>
    Task<PlatformSummaryViewModel> GetPlatformSummaryAsync();
    
    /// <summary>
    /// Forces a refresh of all platform data
    /// </summary>
    Task RefreshDataAsync();
    
    /// <summary>
    /// Gets the total volume traded across all strategies
    /// </summary>
    Task<decimal> GetTotalVolumeAsync();
    
    /// <summary>
    /// Gets the total PnL across all strategies
    /// </summary>
    Task<decimal> GetTotalPnLAsync();
    
    /// <summary>
    /// Gets the total open interest across all positions
    /// </summary>
    Task<decimal> GetTotalOpenInterest();
    
    /// <summary>
    /// Gets the total number of open positions
    /// </summary>
    Task<int> GetTotalPositionCountAsync();
    
    /// <summary>
    /// Gets the daily volume history for the last 30 days for chart visualization
    /// </summary>
    Task<List<VolumeHistoryPoint>> GetVolumeHistoryAsync();
    
    // Event handlers for immediate updates
    /// <summary>
    /// Updates the active strategy count
    /// </summary>
    Task UpdateActiveStrategyCountAsync(int newActiveCount);
    Task OnPositionOpenedAsync(PositionOpenedEvent evt);
    Task OnPositionClosedAsync(PositionClosedEvent evt);
    Task OnTradeExecutedAsync(TradeExecutedEvent evt);
}

/// <summary>
/// Base class for platform metrics events
/// </summary>
[GenerateSerializer]
public abstract class PlatformMetricsEvent
{
    [Id(0)]
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}



/// <summary>
/// Event fired when a new position is opened
/// </summary>
[GenerateSerializer]
public class PositionOpenedEvent : PlatformMetricsEvent
{
    [Id(1)]
    public Guid PositionId { get; set; }
    
    [Id(2)]
    public Ticker Ticker { get; set; }
    
    [Id(3)]
    public decimal Volume { get; set; }
    
    [Id(4)]
    public TradeDirection Direction { get; set; }
}

/// <summary>
/// Event fired when a position is closed
/// </summary>
[GenerateSerializer]
public class PositionClosedEvent : PlatformMetricsEvent
{
    [Id(1)]
    public Guid PositionId { get; set; }
    
    [Id(2)]
    public Ticker Ticker { get; set; }
    
    [Id(3)]
    public decimal RealizedPnL { get; set; }
    
    [Id(4)]
    public decimal Volume { get; set; }

    [Id(5)]
    public decimal InitialVolume { get; set; }
}

/// <summary>
/// Event fired when a trade is executed
/// </summary>
[GenerateSerializer]
public class TradeExecutedEvent : PlatformMetricsEvent
{
    [Id(1)]
    public Guid TradeId { get; set; }
    
    [Id(2)]
    public Guid PositionId { get; set; }
    
    [Id(3)]
    public Guid StrategyId { get; set; }
    
    [Id(4)]
    public Ticker Ticker { get; set; }
    
    [Id(5)]
    public decimal Volume { get; set; }
    
    [Id(6)]
    public decimal PnL { get; set; }
    
    [Id(7)]
    public decimal Fee { get; set; }
    
    [Id(8)]
    public TradeDirection Direction { get; set; }
}
