#!/usr/bin/env bash
# Garante utilizadores Ligbox na org `ligbox` (Owners) — dashboard Forgejo preenchido.
set -euo pipefail

FORGEJO_URL="${FORGEJO_URL:-http://10.10.10.130:3000}"
USER="${FORGEJO_USER:-roger}"
PASS="${FORGEJO_TOKEN:-805353}"
ORG="${FORGEJO_ORG:-ligbox}"
TEAM_ID="${FORGEJO_OWNERS_TEAM_ID:-1}"
MEMBERS=(ligboxadmin mini roger)

auth() { echo -n "${USER}:${PASS}" | base64 -w0 2>/dev/null || echo -n "${USER}:${PASS}" | base64; }

for m in "${MEMBERS[@]}"; do
  code=$(curl -sk -o /dev/null -w "%{http_code}" -X PUT \
    -H "Authorization: Basic $(auth)" \
    "${FORGEJO_URL}/api/v1/teams/${TEAM_ID}/members/${m}")
  echo "  ${m} → team Owners HTTP ${code}"
done

echo "Membros org ${ORG}:"
curl -sk -H "Authorization: Basic $(auth)" \
  "${FORGEJO_URL}/api/v1/orgs/${ORG}/members" | python3 -c "import sys,json; print(', '.join(u['login'] for u in json.load(sys.stdin)))"
