# app/src/trading/auto/state_reset.py

"""
Centralized runtime reset helpers for AutoTrade.

Файл содержит только чистые функции сброса AutoTradeState.
Без EventBus, JournalService, ExecutionEngine и другой бизнес-логики.
"""

from __future__ import annotations

from src.trading.auto.state import AutoTradeState


# -----------------------------------------------------------------------------
# Adaptive Position Sizing
# -----------------------------------------------------------------------------

def reset_adaptive_size_state(state: AutoTradeState) -> None:
    state.adaptive_size_base = None
    state.adaptive_size_final = None
    state.adaptive_size_multiplier = None
    state.adaptive_size_reason = None
    state.adaptive_size_factors = None
    state.effective_risk_percent = None
    state.effective_target_risk_usd = None
    state.execution_size_adjustment_reason = None
    state.adaptive_size_changed_at = None


# -----------------------------------------------------------------------------
# Signal Runtime
# -----------------------------------------------------------------------------

def reset_signal_runtime_state(state: AutoTradeState) -> None:
    state.last_signal = "HOLD"
    state.last_signal_repeat_count = 0
    state.last_signal_confidence = 0.0
    state.last_signal_reason = None

    state.signal_started_at = None
    state.signal_updated_at = None
    state.signal_confirmation_seconds = 0
    state.signal_confirmation_missing_repeats = 0
    state.signal_confirmation_progress = 0.0
    state.signal_confirmation_reason = None

    state.decision_status = "WAITING"
    state.decision_reason = None
    state.is_signal_confirmed = False
    state.is_signal_ready = False


# -----------------------------------------------------------------------------
# Execution Runtime
# -----------------------------------------------------------------------------

def reset_execution_block_state(state: AutoTradeState) -> None:
    state.execution_block_reason = None
    state.execution_block_title = None
    state.execution_block_message = None
    state.execution_block_action = None


def reset_execution_semantic_state(state: AutoTradeState) -> None:
    state.execution_semantic_status = None
    state.execution_semantic_message = None
    state.execution_semantic_reason = None


def reset_execution_quality_state(state: AutoTradeState) -> None:
    state.execution_quality = None
    state.execution_quality_reason = None
    state.execution_quality_message = None
    state.snapshot_age_seconds = None
    state.spread_percent = None


def reset_execution_pricing_state(state: AutoTradeState) -> None:
    state.execution_price_source = None
    state.execution_price_age_seconds = None
    state.execution_bid_price = None
    state.execution_ask_price = None
    state.execution_last_price = None
    state.execution_price_freshness = None


def reset_execution_confidence_state(state: AutoTradeState) -> None:
    state.execution_confidence_score = None
    state.execution_confidence_level = None
    state.execution_confidence_reason = None
    state.execution_confidence_factors = None


def reset_execution_runtime_state(state: AutoTradeState) -> None:
    reset_execution_block_state(state)
    reset_execution_semantic_state(state)
    reset_execution_quality_state(state)
    reset_execution_pricing_state(state)
    reset_execution_confidence_state(state)


# -----------------------------------------------------------------------------
# Market Runtime
# -----------------------------------------------------------------------------

def reset_market_base_state(state: AutoTradeState) -> None:
    state.market_state = None
    state.market_trend = None
    state.market_volatility = None
    state.market_trend_strength = None
    state.market_trend_quality = None
    state.market_phase = None
    state.market_phase_direction = None


def reset_market_score_state(state: AutoTradeState) -> None:
    state.market_score = None
    state.market_score_label = None
    state.market_long_score = None
    state.market_short_score = None


def reset_market_candle_state(state: AutoTradeState) -> None:
    state.last_closed_candle_change_percent = None
    state.last_closed_candle_direction = None
    state.current_interval_change_percent = None
    state.current_interval_direction = None
    state.current_interval_label = None


def reset_market_structure_state(state: AutoTradeState) -> None:
    state.market_structure = None
    state.market_structure_reason = None


