"""ETF/futures ticker universe for the macro book.

Maps each traded/monitored instrument to:
  - the canonical liquid ETF proxy (use this in journal/weekly/event notes)
  - the underlying futures ticker (for context only — journal uses ETF)
  - bucket (rates, FX, commodity, equity-beta)
  - yfinance ticker for the proxy (may differ from display ticker)
  - inverse: True for inverse/bear ETFs (UNF for short EAFE etc.) — none used here
  - currency: display currency for the price

When filling in a journal, weekly note, or event review, use the `display`
ticker as the symbol. Data fetcher uses the `yf` ticker.
"""

from __future__ import annotations

# Bucket codes used in [[risk-framework]] §3
RATES_US = "rates-us"
RATES_DE = "rates-de"
RATES_UK = "rates-uk"
RATES_JP = "rates-jp"
FX = "fx"
COMMODITY = "commodity"
EQUITY = "equity-beta"

UNIVERSE: list[dict] = [
    # --- Traded: US rates ---
    {"id": "2y-ust", "display": "SHY", "yf": "SHY",
     "name": "2y UST", "bucket": RATES_US, "currency": "USD", "kind": "rates"},
    {"id": "5y-ust", "display": "IEI", "yf": "IEI",
     "name": "5y UST", "bucket": RATES_US, "currency": "USD", "kind": "rates"},
    {"id": "10y-ust", "display": "IEF", "yf": "IEF",
     "name": "10y UST", "bucket": RATES_US, "currency": "USD", "kind": "rates"},
    {"id": "30y-ust", "display": "TLT", "yf": "TLT",
     "name": "30y UST", "bucket": RATES_US, "currency": "USD", "kind": "rates"},
    {"id": "sofr", "display": "SHY", "yf": "SHY",
     "name": "SOFR 3M (front)", "bucket": RATES_US, "currency": "USD", "kind": "rates",
     "note": "SOFR futures tracked via SHY + curve via IEF/TLT"},
    # --- Traded: EU rates ---
    {"id": "schatz", "display": "IEGA", "yf": "IEGA.L",
     "name": "Schatz (DE 2y)", "bucket": RATES_DE, "currency": "EUR", "kind": "rates"},
    {"id": "bund", "display": "IEGA", "yf": "IEGA.L",
     "name": "Bund (DE 10y)", "bucket": RATES_DE, "currency": "EUR", "kind": "rates",
     "note": "IEGA is the broad EUR govt bond ETF; Bund vs Schatz split not captured at ETF level"},
    {"id": "oat", "display": "IEGA", "yf": "IEGA.L",
     "name": "OAT (FR 10y)", "bucket": RATES_DE, "currency": "EUR", "kind": "rates",
     "note": "IEGA is the broad EUR govt bond ETF proxy for OAT"},
    # --- Traded: UK rates ---
    {"id": "gilt", "display": "IBGL", "yf": "IBGL.L",
     "name": "Gilt (UK 10y)", "bucket": RATES_UK, "currency": "GBP", "kind": "rates"},
    # --- Traded: Japan rates ---
    {"id": "jgb", "display": "FXY", "yf": "FXY",
     "name": "JGB (JP 10y)", "bucket": RATES_JP, "currency": "JPY", "kind": "rates",
     "note": "No clean JGB ETF; tracked via FXY (JPY move ≈ inverse to JGB yield move)"},
    # --- Traded: FX ---
    {"id": "eurusd", "display": "FXE", "yf": "FXE",
     "name": "EUR/USD", "bucket": FX, "currency": "USD", "kind": "fx"},
    {"id": "usdjpy", "display": "FXY", "yf": "FXY",
     "name": "USD/JPY", "bucket": FX, "currency": "USD", "kind": "fx"},
    {"id": "gbpusd", "display": "FXB", "yf": "FXB",
     "name": "GBP/USD", "bucket": FX, "currency": "USD", "kind": "fx"},
    {"id": "audusd", "display": "FXA", "yf": "FXA",
     "name": "AUD/USD", "bucket": FX, "currency": "USD", "kind": "fx"},
    {"id": "usdcad", "display": "FXC", "yf": "FXC",
     "name": "USD/CAD", "bucket": FX, "currency": "USD", "kind": "fx"},
    {"id": "usdchf", "display": "FXF", "yf": "FXF",
     "name": "USD/CHF", "bucket": FX, "currency": "USD", "kind": "fx"},
    # --- Traded: commodities ---
    {"id": "gc", "display": "GLD", "yf": "GLD",
     "name": "Gold", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "si", "display": "SLV", "yf": "SLV",
     "name": "Silver", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "hg", "display": "CPER", "yf": "CPER",
     "name": "Copper", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "cl", "display": "USO", "yf": "USO",
     "name": "WTI Crude", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "bz", "display": "BNO", "yf": "BNO",
     "name": "Brent Crude", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "ng", "display": "UNG", "yf": "UNG",
     "name": "Natural Gas", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "zc", "display": "CORN", "yf": "CORN",
     "name": "Corn", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "zw", "display": "WEAT", "yf": "WEAT",
     "name": "Wheat", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    {"id": "zs", "display": "SOYB", "yf": "SOYB",
     "name": "Soy", "bucket": COMMODITY, "currency": "USD", "kind": "commodity"},
    # --- Traded: equity index ---
    {"id": "es", "display": "SPY", "yf": "SPY",
     "name": "S&P 500 (Micro E-mini)", "bucket": EQUITY, "currency": "USD", "kind": "equity"},
    {"id": "nq", "display": "QQQ", "yf": "QQQ",
     "name": "Nasdaq 100 (Micro E-mini)", "bucket": EQUITY, "currency": "USD", "kind": "equity"},
    {"id": "rty", "display": "IWM", "yf": "IWM",
     "name": "Russell 2000 (Micro E-mini)", "bucket": EQUITY, "currency": "USD", "kind": "equity"},
    # --- Monitored ---
    {"id": "dxy", "display": "UUP", "yf": "UUP",
     "name": "US Dollar Index (DXY)", "bucket": FX, "currency": "USD", "kind": "monitored"},
    {"id": "vix", "display": "^VIX", "yf": "^VIX",
     "name": "VIX", "bucket": EQUITY, "currency": "USD", "kind": "monitored"},
    {"id": "tip", "display": "TIP", "yf": "TIP",
     "name": "10y TIPS (real yields)", "bucket": RATES_US, "currency": "USD", "kind": "monitored"},
    {"id": "hyg", "display": "HYG", "yf": "HYG",
     "name": "HY OAS (HYG as proxy)", "bucket": EQUITY, "currency": "USD", "kind": "monitored",
     "note": "HYG price is inverse of OAS; HYG up = OAS tight"},
    {"id": "btc", "display": "BTC", "yf": "BTC-USD",
     "name": "Bitcoin", "bucket": FX, "currency": "USD", "kind": "monitored",
     "note": "Use BTC-USD direct from yfinance"},
]

# Groupings used in summary tables
BUCKET_LABELS = {
    RATES_US: "US rates",
    RATES_DE: "EU rates",
    RATES_UK: "UK rates",
    RATES_JP: "JP rates",
    FX: "FX",
    COMMODITY: "Commodity",
    EQUITY: "Equity-beta",
}

# A compact headline set for the daily journal "overnight moves" table.
# One ticker per bucket keeps the journal readable.
HEADLINE_TICKERS = ["SPY", "QQQ", "IWM", "IEF", "TLT", "GLD", "USO",
                    "FXE", "FXY", "FXB", "^VIX", "BTC-USD"]


def by_id(instrument_id: str) -> dict | None:
    for u in UNIVERSE:
        if u["id"] == instrument_id:
            return u
    return None


def by_yf(ticker: str) -> dict | None:
    for u in UNIVERSE:
        if u["yf"] == ticker or u["display"] == ticker:
            return u
    return None


def yf_tickers() -> list[str]:
    return [u["yf"] for u in UNIVERSE]


def headline_yf_tickers() -> list[str]:
    return list(HEADLINE_TICKERS)
