#!/usr/bin/env bash
# One-off: hard-delete the exploratory `open-human-model` RFC from
# wiggleverse/meta so the slot for RFC #1 is reserved for `human`.
# Usage:  GITEA_TOKEN=xxx bash scripts/cleanup-open-human-model.sh
set -euo pipefail

: "${GITEA_TOKEN:?Set GITEA_TOKEN to a Gitea token with write:repository on wiggleverse/meta}"

BASE="https://git.wiggleverse.org/api/v1/repos/wiggleverse/meta"
gcurl() {
  curl -sS -H "Authorization: token ${GITEA_TOKEN}" -H "Content-Type: application/json" "$@"
}

echo "==> 1. Close PR #2 (Claim ownership)"
gcurl -X PATCH "${BASE}/pulls/2" -d '{"state":"closed"}' \
  | python3 -m json.tool | grep -E '"state"|"number"' || true

echo
echo "==> 2. Delete branches"
for b in propose/open-human-model claim/open-human-model edit-open-human-model-1c5ba4; do
  code=$(gcurl -X DELETE -o /dev/null -w "%{http_code}" "${BASE}/branches/${b}")
  echo "    ${b} -> HTTP ${code}"
done

echo
echo "==> 3. Delete rfcs/open-human-model.md from main"
gcurl -X DELETE "${BASE}/contents/rfcs/open-human-model.md" -d '{
  "sha": "ba219763f9df7e3a5cbe3ae9e9f51485781abc1f",
  "branch": "main",
  "message": "Remove exploratory open-human-model RFC; reserve RFC #1 for human"
}' | python3 -m json.tool | grep -E '"sha"|"message"|"name"' || true

echo
echo "==> 4. Verify"
echo "    -- remaining rfcs/ --"
gcurl "${BASE}/contents/rfcs?ref=main" \
  | python3 -c "import sys, json; [print('   ', f['name']) for f in json.load(sys.stdin)]"
echo "    -- remaining branches --"
gcurl "${BASE}/branches?limit=50" \
  | python3 -c "import sys, json; [print('   ', b['name']) for b in json.load(sys.stdin)]"
