#!/usr/bin/env bash
#
# AI 全流程开发环境自检。
# 用法：
#   ./check-env.sh
#   ./check-env.sh --strict   # 有警告也返回非零
#

set -uo pipefail

STRICT=0
if [ "${1:-}" = "--strict" ]; then
  STRICT=1
fi

fail=0
warn=0

ok()   { echo "✅ $*"; }
bad()  { echo "❌ $*"; fail=$((fail + 1)); }
note() { echo "⚠️  $*"; warn=$((warn + 1)); }

check_cmd() {
  local name="$1"; shift
  if "$@" >/dev/null 2>&1; then
    local version
    version="$("$@" 2>&1 | head -n1)"
    ok "$name ($version)"
  else
    bad "$name 未安装或不可用"
  fi
}

echo "=== 基础工具 ==="
check_cmd "git" git --version
check_cmd "curl" curl --version
check_cmd "jq" jq --version
check_cmd "ripgrep" rg --version
check_cmd "python3" python3 --version

echo
echo "=== 开发工具链 ==="
check_cmd "go" go version
check_cmd "node" node --version
check_cmd "npm" npm --version
check_cmd "docker" docker --version
check_cmd "docker compose" docker compose version

echo
echo "=== Claude Code / MCP ==="
check_cmd "claude" claude --version

if command -v claude >/dev/null 2>&1; then
  if claude mcp list 2>/dev/null | grep -qi "connected"; then
    ok "至少一个 MCP server 已连接"
  else
    note "没有检测到已连接的 MCP server；运行 claude mcp list 查看"
  fi
fi

echo
echo "=== Chrome ==="
CHROME_BIN=""
for bin in google-chrome google-chrome-stable chromium chromium-browser; do
  if command -v "$bin" >/dev/null 2>&1; then
    CHROME_BIN="$bin"
    break
  fi
done

if [ -n "$CHROME_BIN" ]; then
  ok "Chrome ($("$CHROME_BIN" --version 2>&1 | head -n1))"
else
  bad "Chrome 未安装"
fi

PORT="${CHROME_DEBUG_PORT:-9222}"
if curl -fsS "http://127.0.0.1:${PORT}/json/version" >/dev/null 2>&1; then
  ok "Chrome 调试端点可用：http://127.0.0.1:${PORT}"
else
  note "Chrome 调试端点未运行（端口 ${PORT}）；运行 scripts/start-chrome-debug.sh"
fi

echo
echo "=== WSL / 文件系统 ==="
if grep -qi microsoft /proc/version 2>/dev/null; then
  ok "运行在 WSL 中"
  if [ -n "${WSL_DISTRO_NAME:-}" ]; then
    echo "   发行版：$WSL_DISTRO_NAME"
  fi
else
  echo "ℹ️  非 WSL 环境（可能是原生 Linux/macOS）"
fi

if [ -d "/mnt/c" ]; then
  note "检测到 /mnt/c；请确保项目在 ~/projects，而不是 /mnt/c 下"
fi

WATCHES="$(cat /proc/sys/fs/inotify/max_user_watches 2>/dev/null || echo 0)"
if [ "$WATCHES" -ge 100000 ] 2>/dev/null; then
  ok "inotify max_user_watches=$WATCHES"
else
  note "inotify max_user_watches=$WATCHES，较大项目可能不够；建议调到 524288"
fi

echo
echo "=== Python 报告依赖 ==="
if python3 -c "import docx" >/dev/null 2>&1; then
  ok "python-docx 已安装"
else
  note "python-docx 未安装；运行 pip install python-docx"
fi

if python3 -c "import openpyxl" >/dev/null 2>&1; then
  ok "openpyxl 已安装"
else
  note "openpyxl 未安装（可选）"
fi

echo
echo "=== AWS（可选） ==="
if command -v aws >/dev/null 2>&1; then
  ok "AWS CLI ($(aws --version 2>&1 | head -n1))"
  if aws sts get-caller-identity >/dev/null 2>&1; then
    ok "AWS 凭证有效"
  else
    note "AWS 凭证无效或已过期；运行 aws sso login --profile <profile>"
  fi
else
  note "AWS CLI 未安装（如果暂不部署可忽略）"
fi

echo
echo "=== 汇总 ==="
echo "失败：$fail，警告：$warn"

if [ "$fail" -gt 0 ]; then
  exit 1
fi
if [ "$STRICT" = "1" ] && [ "$warn" -gt 0 ]; then
  exit 1
fi
exit 0
