"""
AkDevz AI Market Intelligence — Multi-Timeframe Alignment Matrix Engine
Synthesizes trend and momentum across Daily, 60m, 15m, 5m, and 1m intervals.
"""
from typing import Dict, Any, List
import numpy as np

class MultiTimeframeEngine:
    @staticmethod
    def evaluate_timeframe(
        ltp: float,
        vwap: float,
        ema_20: float,
        ema_50: float,
        rsi: float,
        supertrend_dir: int
    ) -> Dict[str, Any]:
        """Evaluates trend and momentum state for an individual timeframe."""
        score = 0
        reasons = []

        if ltp > vwap:
            score += 1
            reasons.append("Price > VWAP")
        else:
            score -= 1
            reasons.append("Price < VWAP")

        if ema_20 > ema_50:
            score += 1
            reasons.append("EMA 20 > EMA 50")
        else:
            score -= 1
            reasons.append("EMA 20 < EMA 50")

        if supertrend_dir == 1:
            score += 1
            reasons.append("Supertrend Bullish (+1)")
        elif supertrend_dir == -1:
            score -= 1
            reasons.append("Supertrend Bearish (-1)")

        if rsi > 55.0:
            score += 1
        elif rsi < 45.0:
            score -= 1

        bias = "NEUTRAL"
        if score >= 3:
            bias = "STRONG_BULLISH"
        elif score in [1, 2]:
            bias = "MILD_BULLISH"
        elif score <= -3:
            bias = "STRONG_BEARISH"
        elif score in [-1, -2]:
            bias = "MILD_BEARISH"

        return {
            "bias": bias,
            "alignment_score": score,
            "reasons": reasons,
            "rsi": round(rsi, 1)
        }

    @staticmethod
    def build_alignment_matrix(
        htf_bias: str,      # Daily / 60m
        itf_bias: str,      # 15m
        ltf_bias: str       # 5m
    ) -> Dict[str, Any]:
        """
        Creates the Confluence & Conflict Matrix across Higher, Intermediate, and Lower timeframes.
        """
        htf_bull = "BULLISH" in htf_bias
        htf_bear = "BEARISH" in htf_bias

        itf_bull = "BULLISH" in itf_bias
        itf_bear = "BEARISH" in itf_bias

        ltf_bull = "BULLISH" in ltf_bias
        ltf_bear = "BEARISH" in ltf_bias

        is_all_bull = htf_bull and itf_bull and ltf_bull
        is_all_bear = htf_bear and itf_bear and ltf_bear

        congruence = "CONFLICTED"
        recommendation = "WAIT"

        if is_all_bull:
            congruence = "FULL_BULLISH_CONFLUENCE"
            recommendation = "FAVOR_BUY_CE"
        elif is_all_bear:
            congruence = "FULL_BEARISH_CONFLUENCE"
            recommendation = "FAVOR_BUY_PE"
        elif htf_bull and not ltf_bull:
            congruence = "HTF_BULLISH_LTF_PULLBACK"
            recommendation = "WAIT_FOR_LTF_CONFIRMATION"
        elif htf_bear and not ltf_bear:
            congruence = "HTF_BEARISH_LTF_BOUNCE"
            recommendation = "WAIT_FOR_LTF_CONFIRMATION"

        return {
            "htf_bias": htf_bias,
            "itf_bias": itf_bias,
            "ltf_bias": ltf_bias,
            "matrix_state": congruence,
            "confluence_recommendation": recommendation,
            "is_aligned": is_all_bull or is_all_bear
        }
