#!/usr/bin/env bash
# Quo² agent CLI
# Docs: https://quosquared.io/cli
# API:  https://quosquared.io/api/agent
set -euo pipefail

HOST="${QUO_HOST:-https://quosquared.io}"
HOST="${HOST%/}"

usage() {
  cat <<EOF
quo — pay Quo² and open an account

Default host: $HOST
Set QUO_HOST to point somewhere else.

Commands
  quo offer
      Read the offer and the USDC destination.

  quo subscribe --label NAME [--units N] [--reinvest] [--from 0x…]
      Mint an invoice. Amount is 1, 10, or 100 USDC on Base.

  quo invoice REF
      Poll that invoice. Wait until status is paid.

  quo pay REF --tx 0x…
      POST the Base transaction hash after you send USDC.

  quo card
      Print the agent discovery card.

  quo help
      This text.

Flow
  1. quo offer
  2. quo subscribe --label my-bot --units 1
  3. Send exactly amount USDC on Base to destination
  4. quo pay Q2-XXXXXX --tx 0x…
  5. quo invoice Q2-XXXXXX until paid, then open the account link

The unit does not open on the POST. It opens when the desk marks money in.
Daily credits are not guaranteed.
EOF
}

json_tool() {
  if command -v python3 >/dev/null 2>&1; then
    python3 -m json.tool
  else
    cat
  fi
}

json_escape() {
  if command -v python3 >/dev/null 2>&1; then
    python3 -c 'import json,sys; print(json.dumps(sys.argv[1]))' "$1"
  else
    printf '"%s"' "${1//"/\"}"
  fi
}

http() {
  local method="$1"
  local path="$2"
  local data="${3-}"
  local url="$HOST$path"
  local tmp code
  tmp="$(mktemp)"
  if [ -n "$data" ]; then
    code="$(curl -sS -o "$tmp" -w "%{http_code}" -X "$method" "$url" \
      -H "content-type: application/json" \
      -H "accept: application/json" \
      -d "$data")" || true
  else
    code="$(curl -sS -o "$tmp" -w "%{http_code}" -X "$method" "$url" \
      -H "accept: application/json")" || true
  fi
  json_tool < "$tmp" || cat "$tmp"
  echo
  rm -f "$tmp"
  case "$code" in
    2*) return 0 ;;
    *) echo "HTTP $code from $url" >&2; return 1 ;;
  esac
}

cmd_offer() {
  http GET /api/agent
}

cmd_card() {
  http GET /.well-known/agent.json
}

cmd_invoice() {
  local ref="${1-}"
  if [ -z "$ref" ]; then
    echo "usage: quo invoice REF" >&2
    return 2
  fi
  http GET "/api/agent/invoice/$ref"
}

cmd_pay() {
  local ref="" tx=""
  while [ $# -gt 0 ]; do
    case "$1" in
      --tx) tx="${2-}"; shift 2 ;;
      --help|-h) echo "usage: quo pay REF --tx 0x…"; return 0 ;;
      --) shift; break ;;
      -*) echo "unknown flag $1" >&2; return 2 ;;
      *)
        if [ -z "$ref" ]; then ref="$1"; shift
        else echo "unexpected argument $1" >&2; return 2
        fi
        ;;
    esac
  done
  if [ -z "$ref" ] || [ -z "$tx" ]; then
    echo "usage: quo pay REF --tx 0x…" >&2
    return 2
  fi
  http POST "/api/agent/invoice/$ref" "{\"tx\":$(json_escape "$tx")}"
}

cmd_subscribe() {
  local label="" units="1" reinvest="false" from=""
  while [ $# -gt 0 ]; do
    case "$1" in
      --label|-l) label="${2-}"; shift 2 ;;
      --units|-n) units="${2-}"; shift 2 ;;
      --reinvest) reinvest="true"; shift ;;
      --from) from="${2-}"; shift 2 ;;
      --help|-h) echo "usage: quo subscribe --label NAME [--units N] [--reinvest] [--from 0x…]"; return 0 ;;
      --) shift; break ;;
      -*) echo "unknown flag $1" >&2; return 2 ;;
      *) echo "unexpected argument $1" >&2; return 2 ;;
    esac
  done
  if [ -z "$label" ]; then
    echo "usage: quo subscribe --label NAME [--units N] [--reinvest] [--from 0x…]" >&2
    return 2
  fi
  local body
  body="{\"label\":$(json_escape "$label"),\"units\":${units},\"reinvest\":${reinvest}"
  if [ -n "$from" ]; then
    body="$body,\"from\":$(json_escape "$from")"
  fi
  body="$body}"
  http POST /api/agent "$body"
}

cmd="${1-help}"
if [ $# -gt 0 ]; then shift; fi
case "$cmd" in
  offer|get) cmd_offer ;;
  subscribe|sub) cmd_subscribe "$@" ;;
  invoice|inv) cmd_invoice "$@" ;;
  pay|tx) cmd_pay "$@" ;;
  card) cmd_card ;;
  help|-h|--help) usage ;;
  *)
    echo "unknown command: $cmd" >&2
    usage >&2
    exit 2
    ;;
esac
