#!/usr/bin/env bash
# Smoke tests Desk produção — semáforo GREEN/RED (Spec 039/040/043)
# Uso: ./smoke-desk.sh [BASE_URL]
# Ex.: DESK_USER=root DESK_PASS=805353 ./smoke-desk.sh https://desk.ligbox.com.br

set -euo pipefail

BASE="${1:-https://desk.ligbox.com.br}"
DESK_USER="${DESK_USER:-root}"
DESK_PASS="${DESK_PASS:-805353}"
VM112_KEY="${VM112_ADMIN_API_KEY:-ibytera-corp-api-key-change-later}"
MIN_AGENTIC_LINES="${MIN_AGENTIC_LINES:-1200}"

FAIL=0
pass() { echo "  OK  $1"; }
fail() { echo "  FAIL $1"; FAIL=$((FAIL + 1)); }

echo "==> Smoke Desk — ${BASE}"
echo "==> $(date -Iseconds)"

# --- Público / assets ---
code=$(curl -sk -o /dev/null -w "%{http_code}" "${BASE}/api/health" --max-time 15)
[[ "$code" == "200" ]] && pass "GET /api/health ($code)" || fail "GET /api/health ($code)"

code=$(curl -sk -o /dev/null -w "%{http_code}" "${BASE}/assets/operational-feed.js" --max-time 10)
[[ "$code" == "200" ]] && pass "GET /assets/operational-feed.js ($code)" || fail "operational-feed.js ($code)"

code=$(curl -sk -o /dev/null -w "%{http_code}" "${BASE}/assets/user-wizard.js" --max-time 10)
[[ "$code" == "200" ]] && pass "GET /assets/user-wizard.js ($code)" || fail "user-wizard.js ($code)"

agentic_body=$(curl -sk "${BASE}/assets/agentic-ops.js" --max-time 20)
lines=$(echo "$agentic_body" | wc -l | tr -d ' ')
if [[ "$lines" -ge "$MIN_AGENTIC_LINES" ]] && echo "$agentic_body" | grep -q "Squad Command"; then
  pass "agentic-ops.js (${lines} linhas, Spec 030)"
else
  fail "agentic-ops.js (${lines} linhas, esperado >= ${MIN_AGENTIC_LINES} + Squad Command)"
fi

# --- Auth + API ---
TOKEN=$(curl -sk -X POST "${BASE}/api/v1/auth/login" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"${DESK_USER}\",\"password\":\"${DESK_PASS}\"}" \
  --max-time 15 | python3 -c "import sys,json; print(json.load(sys.stdin).get('access_token',''))" 2>/dev/null || true)

if [[ -z "$TOKEN" ]]; then
  fail "login JWT (token vazio)"
else
  pass "login JWT"
  AUTH=(-H "Authorization: Bearer ${TOKEN}")

  code=$(curl -sk -o /dev/null -w "%{http_code}" "${BASE}/api/v1/governance/modules" "${AUTH[@]}" --max-time 15)
  [[ "$code" == "200" ]] && pass "GET /api/v1/governance/modules ($code)" || fail "governance/modules ($code)"

  code=$(curl -sk -o /dev/null -w "%{http_code}" -X POST "${BASE}/api/v1/governance/users/wizard" \
    "${AUTH[@]}" -H "Content-Type: application/json" -d '{"email":"invalid"}' --max-time 15)
  [[ "$code" == "400" || "$code" == "422" ]] && pass "POST governance/users/wizard validação ($code)" || fail "governance/users/wizard ($code)"

  code=$(curl -sk -o /dev/null -w "%{http_code}" "${BASE}/api/v1/rbac/actions" "${AUTH[@]}" --max-time 20)
  [[ "$code" == "200" ]] && pass "GET /api/v1/rbac/actions ($code)" || fail "rbac/actions ($code)"

  vm112=$(curl -sk "${BASE}/api/v1/vm112/domains" "${AUTH[@]}" --max-time 30)
  n=$(echo "$vm112" | python3 -c "import sys,json; d=json.load(sys.stdin); print(len(d.get('domains',[])))" 2>/dev/null || echo 0)
  if [[ "$n" -ge 0 ]] && echo "$vm112" | grep -q '"domains"'; then
    pass "GET /api/v1/vm112/domains (${n} domínios)"
  else
    fail "vm112/domains proxy — $(echo "$vm112" | head -c 120)"
  fi
fi

# --- VM112 direct (wizard) ---
code=$(curl -sk -o /tmp/smoke-vm112-domains.json -w "%{http_code}" \
  "http://10.10.10.112:8090/api/admin/domains" \
  -H "X-Api-Key: ${VM112_KEY}" --max-time 30)
if [[ "$code" == "200" ]]; then
  n=$(python3 -c "import json; print(len(json.load(open('/tmp/smoke-vm112-domains.json')).get('domains',[])))" 2>/dev/null || echo 0)
  pass "VM112 GET /api/admin/domains ($code, ${n} domínios)"
else
  fail "VM112 /api/admin/domains HTTP $code"
fi

echo ""
if [[ "$FAIL" -eq 0 ]]; then
  echo "DESK_RELEASE=GREEN"
  exit 0
fi
echo "DESK_RELEASE=RED (${FAIL} falhas)"
exit 1
