#!/usr/bin/env bash
# G1 — validação console.ligbox.com.br (Spec 019 mínimo)
set -euo pipefail

PUBLIC_URL="${CONSOLE_PUBLIC_URL:-https://console.ligbox.com.br}"
LAN_URL="${CONSOLE_LAN_URL:-http://10.10.10.123:8100}"
ONBOARD_SMOKE="${ONBOARD_SMOKE_URL:-https://onboard.ligbox.com.br/onboard}"

failures=0
ok() { echo "OK   [g1]: $*"; }
fail() { echo "FAIL [g1]: $*"; failures=$((failures + 1)); }

code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 10 "$LAN_URL/health" 2>/dev/null || echo "000")
[[ "$code" == "200" ]] && ok "VM123 LAN health $code" || fail "VM123 LAN health $code"

code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 15 "$PUBLIC_URL/health" 2>/dev/null || echo "000")
[[ "$code" == "200" ]] && ok "Public health $code" || fail "Public health $code"

code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 15 "$PUBLIC_URL/" 2>/dev/null || echo "000")
[[ "$code" == "200" ]] && ok "Public SPA $code" || fail "Public SPA $code"

code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 15 "$ONBOARD_SMOKE" 2>/dev/null || echo "000")
[[ "$code" == "200" ]] && ok "Onboard smoke (026) $code" || fail "Onboard smoke (026) $code"

if [[ "$failures" -gt 0 ]]; then
  echo "G1 validation FAILED ($failures)"
  exit 1
fi
echo "G1 validation PASSED"
exit 0