def reset_market_trend_quality_state(state: AutoTradeState) -> None:
    state.market_trend_gap_percent = None
    state.market_trend_consistency = None
    state.market_trend_efficiency = None
    state.trend_quality_score = None

    state.ema_distance_atr_ratio = None
    state.ema_distance_state = None
    state.entry_timing_state = None
    state.entry_timing_reason = None

    state.ema_fast_slope_percent = None
    state.ema_slow_slope_percent = None
    state.candle_noise_score = None
    state.price_position_score = None


def reset_market_htf_state(state: AutoTradeState) -> None:
    state.htf_interval = None
    state.htf_atr_percent = None
    state.htf_atr_percent_baseline = None
    state.htf_volatility_ratio = None
    state.htf_volatility = None

    state.htf_market_state = None
    state.htf_trend = None
    state.htf_trend_strength = None
    state.htf_trend_quality = None
    state.htf_market_phase = None

    state.htf_alignment = None
    state.htf_confirmation_score = None
    state.htf_reason = None


def reset_market_momentum_state(state: AutoTradeState) -> None:
    state.momentum_state = None
    state.momentum_direction = None
    state.momentum_change_percent = None
    state.momentum_strength = None
    state.breakout_level = None
    state.breakout_distance_percent = None
    state.breakout_reason = None


def reset_market_analysis_meta_state(state: AutoTradeState) -> None:
    state.market_analysis_interval = None
    state.market_analysis_reason = None
    state.market_analysis_updated_at = None

    state.entry_block_reason = None
    state.entry_block_message = None
    state.market_runtime_degraded = False


def reset_market_analysis_state(state: AutoTradeState) -> None:
    reset_market_base_state(state)
    reset_market_score_state(state)
    reset_market_candle_state(state)
    reset_market_structure_state(state)
    reset_market_trend_quality_state(state)
    reset_market_htf_state(state)
    reset_market_momentum_state(state)
    reset_market_analysis_meta_state(state)


# -----------------------------------------------------------------------------
# Runtime Expiration
# -----------------------------------------------------------------------------

def reset_runtime_expiration_state(state: AutoTradeState) -> None:
    state.runtime_expired_reason = None
    state.runtime_expired_message = None


# -----------------------------------------------------------------------------
# Position Runtime
# -----------------------------------------------------------------------------

def reset_position_identity_state(state: AutoTradeState) -> None:
    state.position_side = "NONE"
    state.entry_price = None
    state.position_size = None
    state.position_opened_monotonic_at = None
    state.unrealized_pnl_usd = None


def reset_position_health_state(state: AutoTradeState) -> None:
    state.position_pnl_percent = None
    state.position_hold_seconds = None
    state.position_pressure = None
    state.position_health_score = None
    state.position_health_status = None
    state.position_health_reason = None
    state.position_risk_level = None
    state.position_risk_reason = None
    state.position_trend_alignment = None
    state.position_adverse_momentum = False
    state.position_exit_pressure = None


def reset_position_runtime_state(state: AutoTradeState) -> None:
    reset_position_identity_state(state)
    reset_position_health_state(state)


def reset_position_semantics_state(state: AutoTradeState) -> None:
    state.position_lifecycle_stage = None
    state.position_hold_quality = None
    state.position_decay_state = None
    state.position_exit_confidence = None
    state.position_exit_signal = None
    state.position_intelligence_reason = None
    state.position_recommended_action = None

    state.position_peak_pnl_usd = None
    state.position_peak_pnl_percent = None
    state.position_mfe_percent = None
    state.position_mae_percent = None
    state.position_fatigue_score = None
    state.position_fatigue_state = None
    state.position_giveback_percent = None
    state.position_conviction_state = None
    state.position_exit_urgency = None
    state.position_reversal_risk = None
    state.position_stall_state = None
    state.position_stall_reason = None


# -----------------------------------------------------------------------------
# Protection Runtime
# -----------------------------------------------------------------------------

def reset_position_protection_state(state: AutoTradeState) -> None:
    state.position_protection_status = None
    state.position_protection_reason = None

    state.break_even_armed = False
    state.break_even_price = None
    state.trailing_stop_active = False
    state.trailing_stop_price = None
    state.profit_lock_active = False
    state.profit_lock_price = None

    state.runtime_protection_action = None
    state.runtime_protection_reason = None
    state.runtime_protection_updated_at = None


