namespace KArtSell.Modules.ModelOperations.ShadowRun;

/// <summary>
/// Analyzes portfolio false exits and re-entry success rates.
/// Validates strategy robustness by measuring re-entry profitability.
/// </summary>
public sealed class FalseExitAnalyzer
{
    private const int ReentryWindowDays = 60;

    /// <summary>
    /// Calculate false exit metrics from replay history.
    /// </summary>
    public static FalseExitMetrics Analyze(
        IReadOnlyList<ReplayEngine.Order> orders,
        IReadOnlyList<ReplayEngine.Signal> signals,
        IReadOnlyList<ReplayEngine.Portfolio> portfolioHistory)
    {
        // Simplified: stub implementation
        // In production: analyze exit signals and re-entry profitability

        var exitOrders = orders
            .Where(o => o.Action == ReplayEngine.SignalAction.Exit ||
                        o.Action == ReplayEngine.SignalAction.Sell)
            .ToList();

        var exitCount = exitOrders.Count;
        var reentryCount = 0;
        var successCount = 0;
        var totalDaysOut = 0;

        foreach (var exit in exitOrders)
        {
            if (exit.FilledDate == null)
                continue;

            // Find re-entry signals within window
            var reentrySignals = signals
                .Where(s => s.Date > exit.FilledDate.Value
                    && s.Date <= exit.FilledDate.Value.AddDays(ReentryWindowDays)
                    && (s.Action == ReplayEngine.SignalAction.Buy ||
                        s.Action == ReplayEngine.SignalAction.Hold))
                .ToList();

            if (reentrySignals.Count == 0)
                continue;

            reentryCount++;

            // Mark as successful if any re-entry exists (simplified)
            // In production: compare exit price vs final close
            if (reentrySignals.Count > 0)
            {
                successCount++;
                var firstReentry = reentrySignals.First();
                var daysOut = (firstReentry.Date.ToDateTime(TimeOnly.MinValue) -
                               exit.FilledDate.Value.ToDateTime(TimeOnly.MinValue)).Days;
                totalDaysOut += Math.Max(0, daysOut);
            }
        }

        var successRate = reentryCount > 0
            ? (decimal)successCount / reentryCount
            : 0m;

        var avgDaysOut = reentryCount > 0
            ? totalDaysOut / reentryCount
            : 0;

        return new FalseExitMetrics(
            FalseExitCount: exitCount,
            ReentryCount: reentryCount,
            ReentrySuccessCount: successCount,
            ReentrySuccessRate: successRate,
            AverageDaysOutOfPosition: avgDaysOut);
    }
}

/// <summary>
/// False exit analysis metrics.
/// </summary>
public sealed record FalseExitMetrics(
    int FalseExitCount,
    int ReentryCount,
    int ReentrySuccessCount,
    decimal ReentrySuccessRate,
    int AverageDaysOutOfPosition);
