#!/usr/bin/env bash
# adom-mouser smoke — QUOTA-FRUGAL.
#
# This script is safe to run HOURLY: it hits /health with no live ping
# (zero Mouser API calls) and asserts the service is up, the API key is
# configured, and the upstream-status flag looks reasonable. It does NOT
# fire a live search. Use `tests/smoke-live.sh` (daily) for end-to-end.
#
# What "healthy" means here:
#   - /health responds 200 with ok=true
#   - MOUSER_API_KEY is configured (error=null)
#   - The cached quota-state block is readable
# It is explicitly NOT a failure if Mouser reported quota-exhausted
# earlier today — that's a business condition the quota tracker owns
# its own Kel alert for.
#
# Usage:
#   tests/smoke.sh                                     # deployed
#   tests/smoke.sh http://127.0.0.1:8775               # local

set -u
BACKEND="${1:-${MOUSER_API:-https://mouser-xr8t4f5d01bt.adom.cloud}}"
BACKEND="${BACKEND%/}"

if [ -t 1 ]; then
  C_OK=$'\033[32m'; C_FAIL=$'\033[31m'; C_DIM=$'\033[2m'; C_BOLD=$'\033[1m'; C_RESET=$'\033[0m'
else
  C_OK=''; C_FAIL=''; C_DIM=''; C_BOLD=''; C_RESET=''
fi

fail=0; failures=()

echo "${C_BOLD}adom-mouser smoke (quota-free)${C_RESET}  ${C_DIM}$BACKEND${C_RESET}"
echo "${C_DIM}────────────────────────────────────────────────────────${C_RESET}"

health=$(curl -sS --max-time 10 "$BACKEND/health" 2>/dev/null)
if [ -z "$health" ]; then
  echo "${C_FAIL}FAIL${C_RESET}  /health unreachable"; exit 1
fi

ok=$(printf '%s' "$health" | jq -r '.ok // false')
version=$(printf '%s' "$health" | jq -r '.version // "?"')
error=$(printf '%s' "$health" | jq -r '.error // empty')
searches=$(printf '%s' "$health" | jq -r '.quota.searches // 0')
results=$(printf '%s' "$health" | jq -r '.quota.results_returned // 0')
quota_hit=$(printf '%s' "$health" | jq -r '.quota.first_quota_hit_ts // 0')

if [ "$ok" != "true" ]; then
  fail=$((fail+1)); failures+=("/health reported ok=false (MOUSER_API_KEY missing?)")
  printf '%sFAIL%s  /health ok=%s  err=%s\n' "$C_FAIL" "$C_RESET" "$ok" "$error"
else
  printf '%sPASS%s  /health ok=true  version=%s\n' "$C_OK" "$C_RESET" "$version"
fi

if [ -n "$error" ]; then
  fail=$((fail+1)); failures+=("health.error = $error")
  printf '%sFAIL%s  health.error populated  →  %s\n' "$C_FAIL" "$C_RESET" "$error"
fi

# Quota block must be present + parseable. NOT a failure if exhausted —
# that's the tracker's job to alert on.
if [ "$searches" = "null" ]; then
  fail=$((fail+1)); failures+=("quota telemetry missing from /health (too old? bump mouser)")
  printf '%sFAIL%s  quota block missing — service running old binary\n' "$C_FAIL" "$C_RESET"
else
  if [ "$quota_hit" = "0" ] || [ "$quota_hit" = "null" ]; then
    printf '%sPASS%s  quota today: %s searches / %s results returned (not exhausted)\n' \
      "$C_OK" "$C_RESET" "$searches" "$results"
  else
    printf '%sPASS%s  quota today: %s searches / %s results served BEFORE quota hit (informational)\n' \
      "$C_OK" "$C_RESET" "$searches" "$results"
  fi
fi

echo "${C_DIM}────────────────────────────────────────────────────────${C_RESET}"
if [ "$fail" -eq 0 ]; then
  echo "${C_OK}${C_BOLD}OK${C_RESET}  hourly smoke green (0 API calls spent)"
  exit 0
else
  echo "${C_FAIL}${C_BOLD}FAIL${C_RESET}  $fail check(s) failed"
  for f in "${failures[@]}"; do echo "      - $f"; done
  exit 1
fi