"""
=========================================
CV6 AI Trading OS
Strategy Engine
=========================================
"""


class StrategyEngine:

    def generate_strategy(
        self,
        ema: float,
        rsi: float,
        macd: float,
        supertrend: float,
        atr: float,
        vwap: float,
    ):

        buy_score = 0
        sell_score = 0

        # EMA
        if ema > 0:
            buy_score += 1
        else:
            sell_score += 1

        # RSI
        if rsi > 60:
            buy_score += 1
        elif rsi < 40:
            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

        # VWAP
        if vwap > 0:
            buy_score += 1
        else:
            sell_score += 1

        # ATR — volatility filter only (don't use as directional signal)
        # High ATR = high volatility = reduce confidence slightly
        # (no score change — ATR is not directional)

        total = buy_score + sell_score

        confidence = 0.0

        if total > 0:
            confidence = round(
                (max(buy_score, sell_score) / total) * 100,
                2,
            )

        if buy_score > sell_score:
            strategy = "BUY"

        elif sell_score > buy_score:
            strategy = "SELL"

        else:
            strategy = "HOLD"

        return {
            "strategy": strategy,
            "buy_score": buy_score,
            "sell_score": sell_score,
            "confidence": confidence,
            "ema": ema,
            "rsi": rsi,
            "macd": macd,
            "supertrend": supertrend,
            "atr": atr,
            "vwap": vwap,
        }