using Predictalytics.Domain.Enums;

namespace Predictalytics.Application.DTOs;

public record TraderDto(
    int Id,
    string Platform,
    string PlatformUserId,
    string DisplayName,
    string Tier,
    string Strategy,
    decimal CombinedScore,
    decimal CopytradingScore,
    decimal CopytradingQualityScore,
    decimal CopytradingCopyabilityScore,
    decimal WinRate,
    decimal TotalPnl,
    int TotalTrades,
    int Trades30d,
    decimal PnL30d,
    decimal WinRate30d,
    decimal EstimatedBankroll,
    bool IsOnWatchlist,
    bool IsSuspectedBot,
    DateTime? LastPolledAt,
    List<string>? Traits
);

public record TraderDetailDto(
    int Id,
    string Platform,
    string PlatformUserId,
    string DisplayName,
    string? Notes,
    string Tier,
    string Strategy,
    bool IsSuspectedBot,
    int? ManualPriorityOverride,
    decimal WinRate,
    decimal TotalPnl,
    int TotalTrades,
    int Trades30d,
    decimal PnL30d,
    decimal WinRate30d,
    decimal PnL7d,
    decimal WinRate7d,
    decimal PnL24h,
    decimal WinRate24h,
    decimal EstimatedBankroll,
    decimal CurrentBalance,
    decimal ActivityScore,
    decimal QualityScore,
    decimal VolumeScore,
    decimal TimingScore,
    decimal CombinedScore,
    decimal CopytradingScore,
    decimal CopytradingQualityScore,
    decimal CopytradingCopyabilityScore,
    
    // Market Return Metrics
    decimal MedianWinReturnPct,
    decimal AvgWinReturnPct,
    decimal MedianLossReturnPct,
    decimal AvgLossReturnPct,
    decimal? ProfitFactor,

    // H1 Risk-adjusted return (equity-curve smoothness = copyability)
    decimal MaxDrawdownUsd,
    decimal PnlVolatilityUsd,
    int LongestLosingStreakDays,
    decimal? ReturnOverMaxDrawdown,

    // H2 Strategy fingerprint
    decimal CategoryConcentration,
    decimal? ConvictionEdgePct,

    int Rank,
    bool IsOnWatchlist,
    DateTime CreatedAt,
    DateTime? LastPolledAt,
    string? AiStrategySummary,
    IReadOnlyList<TradeDto> RecentTrades,
    IReadOnlyList<TraderCategoryPerformanceDto> CategoryPerformances,
    IReadOnlyList<TraderTraitDto> Traits
);

public record TraderTraitDto(
    string Trait,
    decimal Value
);

public record TraderCategoryPerformanceDto(
    string Category,
    decimal TotalVolume,
    decimal TotalPnL,
    int TotalTrades,
    int WinningTrades,
    decimal WinRate,
    decimal AvgReturnPct
);

public record TraderPositionDto(
    string MarketId,
    string? MarketName,
    string? Category,
    string? OutcomeToken,
    decimal SharesHeld,
    decimal AvgCost,
    decimal RealizedPnl,
    decimal UnrealizedPnl,
    decimal CurrentPrice,
    DateTime? LastTradeExecutedAt
);

public record TraderWindowMetricsDto(
    DateTime WindowStart,
    DateTime WindowEnd,
    int ClosedMarkets,
    decimal WinRate,
    decimal AvgReturnPct,
    decimal MedianWinReturnPct,
    decimal MedianLossReturnPct,
    decimal? ProfitFactor
);

public record TraderProfileDto(
    int Id,
    string DisplayName,
    MasterStatus MasterStatus,
    decimal MedianHoldDurationHours,
    decimal P50PositionSize,
    decimal P90PositionSize,
    decimal TradesPerWeek,
    decimal MedianMarketVolumeUsd,
    decimal MedianPostFillDriftPct,
    decimal NetEdgeAfterFeesPct,
    string? PriceBandProfileJson,
    IReadOnlyList<TraderWindowMetricsDto> WindowMetrics
);

public record TraderCorrelationDto(
    int TraderAId,
    int TraderBId,
    int CommonMarketsCount,
    int SameDirectionMarketsCount,
    decimal IntersectionRatioA,
    decimal IntersectionRatioB,
    decimal AgreementRatio
);

/// <summary>One master in the suggested diversified copy portfolio (#4).</summary>
public record PortfolioPickDto(
    int TraderId,
    string DisplayName,
    string Platform,
    decimal Score,
    decimal CopytradingCopyabilityScore,
    decimal TotalPnl,
    string Category,
    string Reason);

/// <summary>The suggested diversified copy portfolio plus how the funnel narrowed (#4).</summary>
public record CopyPortfolioDto(
    IReadOnlyList<PortfolioPickDto> Picks,
    int CandidatesConsidered,
    int DroppedForCorrelation,
    int DroppedForCategoryCap);

/// <summary>One wallet in the co-movement graph (#1 increment 2).</summary>
public record CoMovementNodeDto(
    int TraderId,
    string DisplayName,
    string Platform,
    decimal Score,
    int Cluster,
    double InformedShare,
    bool IsInsider);

/// <summary>One undirected co-movement edge (shared timely co-entries).</summary>
public record CoMovementEdgeDto(int Source, int Target, int Weight);

/// <summary>The smart-money co-movement graph: clustered nodes + edges (#1 increment 2).</summary>
public record CoMovementGraphDto(
    IReadOnlyList<CoMovementNodeDto> Nodes,
    IReadOnlyList<CoMovementEdgeDto> Edges,
    int ClusterCount);

/// <summary>A wallet that co-moves with a seed trader (smart-money discovery, #1).</summary>
public record CoMovingWalletDto(
    int TraderId,
    string DisplayName,
    string Platform,
    int SharedMarkets,
    double AvgLeadHours,
    bool MovesBeforeSeed);

/// <summary>One row of the dedicated Insider view (system-level, not a user watchlist).</summary>
public record InsiderDto(
    int Id,
    string Platform,
    string DisplayName,
    string PlatformUserId,
    decimal CombinedScore,
    decimal CopytradingCopyabilityScore,
    decimal WinRate,
    decimal TotalPnl,
    int TotalTrades,
    decimal SurpriseValue,
    DateTime? FirstDetectedAt,
    DateTime? LastPolledAt
);
