#!/bin/sh
# Regenerates src/model_catalog.inc, pic's built-in snapshot of Pi's model
# catalog (https://pi.dev/api/models), for the providers and APIs pic has:
# one row per chat model with its Pi model object (id, name, api, baseUrl,
# reasoning, input, contextWindow, maxTokens, compat, thinkingLevelMap). At run time pic lays
# fresher per-provider data from the same service over it
# (~/.pic/models-store.json), as Pi does. Needs curl and sqlite3.
# Usage: sh tools/model-catalog.sh [CATALOG_JSON]   (fetches when not given)
set -eu
cd "$(dirname "$0")/.."
tmp=$(mktemp -d "${TMPDIR:-/tmp}/pic-catalog.XXXXXX")
trap 'rm -rf -- "$tmp"' EXIT HUP INT TERM
if [ $# -ge 1 ]; then
    cp "$1" "$tmp/all.json"
else
    curl -fsS -m 60 -A 'pic-catalog' -o "$tmp/all.json" 'https://pi.dev/api/models?types=chat'
fi
now=$(($(date +%s) * 1000))
out=src/model_catalog.inc
{
    printf '/* Generated by tools/model-catalog.sh from Pi'"'"'s model catalog (https://pi.dev/api/models)\n'
    printf ' * on %s. Do not edit: regenerate. */\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
    printf '#define PIC_CATALOG_GENERATED_MS %sLL\n' "$now"
    printf 'static const struct {\n    const char *provider, *id, *json;\n} catalog_snapshot[] = {\n'
    sqlite3 :memory: "
        WITH d(j) AS (SELECT readfile('$tmp/all.json')),
        m AS (SELECT p.key AS provider, x.value AS v FROM d, json_each(d.j) p, json_each(p.value) x
              WHERE p.key IN ('anthropic','openai','openrouter','xai','groq','cerebras','deepseek','together',
                              'fireworks','huggingface','nvidia','moonshotai','zai')
                AND json_extract(x.value,'\$.type') = 'chat'
                AND json_extract(x.value,'\$.api') IN ('anthropic-messages','openai-completions','openai-responses'))
        SELECT '    {\"' || provider || '\", \"' || replace(json_extract(v,'\$.id'), '\"', '\\\"') || '\", \"' ||
               replace(replace(CASE WHEN json_type(v,'\$.thinkingLevelMap') IS NULL THEN obj
                                    ELSE json_insert(obj, '\$.thinkingLevelMap', json(json_extract(v,'\$.thinkingLevelMap'))) END,
                       '\\', '\\\\'), '\"', '\\\"') || '\"},'
        FROM (SELECT provider, v, json_object('id', json_extract(v,'\$.id'), 'name', json_extract(v,'\$.name'),
                 'api', json_extract(v,'\$.api'), 'baseUrl', json_extract(v,'\$.baseUrl'),
                 'reasoning', json(CASE WHEN json_extract(v,'\$.reasoning') THEN 'true' ELSE 'false' END),
                 'input', json(coalesce(json_extract(v,'\$.input'), '["text"]')),
                 'contextWindow', json_extract(v,'\$.contextWindow'), 'maxTokens', json_extract(v,'\$.maxTokens'),
                 'compat', json(coalesce(json_extract(v,'\$.compat'), '{}'))) AS obj FROM m)
        ORDER BY provider, json_extract(v,'\$.id')"
    printf '};\n'
} > "$tmp/out"
n=$(grep -c '^    {"' "$tmp/out")
[ "$n" -gt 100 ] || { echo "model-catalog: only $n models; refusing to write $out" >&2; exit 1; }
mv "$tmp/out" "$out"
printf 'model-catalog: %s models written to %s\n' "$n" "$out"
