using Managing.Domain.Bots;
using Managing.Domain.Indicators;
using Managing.Domain.Synth.Models;
using static Managing.Common.Enums;

namespace Managing.Application.Abstractions.Services;

/// <summary>
/// Service interface for Synth prediction business logic and probability calculations
/// </summary>
public interface ISynthPredictionService
{
    /// <summary>
    /// Calculates the probability of price reaching a target within a specified time horizon
    /// </summary>
    /// <param name="asset">Asset symbol (e.g., "BTC", "ETH")</param>
    /// <param name="currentPrice">Current market price</param>
    /// <param name="targetPrice">Target price to reach</param>
    /// <param name="timeHorizonSeconds">Time horizon in seconds</param>
    /// <param name="isLongPosition">True for long positions (liquidation when price drops), false for short positions (liquidation when price rises)</param>
    /// <param name="config">Synth configuration for this operation</param>
    /// <returns>Probability as a decimal between 0.0 and 1.0</returns>
    Task<decimal> GetProbabilityOfTargetPriceAsync(
        string asset,
        decimal currentPrice,
        decimal targetPrice,
        int timeHorizonSeconds,
        bool isLongPosition,
        SynthConfiguration config);

    /// <summary>
    /// Gets probabilities for multiple price thresholds at once
    /// </summary>
    /// <param name="asset">Asset symbol</param>
    /// <param name="currentPrice">Current market price</param>
    /// <param name="priceThresholds">Dictionary of threshold names to prices</param>
    /// <param name="timeHorizonSeconds">Time horizon in seconds</param>
    /// <param name="isLongPosition">True for long positions, false for short positions</param>
    /// <param name="config">Synth configuration for this operation</param>
    /// <param name="isBacktest">Parameter for backtest</param>
    /// <param name="signalDate">Signal date</param>
    /// <returns>Dictionary of threshold names to probabilities</returns>
    Task<Dictionary<string, decimal>> GetMultipleThresholdProbabilitiesAsync(
        string asset,
        decimal currentPrice,
        Dictionary<string, decimal> priceThresholds,
        int timeHorizonSeconds,
        bool isLongPosition,
        SynthConfiguration config,
        bool isBacktest,
        DateTime signalDate);

    /// <summary>
    /// Clears cached predictions (useful for testing or forced refresh)
    /// </summary>
    void ClearCache();

    /// <summary>
    /// Clears cached predictions from MongoDB asynchronously
    /// </summary>
    Task ClearCacheAsync();

    /// <summary>
    /// Validates a trading signal using Synth predictions to check for adverse price movements
    /// </summary>
    /// <param name="signal">The trading signal containing ticker, direction, candle data, and other context</param>
    /// <param name="currentPrice">Current market price (required)</param>
    /// <param name="botConfig">Bot configuration with Synth settings</param>
    /// <param name="isBacktest">Whether this is a backtest</param>
    /// <param name="customThresholds">Custom probability thresholds for decision-making. If null, uses default thresholds.</param>
    /// <returns>Comprehensive signal validation result including confidence, probabilities, and risk analysis</returns>
    Task<SignalValidationResult> ValidateSignalAsync(LightSignal signal, decimal currentPrice,
        TradingBotConfig botConfig, bool isBacktest, Dictionary<string, decimal> customThresholds = null);

    /// <summary>
    /// Performs risk assessment before opening a position
    /// </summary>
    /// <param name="ticker">Trading ticker</param>
    /// <param name="direction">Position direction</param>
    /// <param name="currentPrice">Current market price</param>
    /// <param name="botConfig">Bot configuration with Synth settings</param>
    /// <param name="isBacktest">Whether this is a backtest</param>
    /// <returns>True if position should be allowed, false if blocked</returns>
    Task<bool> AssessPositionRiskAsync(Ticker ticker, TradeDirection direction, decimal currentPrice,
        TradingBotConfig botConfig, bool isBacktest);

    /// <summary>
    /// Monitors liquidation risk for an open position
    /// </summary>
    /// <param name="ticker">Trading ticker</param>
    /// <param name="direction">Position direction</param>
    /// <param name="currentPrice">Current market price</param>
    /// <param name="liquidationPrice">Position liquidation price</param>
    /// <param name="positionIdentifier">Position identifier for logging</param>
    /// <param name="botConfig">Bot configuration with Synth settings</param>
    /// <returns>Risk assessment result</returns>
    Task<SynthRiskResult> MonitorPositionRiskAsync(Ticker ticker, TradeDirection direction, decimal currentPrice,
        decimal liquidationPrice, Guid positionIdentifier, TradingBotConfig botConfig);

    /// <summary>
    /// Estimates liquidation price based on money management settings
    /// </summary>
    /// <param name="currentPrice">Current market price</param>
    /// <param name="direction">Position direction</param>
    /// <param name="moneyManagement">Money management settings</param>
    /// <returns>Estimated liquidation price</returns>
    decimal EstimateLiquidationPrice(decimal currentPrice, TradeDirection direction,
        LightMoneyManagement moneyManagement);
}