from __future__ import annotations

from typing import Any, Dict

from app.config import settings


_state: Dict[str, Any] = {
    "models": {
        "default_model": settings.OPENROUTER_MODEL,
        "temperature": 0.3,
        "max_tokens": 800,
    },
    "exchanges": {
        "binance": {"enabled": True},
        "alpha_vantage": {
            "enabled": True,
            "has_api_key": bool(settings.ALPHA_VANTAGE_API_KEY),
        },
    },
}


def get_models() -> Dict[str, Any]:
    return dict(_state["models"])  # shallow copy


def update_models(patch: Dict[str, Any]) -> Dict[str, Any]:
    allowed = {"default_model", "temperature", "max_tokens"}
    for k, v in patch.items():
        if k in allowed:
            _state["models"][k] = v
    return get_models()


def get_exchanges() -> Dict[str, Any]:
    return dict(_state["exchanges"])  # shallow copy


def update_exchanges(patch: Dict[str, Any]) -> Dict[str, Any]:
    # Shallow merge per top-level key
    for k, v in patch.items():
        if k in _state["exchanges"] and isinstance(v, dict):
            _state["exchanges"][k].update(v)
    return get_exchanges()