#!/usr/bin/env bash
set -Eeuo pipefail
REPO="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)"
PROFILE_NAME="${CHATGPT_COMPUTER_MCP_PROFILE:-chatgpt-computer-mcp}"
HELPER="$REPO/dist/native/chatgpt-computer-wayland-helper"
fail=0
ok() { printf 'PASS  %s\n' "$*"; }
bad() { printf 'FAIL  %s\n' "$*" >&2; fail=1; }
info() { printf 'INFO  %s\n' "$*"; }

[[ "$(uname -s)" == Linux ]] && ok "Linux host" || bad "Linux is required"
if command -v node >/dev/null 2>&1; then
  major="$(node -p 'Number(process.versions.node.split(`.`)[0])')"
  (( major >= 22 )) && ok "Node $(node --version)" || bad "Node.js 22+ required; found $(node --version)"
else
  bad "node not found"
fi

session="${XDG_SESSION_TYPE:-unknown}"
info "session type: $session"
if [[ "$session" == wayland || -n "${WAYLAND_DISPLAY:-}" ]]; then
  [[ -n "${WAYLAND_DISPLAY:-}" ]] && ok "WAYLAND_DISPLAY is set" || bad "WAYLAND_DISPLAY is not set"
  [[ -n "${DBUS_SESSION_BUS_ADDRESS:-}" ]] && ok "session D-Bus is available" || info "DBUS_SESSION_BUS_ADDRESS is unset; GLib may still discover the user bus"
  command -v gdbus >/dev/null 2>&1 && ok "gdbus available" || bad "gdbus missing"
  if command -v gdbus >/dev/null 2>&1 && gdbus introspect --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop >/dev/null 2>&1; then
    ok "xdg-desktop-portal is reachable"
  else
    bad "xdg-desktop-portal is not reachable on the user bus"
  fi
  [[ -x "$HELPER" ]] && ok "native Wayland helper exists" || bad "native Wayland helper missing; run pnpm build"
  if [[ -x "$HELPER" ]]; then
    if ldd "$HELPER" 2>/dev/null | grep -q 'not found'; then bad "native Wayland helper has missing runtime libraries"; else ok "native Wayland helper runtime libraries resolve"; fi
  fi
else
  for cmd in xrandr ffmpeg xdotool; do
    command -v "$cmd" >/dev/null 2>&1 && ok "$cmd available" || bad "$cmd missing"
  done
  if [[ -n "${DISPLAY:-}" ]]; then
    ok "DISPLAY is set"
    xrandr --listmonitors >/dev/null 2>&1 && ok "xrandr can query DISPLAY" || bad "xrandr cannot query DISPLAY"
  else
    bad "DISPLAY is not set"
  fi
  [[ -x "$HELPER" ]] && info "native Wayland helper is built for future Wayland sessions" || info "native Wayland helper not built"
fi

if [[ -f "$REPO/config.local.json" ]]; then
  node -e 'JSON.parse(require("fs").readFileSync(process.argv[1], "utf8"))' "$REPO/config.local.json" >/dev/null \
    && ok "config.local.json parses" || bad "config.local.json is invalid JSON"
else
  info "config.local.json absent; defaults/example may be used"
fi

if [[ -f "$REPO/dist/src/stdio.js" ]]; then ok "TypeScript build output exists"; else info "build output absent; run pnpm build"; fi

if command -v tunnel-client >/dev/null 2>&1; then
  ok "tunnel-client available"
  if [[ -r "$REPO/.secrets/runtime-api-key" ]]; then
    export CONTROL_PLANE_API_KEY="$(head -n1 "$REPO/.secrets/runtime-api-key" | tr -d '\r\n')"
    export HEALTH_LISTEN_ADDR="127.0.0.1:0"
    export CHATGPT_COMPUTER_MCP_CONFIG="$REPO/config.local.json"
    if tunnel-client doctor --profile "$PROFILE_NAME" --explain >/dev/null 2>&1; then
      ok "tunnel-client doctor ($PROFILE_NAME)"
    else
      bad "tunnel-client doctor failed for profile $PROFILE_NAME"
    fi
  else
    info "runtime tunnel key not present; tunnel diagnostics skipped"
  fi
else
  info "tunnel-client not installed; local stdio development is still possible"
fi

"$REPO/scripts/secret-check.sh" || fail=1
exit "$fail"
