"""
InstrumentContext — per-symbol trading configuration.

Replaces hardcoded GOLD_SYMBOL/DXY_SYMBOL patterns across the codebase.
Each instrument declares its macro driver, correlation behavior, and
session/volatility characteristics.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Dict, List, Optional


class AssetCategory(str, Enum):
    FOREX_MAJOR = "forex_major"
    FOREX_MINOR = "forex_minor"
    PRECIOUS_METAL = "precious_metal"
    INDEX = "index"
    COMMODITY = "commodity"
    CRYPTO = "crypto"


class CorrelationSign(str, Enum):
    INVERSE = "inverse"     # -1: driver up → symbol down (e.g. Gold-DXY)
    DIRECT = "direct"        # +1: driver up → symbol up (e.g. US30-TNX)
    NEUTRAL = "neutral"      #  0: no fixed relationship


@dataclass
class MacroDriverConfig:
    """Describes how a macro driver affects the trading symbol."""
    driver_symbol: str                              # e.g. "DXY.f"
    correlation_sign: CorrelationSign               # expected direction
    correlation_label: str                          # "DXY USD Index"
    implication_lexicon: Dict[str, str] = field(default_factory=lambda: {
        "favorable": "tailwind",
        "unfavorable": "headwind",
        "neutral": "neutral",
    })
    proxy_symbols: List[str] = field(default_factory=list)
    filter_params: Dict[str, float] = field(default_factory=lambda: {
        "ema_smooth_period": 5,
        "roc_period": 20,
        "z_score_window": 50,
        "z_score_threshold": 0.8,
        "correlation_window": 50,
        "correlation_min_abs": 0.25,
        "direction_persistence": 3,
        "atr_period": 14,
    })
    session_weights: Dict[str, float] = field(default_factory=lambda: {
        "ASIA": 0.5,
        "LONDON": 1.0,
        "NEW_YORK": 1.0,
        "ASIA_LONDON_OVERLAP": 1.2,
        "LONDON_NY_OVERLAP": 1.2,
        "WEEKEND_CLOSED": 0.0,
    })


@dataclass
class InstrumentContext:
    """Complete per-symbol trading configuration."""
    symbol: str                                    # "XAUUSD"
    display_name: str                              # "Gold vs US Dollar"
    asset_name: str                                # "Gold"
    asset_category: AssetCategory
    macro_driver: Optional[MacroDriverConfig] = None
    lot_step: float = 0.01
    min_lot: float = 0.01
    max_lot: float = 0.20
    slippage_points: int = 10
    atr_noise_threshold: float = 1.0
    supported_timeframes: List[str] = field(default_factory=lambda: ["M5", "M15", "H1"])

    @property
    def symbol_display(self) -> str:
        return f"{self.asset_name} ({self.symbol})"

    @property
    def has_macro_driver(self) -> bool:
        return self.macro_driver is not None

    @property
    def dxy_relevant(self) -> bool:
        return (
            self.macro_driver is not None
            and self.macro_driver.driver_symbol.upper() in ("DXY.F", "USDX")
        )
