using Managing.Domain.Synth.Models;

namespace Managing.Application.Synth;

/// <summary>
/// Helper class for creating and configuring Synth API integration
/// </summary>
public static class SynthConfigurationHelper
{
    /// <summary>
    /// Creates a default Synth configuration for live trading
    /// </summary>
    /// <returns>A configured SynthConfiguration instance</returns>
    public static SynthConfiguration CreateLiveTradingConfig()
    {
        return new SynthConfiguration
        {
            IsEnabled = true,
            TopMinersCount = 10,
            TimeIncrement = 300, // 5 minutes
            DefaultTimeLength = 86400, // 24 hours
            MaxLiquidationProbability = 0.10m, // 10% max risk
            PredictionCacheDurationMinutes = 5,
            UseForPositionSizing = true,
            UseForSignalFiltering = true,
            UseForDynamicStopLoss = true
        };
    }

    /// <summary>
    /// Creates a conservative Synth configuration with lower risk tolerances
    /// </summary>
    /// <returns>A conservative SynthConfiguration instance</returns>
    public static SynthConfiguration CreateConservativeConfig()
    {
        return new SynthConfiguration
        {
            IsEnabled = true,
            TopMinersCount = 10,
            TimeIncrement = 300, // 5 minutes
            DefaultTimeLength = 86400, // 24 hours
            MaxLiquidationProbability = 0.05m, // 5% max risk (more conservative)
            PredictionCacheDurationMinutes = 3, // More frequent updates
            UseForPositionSizing = true,
            UseForSignalFiltering = true,
            UseForDynamicStopLoss = true
        };
    }

    /// <summary>
    /// Creates an aggressive Synth configuration with higher risk tolerances
    /// </summary>
    /// <returns>An aggressive SynthConfiguration instance</returns>
    public static SynthConfiguration CreateAggressiveConfig()
    {
        return new SynthConfiguration
        {
            IsEnabled = true,
            TopMinersCount = 15, // More miners for broader consensus
            TimeIncrement = 300, // 5 minutes
            DefaultTimeLength = 86400, // 24 hours
            MaxLiquidationProbability = 0.15m, // 15% max risk (more aggressive)
            PredictionCacheDurationMinutes = 7, // Less frequent updates to reduce API calls
            UseForPositionSizing = true,
            UseForSignalFiltering = false, // Don't filter signals in aggressive mode
            UseForDynamicStopLoss = true
        };
    }

    /// <summary>
    /// Creates a disabled Synth configuration (bot will operate without Synth predictions)
    /// </summary>
    /// <returns>A disabled SynthConfiguration instance</returns>
    public static SynthConfiguration CreateDisabledConfig()
    {
        return new SynthConfiguration
        {
            IsEnabled = false,
            TopMinersCount = 10,
            TimeIncrement = 300,
            DefaultTimeLength = 86400,
            MaxLiquidationProbability = 0.10m,
            PredictionCacheDurationMinutes = 5,
            UseForPositionSizing = false,
            UseForSignalFiltering = false,
            UseForDynamicStopLoss = false
        };
    }

    /// <summary>
    /// Creates a Synth configuration optimized for backtesting (disabled)
    /// </summary>
    /// <returns>A backtesting-optimized SynthConfiguration instance</returns>
    public static SynthConfiguration CreateBacktestConfig()
    {
        // Synth predictions are not available for historical data, so always disabled for backtests
        return CreateDisabledConfig();
    }

    /// <summary>
    /// Validates and provides suggestions for improving a Synth configuration
    /// </summary>
    /// <param name="config">The configuration to validate</param>
    /// <returns>List of validation messages and suggestions</returns>
    public static List<string> ValidateConfiguration(SynthConfiguration config)
    {
        var messages = new List<string>();

        if (config == null)
        {
            messages.Add("❌ Configuration is null");
            return messages;
        }

        if (!config.IsEnabled)
        {
            messages.Add("ℹ️ Synth API is disabled - bot will operate without predictions");
            return messages;
        }

        if (config.TopMinersCount <= 0)
        {
            messages.Add("❌ TopMinersCount must be greater than 0");
        }
        else if (config.TopMinersCount > 20)
        {
            messages.Add("⚠️ TopMinersCount > 20 may result in slower performance and higher API usage");
        }

        if (config.TimeIncrement <= 0)
        {
            messages.Add("❌ TimeIncrement must be greater than 0");
        }

        if (config.DefaultTimeLength <= 0)
        {
            messages.Add("❌ DefaultTimeLength must be greater than 0");
        }

        if (config.MaxLiquidationProbability < 0 || config.MaxLiquidationProbability > 1)
        {
            messages.Add("❌ MaxLiquidationProbability must be between 0 and 1");
        }
        else if (config.MaxLiquidationProbability < 0.02m)
        {
            messages.Add("⚠️ MaxLiquidationProbability < 2% is very conservative and may block many trades");
        }
        else if (config.MaxLiquidationProbability > 0.20m)
        {
            messages.Add("⚠️ MaxLiquidationProbability > 20% is very aggressive and may increase risk");
        }

        if (config.PredictionCacheDurationMinutes <= 0)
        {
            messages.Add("❌ PredictionCacheDurationMinutes must be greater than 0");
        }
        else if (config.PredictionCacheDurationMinutes < 1)
        {
            messages.Add("⚠️ Cache duration < 1 minute may result in excessive API calls");
        }

        if (messages.Count == 0)
        {
            messages.Add("✅ Configuration appears valid");
        }

        return messages;
    }
} 