#!/usr/bin/env bash
# D1 smoke — proves the Day 1 acceptance gate end-to-end.
# Builds all 5 binaries, launches them, polls /health on each until
# ready, then runs the actual probes. Exits 0 on success.
#
# Per Opus + Qwen BLOCK #2 review: replaced the prior `sleep 0.5`
# liveness gate with a poll loop so cold-start CI boxes don't race
# the bind.
#
# Usage:  ./scripts/d1_smoke.sh

set -euo pipefail
cd "$(dirname "$0")/.."

export PATH="$PATH:/usr/local/go/bin"

echo "[d1-smoke] building..."
go build -o bin/ ./cmd/...

PIDS=()
trap 'echo "[d1-smoke] cleanup"; kill ${PIDS[@]} 2>/dev/null || true; wait 2>/dev/null || true' EXIT INT TERM

# Poll /health on each service until it returns 200 or we hit the
# 5s budget. Cheaper than a fixed sleep AND deterministic — first
# bind error surfaces immediately, slow boxes wait as long as needed.
poll_health() {
  local name="$1" port="$2" deadline=$(($(date +%s) + 5))
  while [ "$(date +%s)" -lt "$deadline" ]; do
    if curl -sS --max-time 1 "http://127.0.0.1:$port/health" >/dev/null 2>&1; then
      return 0
    fi
    sleep 0.05
  done
  echo "  [d1-smoke] $name (:$port) failed to bind within 5s — log:"
  tail -5 "/tmp/${name}.log" | sed 's/^/    /'
  return 1
}

# Launch services in dependency order — catalogd's rehydrate needs
# storaged up; queryd's initial Refresh needs catalogd up. Gateway is
# last so its proxy probes have all upstreams ready.
echo "[d1-smoke] launching in dep order..."
for SPEC in "storaged:3211" "catalogd:3212" "ingestd:3213" "queryd:3214" "gateway:3110"; do
  NAME="${SPEC%:*}"; PORT="${SPEC#*:}"
  ./bin/$NAME > /tmp/${NAME}.log 2>&1 &
  PIDS+=($!)
  if ! poll_health "$NAME" "$PORT"; then
    exit 1
  fi
done

echo "[d1-smoke] /health probes:"
FAILED=0
for SPEC in "gateway:3110" "storaged:3211" "catalogd:3212" "ingestd:3213" "queryd:3214"; do
  NAME="${SPEC%:*}"; PORT="${SPEC#*:}"
  RESP="$(curl -sS --max-time 2 "http://127.0.0.1:$PORT/health" || echo FAIL)"
  if echo "$RESP" | grep -q "\"service\":\"$NAME\""; then
    echo "  ✓ $NAME (:$PORT) → $RESP"
  else
    echo "  ✗ $NAME (:$PORT) → $RESP"
    FAILED=1
  fi
done

# Post-D6: gateway proxies /v1/ingest → ingestd and /v1/sql → queryd.
# We verify the proxy is wired by sending malformed-but-reachable
# requests and asserting the BACKING service's 400 (not the gateway's
# 502 which would mean the proxy can't reach the upstream).
echo "[d1-smoke] gateway proxy probes (D6+):"
# /v1/ingest with no `name` query param → ingestd returns 400.
HTTP="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 2 -X POST "http://127.0.0.1:3110/v1/ingest")"
if [ "$HTTP" = "400" ]; then
  echo "  ✓ POST /v1/ingest (no name) → 400 from ingestd (proxy wired)"
else
  echo "  ✗ POST /v1/ingest → $HTTP (want 400; 502 means proxy can't reach ingestd)"
  FAILED=1
fi
# /v1/sql with empty body → queryd returns 400.
HTTP="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 2 -X POST -H 'Content-Type: application/json' "http://127.0.0.1:3110/v1/sql")"
if [ "$HTTP" = "400" ]; then
  echo "  ✓ POST /v1/sql (no body) → 400 from queryd (proxy wired)"
else
  echo "  ✗ POST /v1/sql → $HTTP (want 400; 502 means proxy can't reach queryd)"
  FAILED=1
fi

if [ "$FAILED" -ne 0 ]; then
  echo "[d1-smoke] FAILED"
  exit 1
fi
echo "[d1-smoke] D1 acceptance gate: PASSED"
