# app/src/trading/execution/resets.py

from __future__ import annotations

from typing import Protocol

from src.trading.auto.state import AutoTradeState


class _ExecutionResetsProtocol(Protocol):
    """
    Protocol для reset mixin.

    Сейчас пустой, но оставлен для единообразия архитектуры.
    """

    pass


class ExecutionResetsMixin(_ExecutionResetsProtocol):
    """
    Общие reset-функции execution слоя.

    Здесь находятся методы очистки runtime/protection/
    lifecycle состояния позиции.
    """

    def _reset_runtime_protection_state(
        self,
        state: AutoTradeState,
    ) -> None:
        """
        Полный reset runtime protection состояния позиции.
        Вызывается после закрытия позиции или перед flip.
        """

        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

    def _reset_position_lifecycle_state(
        self,
        state: AutoTradeState,
    ) -> None:
        """
        Reset lifecycle/runtime состояния закрытой позиции.
        Используется после полного закрытия позиции или перед flip.
        """

        state.position_opened_monotonic_at = 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

        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_stall_state = None
        state.position_stall_reason = 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.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

        state.execution_block_reason = None
        state.last_flip_block_reason = None