#!/usr/bin/env bash
# Acliva Suite — device capability probe
# ---------------------------------------------------------------------------
# Tells you which AI mode your device gets in the installed (browser) app:
#   LOCAL   — the AI runs in your browser, on your machine; your data stays local.
#   CLOUD   — the AI runs on our OCI server; included with your subscription.
# Either way you get the full app — this just picks where the brain runs.
#
# Basis: the same detection our engine configurator uses
# (June2026On/infer/tools/configure.py — CPU/RAM/GPU → model-that-fits).
# Honest: this reads real hardware; it does NOT test your browser's WebGPU
# (only Chrome/Edge 113+, Safari 18+ can run local AI). The installed app does
# the final in-browser check and picks LOCAL or CLOUD automatically.
# ---------------------------------------------------------------------------
set -u
say(){ printf '%s\n' "$*"; }
os="$(uname -s 2>/dev/null || echo Unknown)"

# ---- CPU ----
cores="$( { nproc; } 2>/dev/null || { sysctl -n hw.ncpu; } 2>/dev/null || echo '?')"

# ---- RAM (MB) ----
ram_mb=0
case "$os" in
  Darwin) ram_mb=$(( $(sysctl -n hw.memsize 2>/dev/null || echo 0) / 1048576 ));;
  Linux)  ram_mb=$(( $(awk '/MemTotal/{print $2}' /proc/meminfo 2>/dev/null || echo 0) / 1024 ));;
esac
ram_gb=$(( ram_mb / 1024 ))

# ---- GPU (mirrors configure.py: nvidia-smi / Apple unified / other) ----
gpu="none"; vram_mb=0; unified=0; apple_si=0; disc_nv=0
if command -v nvidia-smi >/dev/null 2>&1; then
  ln="$(nvidia-smi --query-gpu=name,memory.total --format=csv,noheader,nounits 2>/dev/null | head -1)"
  if [ -n "$ln" ]; then
    gpu="NVIDIA ${ln%%,*}"
    vram_mb="$(printf '%s' "$ln" | awk -F, '{gsub(/[^0-9]/,"",$2);print ($2==""?0:$2)}')"
    disc_nv=1
  fi
elif [ "$os" = "Darwin" ]; then
  chip="$(sysctl -n machdep.cpu.brand_string 2>/dev/null)"
  case "$chip" in
    *Apple*) apple_si=1; unified=1; gpu="Apple ${chip}"; vram_mb=$ram_mb ;;
    *)       gpu="Intel Mac GPU (integrated)" ;;
  esac
elif [ "$os" = "Linux" ]; then
  g="$(lspci 2>/dev/null | grep -iE 'vga|3d|display' | head -1 | sed 's/.*: //')"
  [ -n "$g" ] && gpu="$g"
fi

# ---- tier decision (thresholds mirror configure.py model_fit) ----
# LOCAL-FULL : can host a 3B-class in-browser (private, snappy)
# LOCAL-LITE : can host a ~1B in-browser (lightweight assist)
# CLOUD      : too little for a useful local model -> use the OCI brain
tier="CLOUD"; model="—"; why=""
if [ "$disc_nv" = "1" ] && [ "$vram_mb" -ge 6144 ]; then
  tier="LOCAL-FULL"; model="a 3B-class model in-browser (WebGPU)"; why="NVIDIA GPU with ${vram_mb}MB VRAM"
elif [ "$apple_si" = "1" ] && [ "$ram_gb" -ge 16 ]; then
  tier="LOCAL-FULL"; model="a 3B-class model in-browser (Metal/WebGPU)"; why="Apple Silicon, ${ram_gb}GB unified memory"
elif [ "$ram_gb" -ge 16 ]; then
  tier="LOCAL-FULL"; model="a 3B-class model in-browser (may be slower on integrated GPU)"; why="${ram_gb}GB RAM"
elif { [ "$disc_nv" = "1" ] && [ "$vram_mb" -ge 4096 ]; } || [ "$apple_si" = "1" ] || [ "$ram_gb" -ge 8 ]; then
  tier="LOCAL-LITE"; model="a ~1B model in-browser (lightweight drafts/assist)"; why="${ram_gb}GB RAM${vram_mb:+, ${vram_mb}MB VRAM}"
else
  tier="CLOUD"; model="cloud inference on our OCI server (included)"; why="${ram_gb}GB RAM — below the local threshold"
fi

echo   "──────────────────────────────────────────────"
say    " Acliva Suite · device capability probe"
echo   "──────────────────────────────────────────────"
say    " OS         : $os"
say    " CPU cores  : $cores"
say    " RAM        : ${ram_gb} GB"
say    " GPU        : $gpu"
[ "$vram_mb" -gt 0 ] && [ "$unified" = "0" ] && say " VRAM       : ${vram_mb} MB"
echo   "──────────────────────────────────────────────"
say    " Recommended mode : $tier"
say    " Runs            : $model"
say    " Because         : $why"
echo   "──────────────────────────────────────────────"
case "$tier" in
  LOCAL-FULL) say " ✓ Your device can run the AI privately, in your browser.";;
  LOCAL-LITE) say " ✓ Local AI works for light tasks; heavy jobs fall back to cloud.";;
  CLOUD)      say " ✓ You'll use our cloud AI — it's included in your plan, nothing to install.";;
esac
say " Note: local AI also needs a WebGPU browser (Chrome/Edge 113+, Safari 18+)."
say " The installed app confirms this and switches automatically. No data leaves"
say " your device in LOCAL mode; CLOUD mode processes on our OCI server."
