"""
Helfer rund um llama-swap und dessen config.yaml.

- _swap_get: liest llama-swap-Endpoints (/running, /v1/models, ...)
- read_config / write_config: lesen/schreiben der config.yaml ueber ruamel.yaml,
  sodass Kommentare und Quotes erhalten bleiben.
"""

import httpx

from config import CONFIG_PATH, LLAMA_SWAP_URL, yaml


def _swap_get(path: str):
    with httpx.Client(timeout=5.0) as c:
        r = c.get(f"{LLAMA_SWAP_URL}{path}")
        r.raise_for_status()
        return r.json()


def read_config() -> dict:
    if not CONFIG_PATH.exists():
        return {"models": {}}
    with CONFIG_PATH.open("r", encoding="utf-8") as f:
        data = yaml.load(f) or {}
    if "models" not in data or data["models"] is None:
        data["models"] = {}
    return data


def write_config(cfg: dict) -> None:
    CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
    with CONFIG_PATH.open("w", encoding="utf-8") as f:
        yaml.dump(cfg, f)
