"""Optional helper for authoring richer GraphRouter model descriptions.

This script uses the Ember model runtime to prompt each configured model for a
concise self-description and writes the responses to a cache file. It is
intended for occasional manual refreshes; consuming code should still rely on
the deterministic JSON updated by ``update_llm_descriptions.py``.
"""

from __future__ import annotations

import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict

from ember.models.runtime.registry import ModelRegistry

BASE_DIR = Path(__file__).resolve().parents[1]
CONFIG_PATH = BASE_DIR / "configs" / "LLM_Descriptions.json"
CACHE_PATH = BASE_DIR / "cache" / "llm_description_runs.jsonl"
CACHE_PATH.parent.mkdir(exist_ok=True)


PROMPT_TEMPLATE = """
You are helping document the model "{display_name}" ({model_id}).
Write a single paragraph (<=70 words) covering:
- The model's positioning (e.g., flagship, fast, lightweight)
- Typical strengths or tasks
- Any notable context or modality limits
Use concise, factual language.
""".strip()


def generate_descriptions() -> Dict[str, str]:
    """Generate prose descriptions for each configured model via Ember."""

    config = json.loads(CONFIG_PATH.read_text())
    registry = ModelRegistry()
    results: Dict[str, str] = {}

    for display_name, entry in config.items():
        model_id = entry["model"]
        prompt = PROMPT_TEMPLATE.format(display_name=display_name, model_id=model_id)
        provider = registry.get_model(model_id)
        response = provider.complete(prompt, model=model_id)
        text = response.data.strip()
        results[display_name] = text

    timestamp = datetime.now(timezone.utc).isoformat()
    with CACHE_PATH.open("a", encoding="utf-8") as f:
        f.write(
            json.dumps(
                {
                    "timestamp": timestamp,
                    "descriptions": results,
                }
            )
            + "\n"
        )

    return results


if __name__ == "__main__":
    generate_descriptions()
