# app/src/trading/market_analysis/unknown.py

from __future__ import annotations

from src.trading.market_analysis.models import (
    EmaDistanceState,
    EntryTimingState,
    MarketAnalysisResult,
    MarketPhase,
    MarketState,
    MarketStructure,
    MomentumState,
    TrendDirection,
    TrendQuality,
    TrendStrength,
    VolatilityState,
)


def build_unknown_market_analysis_result(
    *,
    symbol: str,
    interval: str,
    reason: str,
    candles_count: int = 0,
    htf_interval: str = "1h",
) -> MarketAnalysisResult:
    # UNKNOWN-result используется, когда полноценный анализ невозможен:
    # нет свечей, ошибка API, мало данных или не рассчитались индикаторы.
    #
    # Важно: payload должен содержать те же ключи, что и обычный market payload,
    # чтобы formatter/snapshot/runtime не падали на отсутствующих полях.
    payload = {
        "symbol": symbol,
        "interval": interval,
        "market_state": MarketState.UNKNOWN.value,
        "trend": TrendDirection.UNKNOWN.value,
        "volatility": VolatilityState.UNKNOWN.value,
        "market_trend_strength": TrendStrength.UNKNOWN.value,
        "market_trend_quality": TrendQuality.UNKNOWN.value,
        "market_phase": MarketPhase.UNKNOWN.value,
        "market_phase_direction": TrendDirection.UNKNOWN.value,
        "market_phase_change_percent": None,
        "market_phase_direction_consistency": None,
        "market_phase_reason": reason,

        # Текущая свеча / текущий интервал.
        "current_interval_change_percent": None,
        "current_interval_direction": TrendDirection.UNKNOWN.value,
        "current_interval_label": interval,

        # Последняя закрытая свеча.
        "last_closed_candle_change_percent": None,
        "last_closed_candle_direction": TrendDirection.UNKNOWN.value,

        "market_structure": MarketStructure.UNKNOWN.value,
        "market_structure_reason": reason,

        "momentum_state": MomentumState.UNKNOWN.value,
        "momentum_direction": TrendDirection.UNKNOWN.value,
        "momentum_change_percent": None,
        "momentum_strength": None,
        "breakout_level": None,
        "breakout_distance_percent": None,
        "breakout_reason": reason,

        "market_trend_gap_percent": None,
        "ema_fast_slope_percent": None,
        "ema_slow_slope_percent": None,
        "market_trend_consistency": None,
        "market_trend_efficiency": None,
        "trend_quality_score": None,
        "ema_distance_atr_ratio": None,
        "ema_distance_state": EmaDistanceState.UNKNOWN.value,
        "entry_timing_state": EntryTimingState.UNKNOWN.value,
        "entry_timing_reason": reason,

        "close_price": None,
        "ema_fast": None,
        "ema_slow": None,
        "atr": None,
        "atr_percent": None,
        "rsi": None,

        "market_score": None,
        "market_score_label": None,
        "market_long_score": None,
        "market_short_score": None,

        "htf_interval": htf_interval,
        "htf_atr_percent": None,
        "htf_atr_percent_baseline": None,
        "htf_volatility_ratio": None,
        "htf_volatility": None,
        "htf_market_state": MarketState.UNKNOWN.value,
        "htf_trend": TrendDirection.UNKNOWN.value,
        "htf_trend_strength": TrendStrength.UNKNOWN.value,
        "htf_trend_quality": TrendQuality.UNKNOWN.value,
        "htf_market_phase": MarketPhase.UNKNOWN.value,
        "htf_alignment": "UNKNOWN",
        "htf_confirmation_score": None,
        "htf_reason": reason,

        "candles_count": candles_count,
        "is_trade_allowed": False,
        "reason": reason,
    }

    return MarketAnalysisResult(
        symbol=symbol,
        interval=interval,
        state=MarketState.UNKNOWN,
        trend=TrendDirection.UNKNOWN,
        volatility=VolatilityState.UNKNOWN,
        close_price=None,
        ema_fast=None,
        ema_slow=None,
        atr=None,
        atr_percent=None,
        rsi=None,
        candles_count=candles_count,
        reason=reason,
        is_trade_allowed=False,
        payload=payload,

        trend_strength=TrendStrength.UNKNOWN,
        trend_quality=TrendQuality.UNKNOWN,
        market_phase=MarketPhase.UNKNOWN,

        trend_gap_percent=None,
        trend_consistency=None,
        trend_efficiency=None,
        ema_distance_atr_ratio=None,

        phase_direction=TrendDirection.UNKNOWN,
        phase_change_percent=None,
        phase_reason=reason,

        market_structure=MarketStructure.UNKNOWN,
        market_structure_reason=reason,

        ema_fast_slope_percent=None,
        ema_slow_slope_percent=None,
        phase_direction_consistency=None,

        current_interval_change_percent=None,
        current_interval_direction=TrendDirection.UNKNOWN,
        current_interval_label=interval,

        momentum_state=MomentumState.UNKNOWN,
        momentum_direction=TrendDirection.UNKNOWN,
        momentum_change_percent=None,
        momentum_strength=None,
        breakout_level=None,
        breakout_distance_percent=None,
        breakout_reason=reason,

        htf_interval=htf_interval,
        htf_atr_percent=None,
        htf_atr_percent_baseline=None,
        htf_volatility_ratio=None,
        htf_volatility=None,

        trend_quality_score=None,
        ema_distance_state=EmaDistanceState.UNKNOWN,
        entry_timing_state=EntryTimingState.UNKNOWN,
        entry_timing_reason=reason,

        htf_market_state=MarketState.UNKNOWN,
        htf_trend=TrendDirection.UNKNOWN,
        htf_trend_strength=TrendStrength.UNKNOWN,
        htf_trend_quality=TrendQuality.UNKNOWN,
        htf_market_phase=MarketPhase.UNKNOWN,
        htf_alignment="UNKNOWN",
        htf_confirmation_score=None,
        htf_reason=reason,

        market_score=None,
        market_score_label=None,
        market_long_score=None,
        market_short_score=None,
    )