using Predictalytics.Domain.Entities;
using Predictalytics.Domain.Enums;

namespace Predictalytics.Domain.Interfaces;

/// <summary>
/// Platform-agnostic provider interface. Each prediction market platform
/// must implement this to be integrated into the system.
/// </summary>
public interface IPlatformProvider
{
    /// <summary>Which platform this provider serves.</summary>
    PlatformType Platform { get; }

    /// <summary>Human-readable name of the platform.</summary>
    string PlatformName { get; }

    /// <summary>Whether this provider is fully implemented and operational.</summary>
    bool IsImplemented { get; }

    /// <summary>Fetch recent trades for a specific trader.</summary>
    Task<IReadOnlyList<Trade>> GetTraderTradesAsync(string platformUserId, int limit = 50, CancellationToken ct = default);

    /// <summary>Fetch all historical trades for a specific trader via pagination.</summary>
    Task<IReadOnlyList<Trade>> GetTradesPagedAsync(string platformUserId, int limitPerRequest = 500, CancellationToken ct = default);

    /// <summary>Fetch current positions/holdings for a trader.</summary>
    Task<IReadOnlyList<TraderPositionInfo>> GetTraderPositionsAsync(string platformUserId, CancellationToken ct = default);

    /// <summary>Discover notable/active traders on the platform.</summary>
    Task<IReadOnlyList<DiscoveredTrader>> DiscoverTradersAsync(int limit = 20, CancellationToken ct = default);

    /// <summary>Fetch market metadata by platform-specific market ID.</summary>
    Task<Market?> GetMarketAsync(string platformMarketId, CancellationToken ct = default);

    /// <summary>Fetch a batch of events with their markets and outcomes for bulk sync.</summary>
    Task<IReadOnlyList<Event>> GetEventsAsync(int limit = 100, string? cursor = null, bool includeClosed = false, CancellationToken ct = default);

    /// <summary>Fetch top holders for a market to discover new traders.</summary>
    Task<IReadOnlyList<DiscoveredTrader>> GetTopHoldersAsync(string platformMarketId, int limit = 20, CancellationToken ct = default);

    /// <summary>Fetch recent trades that occurred on a specific market.</summary>
    Task<IReadOnlyList<Trade>> GetMarketTradesAsync(string platformMarketId, int limit = 50, CancellationToken ct = default);

    /// <summary>Fetch historical prices for an outcome token.</summary>
    Task<IReadOnlyList<MarketOutcomePriceSnapshot>> GetPriceHistoryAsync(string tokenId, CancellationToken ct = default);
}

/// <summary>
/// A trader's current position in a market.
/// </summary>
public record TraderPositionInfo(
    string PlatformUserId,
    string MarketId,
    string MarketQuestion,
    string Outcome,
    decimal Size,
    decimal AveragePrice,
    decimal CurrentValue,
    decimal PnlPercent,
    string? AssetId = null,
    decimal RealizedPnl = 0
);

/// <summary>
/// A trader discovered during auto-discovery scanning.
/// </summary>
public record DiscoveredTrader(
    string PlatformUserId,
    string DisplayName,
    decimal Volume24h,
    int TradeCount24h,
    decimal WinRate
);