# -----------------------------------------------------------------------------
# Autonomous Runtime
# -----------------------------------------------------------------------------

def reset_autonomous_runtime_state(state: AutoTradeState) -> None:
    state.autonomous_action = None
    state.autonomous_action_reason = None
    state.autonomous_action_confidence = None
    state.autonomous_protection_required = False
    state.autonomous_reduce_required = False
    state.autonomous_exit_required = False

    state.autonomous_last_action = None
    state.autonomous_last_action_reason = None
    state.autonomous_last_action_at = None


# -----------------------------------------------------------------------------
# Loss Cooldown / Cycle / Flip / Last Execution
# -----------------------------------------------------------------------------

def reset_loss_cooldown_state(state: AutoTradeState) -> None:
    state.last_loss_monotonic_at = None
    state.loss_cooldown_active = False
    state.loss_cooldown_reason = None


def reset_cycle_statistics_state(state: AutoTradeState) -> None:
    state.cycle_realized_pnl_usd = 0.0
    state.cycle_closed_trades = 0
    state.cycle_winning_trades = 0
    state.cycle_losing_trades = 0
    state.cycle_consecutive_losses = 0
    state.cycle_trade_fees_usd = 0.0
    state.cycle_overnight_fees_usd = 0.0


def reset_flip_runtime_state(state: AutoTradeState) -> None:
    state.last_flip_old_side = None
    state.last_flip_new_side = None
    state.last_flip_pnl_usd = None
    state.last_flip_reason = None
    state.last_flip_monotonic_at = None
    state.last_flip_at = None
    state.last_flip_block_reason = None


def reset_last_execution_state(state: AutoTradeState) -> None:
    state.last_execution_action = None
    state.last_execution_reason = None


# -----------------------------------------------------------------------------
# Full Runtime Reset
# -----------------------------------------------------------------------------

def reset_full_runtime_state(state: AutoTradeState) -> None:
    reset_cycle_statistics_state(state)
    reset_adaptive_size_state(state)
    reset_signal_runtime_state(state)
    reset_execution_runtime_state(state)
    reset_market_analysis_state(state)
    reset_runtime_expiration_state(state)
    reset_position_runtime_state(state)
    reset_position_semantics_state(state)
    reset_position_protection_state(state)
    reset_autonomous_runtime_state(state)
    reset_loss_cooldown_state(state)
    reset_flip_runtime_state(state)
    reset_last_execution_state(state)


def reset_runtime_state(state: AutoTradeState) -> None:
    reset_full_runtime_state(state)


# -----------------------------------------------------------------------------
# Lifecycle Scenario Resets
# -----------------------------------------------------------------------------

def reset_after_auto_start(state: AutoTradeState) -> None:
    reset_cycle_statistics_state(state)
    reset_loss_cooldown_state(state)
    reset_adaptive_size_state(state)
    reset_signal_runtime_state(state)
    reset_execution_runtime_state(state)
    reset_runtime_expiration_state(state)
    reset_flip_runtime_state(state)


def reset_after_auto_stop(state: AutoTradeState) -> None:
    reset_full_runtime_state(state)


def reset_after_symbol_or_strategy_change(state: AutoTradeState) -> None:
    reset_adaptive_size_state(state)
    reset_signal_runtime_state(state)
    reset_execution_runtime_state(state)
    reset_market_analysis_state(state)
    reset_runtime_expiration_state(state)


def reset_after_position_closed(state: AutoTradeState) -> None:
    reset_position_runtime_state(state)
    reset_position_semantics_state(state)
    reset_position_protection_state(state)
    reset_autonomous_runtime_state(state)
    reset_flip_runtime_state(state)


def reset_after_market_runtime_expired(state: AutoTradeState) -> None:
    reset_market_analysis_state(state)


def reset_after_signal_runtime_expired(state: AutoTradeState) -> None:
    reset_signal_runtime_state(state)
    reset_execution_runtime_state(state)