#!/bin/bash
# GABGOD CLI — corpus->NPC-line forge. Run from anywhere; lives in ~/Documents/GABGOD.
cd "$(dirname "$0")"
PSQL=/opt/homebrew/opt/postgresql@16/bin/psql
case "$1" in
  abliterated)  # admin toggle: gabgod abliterated on|off|status
    case "$2" in
      on)  /opt/homebrew/bin/python3 - <<'EOF'
import json; p="config.json"; c=json.load(open(p)); c["writer"]["abliterated"]=True
json.dump(c, open(p,"w"), indent=2); print("abliterated: ON  ->", c["writer"]["abliterix"]["model"])
EOF
        ;;
      off) /opt/homebrew/bin/python3 - <<'EOF'
import json; p="config.json"; c=json.load(open(p)); c["writer"]["abliterated"]=False
json.dump(c, open(p,"w"), indent=2); print("abliterated: OFF ->", c["writer"]["stock"]["model"])
EOF
        ;;
      *)   /opt/homebrew/bin/python3 -c "
import json; c=json.load(open('config.json'))['writer']
m = c['abliterix'] if c['abliterated'] else c['stock']
print('abliterated:', 'ON' if c['abliterated'] else 'OFF', '->', m['model'], '@', m['url'])" ;;
    esac ;;
  distill)  shift; ./distill.py "$@" ;;
  status)   cat ~/.jobs/gabgod-distill.status 2>/dev/null || echo "no job run yet" ;;
  sample)   # gabgod sample [persona] — random unreviewed lines
    W=""; [ -n "$2" ] && W="where persona='$2'"
    $PSQL -d discogs_full -c "select persona, situation, line from gabgod.line_bank $W order by random() limit 15" ;;
  counts)   $PSQL -d discogs_full -c "select persona, count(*) lines, count(*) filter (where approved) approved from gabgod.line_bank group by persona order by 1" ;;
  export)   shift; ./export_sft.py "$@" ;;
  *) echo "usage: gabgod {abliterated on|off|status} | distill [...] | status | sample [persona] | counts | export [--all]" ;;
esac
