#!/usr/bin/env bash
set -euo pipefail

OPS_URL="${OPS_URL:-http://10.10.10.122:8080}"
DOMAIN="${AUDIT_DOMAIN:-diarissima.com}"
TENANT_ID="${AUDIT_TENANT_ID:-1}"

echo "=== Ligbox Ops Audit Overview (009) ==="
echo "OPS_URL=$OPS_URL DOMAIN=$DOMAIN"

echo "[1] Health"
curl -sf "$OPS_URL/health" | python3 -m json.tool | grep -E 'version|status'

echo "[2] Register + audit domain (if needed)"
curl -sf -X POST "$OPS_URL/api/v1/audit/run/${TENANT_ID}?domain=${DOMAIN}" | python3 -m json.tool | head -15

echo "[3] Audit cycle (sync all onboarding domains)"
curl -sf -X POST "$OPS_URL/api/v1/audit/cycle" | python3 -m json.tool | head -30

echo "[4] Overview"
curl -sf "$OPS_URL/api/v1/audit/overview" | python3 -m json.tool | head -45

echo "[5] Scorecard tenant=$TENANT_ID domain=$DOMAIN"
curl -sf "$OPS_URL/api/v1/audit/tenants/${TENANT_ID}/scorecard?domain=${DOMAIN}" | python3 -m json.tool | head -50

CHECKS=$(curl -sf "$OPS_URL/api/v1/audit/tenants/${TENANT_ID}/scorecard?domain=${DOMAIN}" | python3 -c "import sys,json; d=json.load(sys.stdin); print(len(d.get('checks') or []))")
echo "[6] Checks count: $CHECKS (expect 8)"
if [[ "$CHECKS" -lt 1 ]]; then
  echo "WARN: no checks yet — domain may not be in audit_domains"
  exit 1
fi

echo "=== Audit verification complete ==="
