"""Read two public model catalogs before changing a client's model ID.

    python3 compare-model-catalogs.py
    python3 compare-model-catalogs.py --openrouter-id anthropic/claude-sonnet-4.6 --kunavo-id claude-sonnet-4-6

Exit status 0 means both IDs were listed at the time of the check.
"""
import argparse
import json
import sys
from urllib.error import URLError
from urllib.request import Request, urlopen


def catalog_ids(url, key):
    request = Request(url, headers={"Accept": "application/json", "User-Agent": "kunavo-catalog-example/1"})
    with urlopen(request, timeout=20) as response:
        catalog = json.load(response)
    return {model["id"] for model in catalog[key]}


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--openrouter-id", default="anthropic/claude-sonnet-4.6")
    parser.add_argument("--kunavo-id", default="claude-sonnet-4-6")
    args = parser.parse_args()
    endpoints = [
        ("OpenRouter", "https://openrouter.ai/api/v1", "https://openrouter.ai/api/v1/models", "data", args.openrouter_id),
        ("Kunavo", "https://api.kunavo.com/v1", "https://kunavo.com/pricing.json", "models", args.kunavo_id),
    ]
    matched = True
    for service, base, url, rows, model in endpoints:
        try:
            found = model in catalog_ids(url, rows)
        except (URLError, TimeoutError, KeyError, ValueError) as error:
            print(f"{service}: unable to read the catalog: {error}", file=sys.stderr)
            return 2
        print(f"{service}: {model} | {'listed' if found else 'missing'} | base URL {base}")
        matched = matched and found
    return 0 if matched else 1


if __name__ == "__main__":
    sys.exit(main())
