import numpy as np

from indicators import calc_trend_score


class TradingEngine:
    def __init__(self):
        pass

    # =====================================================================
    #  MAIN CYCLE
    # =====================================================================
    def process_cycle(self, state, decision):

        # ??? ???????
        has_position = state.get("has_position", False)

        # ????: ???? pnl_pct ??-state (run.py ???? ???? ??)
        pnl_pct = state.get("pnl_pct", 0.0)

        buy_ratio = state.get("buy_ratio", 0.0)
        ratio_5m = state.get("ratio_5m", 0.0)
        ratio_15m = state.get("ratio_15m", 0.0)
        cvd = state.get("cvd", 0.0)
        whale = state.get("whale", 0.0)
        depth_ratio = state.get("depth_ratio", 1.0)
        smooth = state.get("smooth", 0.0)
        price_change_5m = state.get("price_change_pct_5m", 0.0)

        f_mempool, f_fee, f_block = state.get("onchain", [0, 0, 0])

        trend_score = calc_trend_score(
            smooth,
            buy_ratio,
            cvd,
            whale,
            f_mempool,
            f_fee,
            f_block
        )

        # =====================================================================
        #  SELL LOGIC (simple & safe)
        # =====================================================================
        if has_position:

            # TP – ???? +1% (????? ?? ?? ????? ??)
            if pnl_pct >= 1.0:
                return {
                    "action": "SELL",
                    "reason": "TP_hit",
                    "pnl_pct": pnl_pct,
                    "trend_score": trend_score
                }

            # SL – ???? -1%
            if pnl_pct <= -1.0:
                return {
                    "action": "SELL",
                    "reason": "SL_hit",
                    "pnl_pct": pnl_pct,
                    "trend_score": trend_score
                }

            # ??? ????? – ???????
            return {
                "action": "WAIT",
                "reason": "holding",
                "pnl_pct": pnl_pct,
                "trend_score": trend_score
            }

        # =====================================================================
        #  BUY LOGIC v3 (????? ??Binance Global + Vision)
        # =====================================================================

        # 1. ????? ???? – ???? ?? ???????? ??? ?? ?????
        #    ?? bids ????? ???? ?? ?????? ????? ??asks ? ?? ??????
        if depth_ratio < 0.4 or depth_ratio > 12:
            return {"action": "WAIT", "reason": "depth_extreme"}

        # 2. Smooth – ?? ?????? ??? ???? ??, ???? ?? ????? ????? ???
        if smooth > 0.8:
            return {"action": "WAIT", "reason": "smooth_high"}

        # 3. ??????? – ?????? ??? ????? ????? ??5 ????
        #    0.02 ~= 0.02% (?? ???? ??? ?? ???? ?????)
        if price_change_5m < 0.02:
            return {"action": "WAIT", "reason": "momentum_low"}

        # 4. Strong indicators combined (????? ?? ???? ????? ???)
        strong_buy = (
            buy_ratio > 0.74 and      # ???? ??? 0.76
            ratio_5m > 0.72 and
            ratio_15m > 0.70 and
            cvd > 0.8 and             # ???? ??? 2.0
            whale > 0.6               # ??? ???? ????
        )

        # 5. Trend score integration – ??? ?? ????
        trend_needed = (
            trend_score > 60 or
            (trend_score > 52 and buy_ratio > 0.80)
        )

        if strong_buy and trend_needed:
            return {
                "action": "BUY",
                "reason": "v3_strong_entry",
                "trend_score": trend_score,
                "buy_ratio": buy_ratio,
                "cvd": cvd,
                "whale": whale,
                "depth_ratio": depth_ratio,
                "price_change_5m": price_change_5m
            }

        return {"action": "WAIT", "reason": "no_conditions"}


# =====================================================================
#  detect_signal FOR run.py compatibility
# =====================================================================
def detect_signal(state):
    engine = TradingEngine()
    decision = {}   # run.py does not provide decision
    return engine.process_cycle(state, decision)
