#!/usr/bin/env bash
# pollmail — Hardened health poller with signal handling
set -euo pipefail
shopt -s inherit_errexit 2>/dev/null || true

SDIR="$(cd "$(dirname "$0")" && pwd)"
source "${SDIR}/se_client.sh"

# ── Configuration ──────────────────────────────────────────────────────────
readonly MAX_FAIL=3
readonly INTERVAL=5
FAIL_COUNT=0

# ── Signal handling ────────────────────────────────────────────────────────
cleanup_poll() {
    se_log "info" "health_poll: shutting down (fail_count=${FAIL_COUNT})"
    exit 0
}
trap cleanup_poll SIGTERM SIGINT SIGHUP

# ── Main loop ──────────────────────────────────────────────────────────────
while true; do
    RESP="$(se_curl_get "/a/health" 2>/dev/null || printf '%s' '')"

    if [[ -z "${RESP}" ]]; then
        ((FAIL_COUNT++)) || true
        se_log "err" "health_poll: empty response fail=${FAIL_COUNT}"
    else
        STATUS="$(printf '%s' "${RESP}" | jq -r '.status // "unknown"' 2>/dev/null || printf '%s' 'unknown')"
        VERSION="$(printf '%s' "${RESP}" | jq -r '.version // "unknown"' 2>/dev/null || printf '%s' 'unknown')"
        SERVICES="$(printf '%s' "${RESP}" | jq -c '.services // {}' 2>/dev/null || printf '%s' '{}')"

        if [[ "${STATUS}" == "ok" ]]; then
            FAIL_COUNT=0
        else
            ((FAIL_COUNT++)) || true
        fi
        se_log "info" "health_poll: status=${STATUS} version=${VERSION} fail=${FAIL_COUNT}"
    fi

    if [[ "${FAIL_COUNT}" -ge "${MAX_FAIL}" ]]; then
        se_log "alert" "health_poll: ${MAX_FAIL} consecutive failures, alerting"
        printf '%s %s UNHEALTHY engine=%s services=%s\n' \
            "${NODE_ID}" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
            "${VERSION:-unknown}" "${SERVICES:-unknown}" >&2
        exit 1
    fi

    sleep "${INTERVAL}"
done
