"""
=========================================
CV6 AI Trading OS
Decision Engine
=========================================
"""


class DecisionEngine:

    def decide(
        self,
        ema: float,
        rsi: float,
        macd: float,
        supertrend: float,
        atr: float,
        vwap: float,
        strategy: str,
        confidence: float
    ):

        buy_score = 0
        sell_score = 0

        # EMA
        if ema > vwap:
            buy_score += 1
        else:
            sell_score += 1

        # RSI
        if rsi < 30:
            buy_score += 1
        elif rsi > 70:
            sell_score += 1

        # MACD
        if macd > 0:
            buy_score += 1
        else:
            sell_score += 1

        # SuperTrend
        if supertrend > 0:
            buy_score += 1
        else:
            sell_score += 1

        # Strategy
        if strategy.upper() == "BUY":
            buy_score += 2

        elif strategy.upper() == "SELL":
            sell_score += 2

        # Final Decision
        if buy_score > sell_score:
            decision = "BUY"

        elif sell_score > buy_score:
            decision = "SELL"

        else:
            decision = "HOLD"

        score = max(buy_score, sell_score)

        return {

            "decision": decision,

            "confidence": confidence,

            "score": score,

            "reason": f"BUY={buy_score} SELL={sell_score}",

            "ema": ema,

            "rsi": rsi,

            "macd": macd,

            "supertrend": supertrend,

            "atr": atr,

            "vwap": vwap
        }