"""
Live Market Data Fetcher
读取 XAUUSD 实时价格 — 委托给 data_layer，保留此模块作为兼容入口。

Usage:
    from live_price import get_xauusd_price
    price = get_xauusd_price()  # returns {"bid": float, "ask": float, "spread": float}
"""

import json
from typing import Any, Dict

from src.data_layer import live_xauusd_price, get_live_bid_ask


def get_xauusd_price() -> Dict[str, Any]:
    """Fetch live XAUUSD bid/ask（委托给 data_layer）。"""
    return live_xauusd_price()


def get_live_price(symbol: str = "XAUUSD") -> Dict[str, Any]:
    """Unified price getter for multiple symbols."""
    if symbol.upper() in ("XAUUSD", "GOLD"):
        return get_xauusd_price()
    return {
        "source": "swissquote", "symbol": symbol,
        "bid": 0, "ask": 0, "spread": 0,
        "error": f"Unknown symbol: {symbol}",
        "timestamp": "", "success": False,
    }


# ─── CLI ────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
    import sys
    sym = sys.argv[1] if len(sys.argv) > 1 else "XAUUSD"
    result = get_live_price(sym)
    print(json.dumps(result, indent=2, ensure_ascii=False))
    sys.exit(0 if result["success"] else 1)
