#!/bin/bash
export LC_ALL=C
DB=/var/lib/docker/volumes/n8n_n8n_data/_data/database.sqlite
cp "$DB" /tmp/n8n_copy.db 2>/dev/null && echo "db copied: $(stat -c %s /tmp/n8n_copy.db) bytes"

python3 - <<'PY'
import json, sqlite3, re
db = sqlite3.connect('/tmp/n8n_copy.db')
db.row_factory = sqlite3.Row
try:
    rows = db.execute("select id,name,active from workflow_entity").fetchall()
except Exception as e:
    print("schema err", e); raise SystemExit
print(f"\nworkflows: {len(rows)}")
for r in rows:
    print(f"  - {r['name']}  (active={r['active']})")
PY

echo
echo "===== NODE TYPES / URLS / MODELS ====="
python3 - <<'PY'
import json, sqlite3
db = sqlite3.connect('/tmp/n8n_copy.db')
db.row_factory = sqlite3.Row
for r in db.execute("select name,nodes from workflow_entity"):
    nodes = json.loads(r['nodes']) if r['nodes'] else []
    print(f"\n### {r['name']}  ({len(nodes)} nodes)")
    for n in nodes:
        t = n.get('type','').split('.')[-1]
        p = n.get('parameters',{}) or {}
        blob = json.dumps(p, ensure_ascii=False)
        hint = []
        for pat in (r'https?://[^\s"\\]+', r'"model"\s*:\s*"[^"]+"', r'"modelId"\s*:\s*"[^"]+"'):
            hint += re.findall(pat, blob) if (re := __import__('re')) else []
        keep = [h for h in hint if any(k in h.lower() for k in ('ollama','11434','9router','20128','qdrant','6333','mimo','deepseek','opencode','gpt','gemini'))]
        if keep or t in ('agent','lmChatOpenAi','lmChatOllama','toolVectorStore','vectorStoreQdrant','httpRequest'):
            print(f"  - {t:24s} {n.get('name','')[:34]:34s} {sorted(set(keep))[:4]}")
PY
