#!/usr/bin/env bash
# Reachability smoke for mobile-app + console services (public or CRASHES_BASE).
# Exit 0 when all required checks pass; prints FAIL lines for ticket triage.
set -uo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PUBLIC="${PUBLIC_BASE:-https://apps.f0xx.org}"
CRASHES="${CRASHES_BASE:-${PUBLIC}/app/androidcast_project/crashes}"
GRAPH="${GRAPH_BASE:-${PUBLIC}/app/androidcast_project/graphs}"
HUB="${HUB_BASE:-${PUBLIC}/app/androidcast_project/}"
BUILD="${BUILD_BASE:-${PUBLIC}/app/androidcast_project/build/}"
OTA="${OTA_CHANNEL:-${PUBLIC}/v0/ota/channel/stable.json}"

SHORTENER="${URL_SHORTENER_BASE:-https://s.f0xx.org}"

fail=0
ok() { echo "OK   $*"; }
bad() { echo "FAIL $*"; fail=1; }
note() { echo "WARN $*"; }

http_code() {
  curl -sS -o /dev/null -w '%{http_code}' --max-time "${CURL_TIMEOUT:-15}" "$1" 2>/dev/null || echo "000"
}

js_syntax() {
  local url="$1"
  local tmp
  tmp="$(mktemp --suffix=.js)"
  if ! curl -sSLf --max-time "${CURL_TIMEOUT:-15}" "$url" -o "$tmp" 2>/dev/null; then
    rm -f "$tmp"
    echo "fetch"
    return 1
  fi
  if node --check "$tmp" 2>/dev/null; then
    rm -f "$tmp"
    echo "ok"
    return 0
  fi
  rm -f "$tmp"
  echo "syntax"
  return 1
}

echo "== Android Cast service reachability =="
echo "PUBLIC=$PUBLIC"
echo

# Hub
code="$(http_code "$HUB")"
[[ "$code" =~ ^(200|301|302)$ ]] && ok "hub $HUB HTTP $code" || bad "hub $HUB HTTP $code"

# Crashes console (login redirect OK)
code="$(http_code "$CRASHES/")"
[[ "$code" =~ ^(200|302)$ ]] && ok "crashes $CRASHES/ HTTP $code" || bad "crashes $CRASHES/ HTTP $code"

# Graphs
code="$(http_code "$GRAPH/")"
[[ "$code" =~ ^(200|302)$ ]] && ok "graphs $GRAPH/ HTTP $code" || bad "graphs $GRAPH/ HTTP $code"

# Builder
code="$(http_code "$BUILD")"
[[ "$code" =~ ^(200|302)$ ]] && ok "build $BUILD HTTP $code" || bad "build $BUILD HTTP $code"

# OTA channel (may 404 until wired — warn only)
code="$(http_code "$OTA")"
if [[ "$code" == "200" ]]; then
  ok "ota $OTA HTTP $code"
elif [[ "$code" == "404" ]]; then
  note "ota $OTA HTTP 404 — BE mount empty or channel missing"
elif [[ "$code" == "400" ]]; then
  note "ota $OTA HTTP 400 — FE missing location /v0/ota/ → BE :80 (run install-fe-ota-nginx.sh on FE)"
else
  bad "ota $OTA HTTP $code"
fi

# URL shortener (opt-in vhost — warn until PHP + MariaDB live)
code="$(http_code "$SHORTENER/api/v1/health")"
if [[ "$code" == "200" ]]; then
  ok "url-shortener $SHORTENER/api/v1/health HTTP $code"
elif [[ "$code" =~ ^(000|502|503|504)$ ]]; then
  note "url-shortener $SHORTENER/api/v1/health HTTP $code — nginx up? sync backend/url-shortener + php-fpm"
else
  note "url-shortener $SHORTENER/api/v1/health HTTP $code (stub expects 200 when docroot synced)"
fi

# Device APIs (no session) — 403 on public edge is OK if upload/heartbeat work
code="$(http_code "$CRASHES/api/diag.php")"
if [[ "$code" == "200" ]]; then
  ok "diag API HTTP $code"
elif [[ "$code" == "403" ]]; then
  note "diag API HTTP 403 (edge block; use upload/heartbeat for device path)"
else
  bad "diag API HTTP $code"
fi
code="$(http_code "$CRASHES/api/heartbeat.php")"
[[ "$code" =~ ^(200|400|405)$ ]] && ok "heartbeat API reachable HTTP $code" || bad "heartbeat API HTTP $code"

# JS assets
for pair in \
  "crashes-app:${CRASHES}/assets/js/app.js" \
  "graphs-js:${GRAPH}/assets/js/graphs.js" \
  "tickets-js:${CRASHES}/assets/js/tickets.js" \
  "i18n-js:${CRASHES}/assets/js/i18n.js"; do
  name="${pair%%:*}"
  url="${pair#*:}"
  st="$(js_syntax "$url" || true)"
  [[ "$st" == "ok" ]] && ok "js $name syntax" || bad "js $name ($st) $url"
done

# Authenticated API smoke (optional)
if [[ "${RUN_API_SMOKE:-1}" == "1" ]] && command -v bash >/dev/null 2>&1; then
  if CRASHES_BASE="$CRASHES" bash "$ROOT/scripts/test_remote_access_api.sh" >/dev/null 2>&1; then
    ok "remote_access API smoke"
  else
    bad "remote_access API smoke"
  fi
  if CRASHES_BASE="$CRASHES" bash "$ROOT/scripts/test_rbac_api.sh" >/dev/null 2>&1; then
    ok "rbac API smoke"
  else
    bad "rbac API smoke"
  fi
fi

echo
if [[ "$fail" -eq 0 ]]; then
  echo "All required service checks passed."
else
  echo "Some checks failed — file tickets via ingest or Issues dashboard."
fi
exit "$fail"
