#!/usr/bin/env bash
# VM112 wizard — garantir carbonio.list_all_domains + smoke /api/admin/domains
# SSH: root@10.10.10.112 senha @betinplace (NÃO usar 805353)

set -euo pipefail

HOST="${1:-root@10.10.10.112}"
PASS="${VM112_SSH_PASS:-@betinplace}"
KEY="${VM112_ADMIN_API_KEY:-ibytera-corp-api-key-change-later}"
REPO="$(cd "$(dirname "$0")/../.." && pwd)"
PATCH="${REPO}/deploy/vm112-wizard/ensure-carbonio-list-domains.py"

echo "==> VM112 wizard smoke + carbonio patch"
sshpass -p "${PASS}" scp -o StrictHostKeyChecking=no "$PATCH" "${HOST}:/tmp/ensure-carbonio-list-domains.py"
sshpass -p "${PASS}" ssh -o StrictHostKeyChecking=no "${HOST}" bash -s <<EOF
set -e
python3 /tmp/ensure-carbonio-list-domains.py
systemctl restart ligbox-wizard
sleep 4
systemctl is-active ligbox-wizard
curl -sf -H "X-Api-Key: ${KEY}" http://127.0.0.1:8090/api/admin/domains | python3 -c "import sys,json; d=json.load(sys.stdin); print('domains', len(d.get('domains',[])), 'cached', d.get('cached'))"
EOF
echo "VM112 wizard OK"
