"""
AkDevz AI Market Intelligence — Strategy Engine
Evaluates the 12 codified intraday strategies against multi-timeframe indicator states.
"""
from typing import Dict, List, Any, Optional

class StrategyEngine:
    @staticmethod
    def evaluate_all(
        symbol: str,
        ltp: float,
        vwap: float,
        ema_9: float,
        ema_20: float,
        ema_50: float,
        rsi_14: float,
        atr_14: float,
        supertrend: float,
        supertrend_dir: int,
        pivots: Dict[str, float],
        regime: str,
        orb_high: float = 0.0,
        orb_low: float = 0.0,
        volume_ratio: float = 1.0,
    ) -> List[Dict[str, Any]]:
        candidates = []

        # 1. Opening Range Breakout (ORB)
        if orb_high > 0 and orb_low > 0:
            if ltp > orb_high and vwap > 0 and ltp > vwap and volume_ratio >= 1.2:
                sl = max(orb_low, ltp - (1.5 * atr_14))
                t1 = ltp + 1.0 * (orb_high - orb_low)
                t2 = ltp + 1.618 * (orb_high - orb_low)
                candidates.append({
                    "strategy_code": "ORB_15M",
                    "strategy_name": "Opening Range Breakout (ORB)",
                    "action": "BUY_CE",
                    "trigger_price": orb_high,
                    "stop_loss": round(sl, 2),
                    "target_1": round(t1, 2),
                    "target_2": round(t2, 2),
                    "confluence_score": 8.0,
                    "reason": f"Confirmed close above 15m ORB High ({orb_high:.2f}) with VWAP and volume expansion.",
                })
            elif ltp < orb_low and vwap > 0 and ltp < vwap and volume_ratio >= 1.2:
                sl = min(orb_high, ltp + (1.5 * atr_14))
                t1 = ltp - 1.0 * (orb_high - orb_low)
                t2 = ltp - 1.618 * (orb_high - orb_low)
                candidates.append({
                    "strategy_code": "ORB_15M",
                    "strategy_name": "Opening Range Breakout (ORB)",
                    "action": "BUY_PE",
                    "trigger_price": orb_low,
                    "stop_loss": round(sl, 2),
                    "target_1": round(t1, 2),
                    "target_2": round(t2, 2),
                    "confluence_score": 8.0,
                    "reason": f"Confirmed close below 15m ORB Low ({orb_low:.2f}) with VWAP breakdown.",
                })

        # 2. VWAP Pullback Strategy
        if vwap > 0 and atr_14 > 0:
            dist_to_vwap = abs(ltp - vwap) / vwap * 100.0
            if dist_to_vwap <= 0.20:
                if ema_9 > ema_20 and ltp > vwap and 45 <= rsi_14 <= 60:
                    sl = vwap - (1.0 * atr_14)
                    risk = ltp - sl
                    t1 = ltp + (1.5 * risk)
                    t2 = ltp + (2.5 * risk)
                    candidates.append({
                        "strategy_code": "VWAP_PULLBACK",
                        "strategy_name": "VWAP Pullback & Rejection",
                        "action": "BUY_CE",
                        "trigger_price": round(ltp, 2),
                        "stop_loss": round(sl, 2),
                        "target_1": round(t1, 2),
                        "target_2": round(t2, 2),
                        "confluence_score": 7.5,
                        "reason": f"Pullback to rising VWAP ({vwap:.2f}) with RSI reset at {rsi_14:.1f}.",
                    })
                elif ema_9 < ema_20 and ltp < vwap and 40 <= rsi_14 <= 55:
                    sl = vwap + (1.0 * atr_14)
                    risk = sl - ltp
                    t1 = ltp - (1.5 * risk)
                    t2 = ltp - (2.5 * risk)
                    candidates.append({
                        "strategy_code": "VWAP_PULLBACK",
                        "strategy_name": "VWAP Pullback & Rejection",
                        "action": "BUY_PE",
                        "trigger_price": round(ltp, 2),
                        "stop_loss": round(sl, 2),
                        "target_1": round(t1, 2),
                        "target_2": round(t2, 2),
                        "confluence_score": 7.5,
                        "reason": f"Retracement into descending VWAP ({vwap:.2f}) with RSI resistance.",
                    })

        # 3. EMA Stack Pullback (9 / 20 EMA)
        if ema_9 > 0 and ema_20 > 0 and atr_14 > 0:
            if ema_9 > ema_20 and (ema_20 <= ltp <= ema_9):
                sl = ema_20 - (1.0 * atr_14)
                risk = ltp - sl
                t1 = ltp + (1.8 * risk)
                t2 = ltp + (2.8 * risk)
                candidates.append({
                    "strategy_code": "EMA_PULLBACK",
                    "strategy_name": "EMA Value Zone Retracement",
                    "action": "BUY_CE",
                    "trigger_price": round(ltp, 2),
                    "stop_loss": round(sl, 2),
                    "target_1": round(t1, 2),
                    "target_2": round(t2, 2),
                    "confluence_score": 7.0,
                    "reason": "Price tested value zone between 9 EMA and 20 EMA in bullish trend.",
                })
            elif ema_9 < ema_20 and (ema_9 <= ltp <= ema_20):
                sl = ema_20 + (1.0 * atr_14)
                risk = sl - ltp
                t1 = ltp - (1.8 * risk)
                t2 = ltp - (2.8 * risk)
                candidates.append({
                    "strategy_code": "EMA_PULLBACK",
                    "strategy_name": "EMA Value Zone Retracement",
                    "action": "BUY_PE",
                    "trigger_price": round(ltp, 2),
                    "stop_loss": round(sl, 2),
                    "target_1": round(t1, 2),
                    "target_2": round(t2, 2),
                    "confluence_score": 7.0,
                    "reason": "Price retraced into value zone between 9 EMA and 20 EMA in bearish trend.",
                })

        return candidates
