using System.Text;

namespace Managing.Domain.Backtests;

public class BacktestScoringResult
{
    public double Score { get; set; }
    public List<ScoringCheck> Checks { get; set; } = new();
    public string SummaryMessage { get; set; } = string.Empty;

    public BacktestScoringResult(double score)
    {
        Score = score;
    }

    public void AddCheck(string component, double score, double weight, string message, bool passed = true)
    {
        Checks.Add(new ScoringCheck
        {
            Component = component,
            Score = score,
            Weight = weight,
            Message = message,
            Passed = passed
        });
    }

    public void AddEarlyExitCheck(string reason, string message)
    {
        Checks.Add(new ScoringCheck
        {
            Component = "Early Exit",
            Score = 0,
            Weight = 0,
            Message = message,
            Passed = false,
            IsEarlyExit = true
        });
    }

    public void AddPenaltyCheck(string component, double penaltyMultiplier, string message)
    {
        Checks.Add(new ScoringCheck
        {
            Component = component,
            Score = 0,
            Weight = 0,
            Message = message,
            Passed = penaltyMultiplier >= 1.0,
            IsPenalty = true,
            PenaltyMultiplier = penaltyMultiplier
        });
    }

    public string GenerateSummaryMessage()
    {
        var passedChecks = Checks.Where(c => c.Passed && !c.IsEarlyExit && !c.IsPenalty).ToList();
        var failedChecks = Checks.Where(c => !c.Passed && !c.IsEarlyExit && !c.IsPenalty).ToList();
        var penalties = Checks.Where(c => c.IsPenalty).ToList();
        var earlyExits = Checks.Where(c => c.IsEarlyExit).ToList();

        var summary = new StringBuilder();
        summary.AppendLine($"Final Score: {Score:F1}/100");

        if (earlyExits.Any())
        {
            summary.AppendLine($"🚫 Early Exit Reasons ({earlyExits.Count}):");
            foreach (var earlyExit in earlyExits)
            {
                summary.AppendLine($"  • {earlyExit.Component}: {earlyExit.Message}");
            }
        }

        if (passedChecks.Any())
        {
            summary.AppendLine($"✅ Passed Checks ({passedChecks.Count}):");
            foreach (var check in passedChecks.OrderByDescending(c => c.Score * c.Weight))
            {
                summary.AppendLine($"  • {check.Component}: {check.Score:F1} points ({check.Message})");
            }
        }

        if (failedChecks.Any())
        {
            summary.AppendLine($"❌ Failed Checks ({failedChecks.Count}):");
            foreach (var check in failedChecks)
            {
                summary.AppendLine($"  • {check.Component}: {check.Message}");
            }
        }

        if (penalties.Any())
        {
            summary.AppendLine($"⚠️ Applied Penalties ({penalties.Count}):");
            foreach (var penalty in penalties)
            {
                summary.AppendLine($"  • {penalty.Component}: {penalty.Message}");
            }
        }

        return summary.ToString().TrimEnd();
    }
}

public class ScoringCheck
{
    public string Component { get; set; } = string.Empty;
    public double Score { get; set; }
    public double Weight { get; set; }
    public string Message { get; set; } = string.Empty;
    public bool Passed { get; set; }
    public bool IsEarlyExit { get; set; }
    public bool IsPenalty { get; set; }
    public double PenaltyMultiplier { get; set; }
} 