#!/usr/bin/env bash
#
# champollion.dev/queue — community sweep-queue viewer.
#
#   curl -fsSL champollion.dev/queue | bash
#
# What it does (and nothing more):
#   1. Fetches champollion.dev/queue.json (the public sweep queue).
#   2. Pretty-prints the top open items with their exact `mt-eval run`
#      commands, ready to copy-paste.
#
# It NEVER executes a benchmark, never spends your tokens, never writes
# files. Display only. Run the printed commands yourself when ready.
#
# Options (pass after `bash -s --`):
#   --top N        show N items (default 10)
#   --json         dump raw queue.json to stdout
#   QUEUE_URL=...  override the queue location (env var)

set -u

QUEUE_URL="${QUEUE_URL:-https://champollion.dev/queue.json}"
TOP=10
RAW=0

while [ "$#" -gt 0 ]; do
  case "$1" in
    --top) TOP="${2:-10}"; shift 2 ;;
    --json) RAW=1; shift ;;
    -h|--help)
      sed -n '2,19p' "$0" 2>/dev/null || true
      echo "Usage: queue [--top N] [--json]"
      exit 0
      ;;
    *) echo "Unknown option: $1 (try --help)" >&2; exit 2 ;;
  esac
done

if ! command -v curl >/dev/null 2>&1; then
  echo "error: curl not found" >&2
  exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
  echo "error: python3 not found (needed to read the queue JSON)" >&2
  exit 1
fi

QUEUE_JSON="$(curl -fsSL "$QUEUE_URL")" || {
  echo "error: could not fetch $QUEUE_URL" >&2
  exit 1
}

if [ "$RAW" -eq 1 ]; then
  printf '%s\n' "$QUEUE_JSON"
  exit 0
fi

printf '%s' "$QUEUE_JSON" | TOP="$TOP" python3 -c '
import json, os, sys

q = json.load(sys.stdin)
meta, items = q["metadata"], q["items"]
top = int(os.environ.get("TOP", "10"))

print()
print("MT Eval Arena — community sweep queue")
print("=" * 60)
print(f"  open items:   {meta['"'"'open_items'"'"']}")
print(f"  generated at: {meta['"'"'generated_at'"'"']}")
print(f"  coverage:     {meta['"'"'coverage_source'"'"']}")
print()
print(f"Top {min(top, len(items))} items (uncovered pairs first, low-resource first):")
print()
for it in items[:top]:
    c = it.get("est_cost_usd")
    est = "cost unknown" if c is None else ("<$0.01" if c < 0.01 else f"~${c:.2f}")
    print(f"#{it['"'"'priority'"'"']}  {it['"'"'language_pair'"'"']} ({it['"'"'target_language'"'"']})  "
          f"{it['"'"'model'"'"']}  [{it['"'"'condition'"'"']}]  {est}  "
          f"({it['"'"'entry_count'"'"']} entries)")
    print(f"    {it['"'"'run_command'"'"']}")
    print()
print("-" * 60)
print("Setup (once):  curl -fsSL champollion.dev/harness | bash")
print("               export OPENROUTER_API_KEY=sk-or-...")
print("Publish:       mt-eval publish <report.json>")
print()
print("No claim-locking — pick any open item. " + meta["dedupe_note"].split(". ", 1)[1])
print("Full queue + guides: https://champollion.dev/contribute")
'
