#!/bin/bash
# check-release.sh — release.feature 的可執行佐證：列出每個 publish target 的目前狀態
#
# 用途：「完成推送」後跑這個，一眼確認所有對外介面是否同步到位（壓測報告第 3 點）。
#   - CF workers / landing：是否由 CI 部署（看 deploy.yml 掃描範圍）
#   - npm CLI：本機 cli/package.json version vs npm 上的版本（最常漂移的一個）
#   - MCP：是否有未部署變動
#
# 不做破壞性動作，只讀狀態。需要 network（npm view / curl）才能查線上版本；
# 無 network 時只比對本機可知的部分，並誠實標「無法查線上」。
#
# 對應：tests/release.feature、.claude/rules/05-deploy-convention.md

set -uo pipefail
cd "$(dirname "$0")/.." || exit 1

GREEN='\033[0;32m'; YELLOW='\033[0;33m'; RED='\033[0;31m'; GRAY='\033[0;90m'; NC='\033[0m'
ok()   { echo -e "  ${GREEN}✓${NC} $1"; }
warn() { echo -e "  ${YELLOW}⚠${NC} $1"; }
err()  { echo -e "  ${RED}✗${NC} $1"; }
info() { echo -e "  ${GRAY}·${NC} $1"; }

echo ""
echo "═══ arcrun release 狀態檢查（release.feature 佐證）═══"
echo ""

# ── 0. Git 同步（最關鍵：self-hosted 從 origin/main codeload 抓 worker）──────
# 壓測階段 6 抓到的漂移：CLI 新（npm）但 worker 舊（main 沒推 init-seed）→ acr init
#   從 main 抓到舊 cypher → 薄殼打不存在的 API → 404。
#   ∴ deploy 前 git 必須先 push main，否則 self-hosted 用戶裝到舊碼。
echo "【0. Git 同步（deploy 前必過——self-hosted 從 origin/main 抓 worker 源）】"
GIT_BLOCK=0
if git rev-parse --git-dir >/dev/null 2>&1; then
  BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  DIRTY=$(git status --porcelain 2>/dev/null | grep -v '^?? ' | head -1)
  if [ -n "$DIRTY" ]; then
    err "工作目錄有未 commit 的變動 → deploy 前先 commit（否則部署的不是 GitHub 上的碼）"
    GIT_BLOCK=1
  else
    ok "工作目錄乾淨（無未 commit 追蹤檔變動）"
  fi
  # local main vs origin/main
  git fetch origin "$BRANCH" --quiet 2>/dev/null || true
  AHEAD=$(git rev-list --count "origin/$BRANCH..$BRANCH" 2>/dev/null || echo "?")
  BEHIND=$(git rev-list --count "$BRANCH..origin/$BRANCH" 2>/dev/null || echo "?")
  if [ "$AHEAD" = "0" ] && [ "$BEHIND" = "0" ]; then
    ok "$BRANCH 與 origin/$BRANCH 同步（self-hosted codeload 會抓到最新 worker）"
  elif [ "$AHEAD" != "0" ] && [ "$AHEAD" != "?" ]; then
    err "$BRANCH 領先 origin/$BRANCH $AHEAD 個 commit 未 push → **deploy 前先 git push**（否則 self-hosted 裝到舊 worker，重演壓測階段 6 的 seed 404）"
    GIT_BLOCK=1
  else
    warn "${BRANCH} vs origin/${BRANCH}：ahead=${AHEAD} behind=${BEHIND}（確認狀態）"
  fi
else
  info "非 git repo，跳過 git 同步檢查"
fi
echo ""

# ── 1. CF Worker 部署單位（由 scripts/local-deploy.sh 掃描部署）──────────
# 註：GH Actions 2026-05-16 停用、公開 repo 已移除 .github/，現行 deploy 走 local-deploy.sh。
echo "【CF Workers（scripts/local-deploy.sh 掃 wrangler.toml 後 wrangler deploy）】"
WORKER_DIRS=$(find . -name 'wrangler.toml' -not -path '*/node_modules/*' -not -name 'wrangler.test.toml' \
  | xargs -n1 dirname 2>/dev/null | sort -u)
WORKER_COUNT=$(echo "$WORKER_DIRS" | grep -c . )
info "掃到 $WORKER_COUNT 個部署單位（push 對應目錄即自動 deploy）"
echo "$WORKER_DIRS" | sed 's/^/      /'

# ── 2. CLI npm（最常漂移）──────────────────────────────────────────────
echo ""
echo "【npm CLI（arcrun / acr）】"
LOCAL_CLI=$(node -p "require('./cli/package.json').version" 2>/dev/null || echo "?")
if command -v npm >/dev/null 2>&1; then
  REMOTE_CLI=$(npm view arcrun version 2>/dev/null || echo "無法查線上")
else
  REMOTE_CLI="無 npm CLI 可查"
fi
if [ "$LOCAL_CLI" = "$REMOTE_CLI" ]; then
  ok "CLI 版本同步：本機 $LOCAL_CLI = npm $REMOTE_CLI"
elif [ "$REMOTE_CLI" = "無法查線上" ] || [ "$REMOTE_CLI" = "無 npm CLI 可查" ]; then
  info "本機 CLI ${LOCAL_CLI}（${REMOTE_CLI}）"
else
  warn "CLI 版本漂移：本機 $LOCAL_CLI ≠ npm $REMOTE_CLI"
  warn "→ 跑 scripts/local-deploy.sh（含 cli/ 變動時）會 npm publish；或手動 cd cli && npm publish"
fi

# 偵測「cli 有未發佈變動但版本可能未 bump」
if git rev-parse --git-dir >/dev/null 2>&1; then
  LAST_CLI_PKG_CHANGE=$(git log -1 --format=%h -- cli/package.json 2>/dev/null || echo "")
  LAST_CLI_SRC_CHANGE=$(git log -1 --format=%h -- cli/src 2>/dev/null || echo "")
  if [ -n "$LAST_CLI_SRC_CHANGE" ] && [ "$LAST_CLI_SRC_CHANGE" != "$LAST_CLI_PKG_CHANGE" ]; then
    info "cli/src 最後變動（${LAST_CLI_SRC_CHANGE}）晚於 package.json（${LAST_CLI_PKG_CHANGE}）→ 確認是否需 bump version"
  fi
fi

# ── 3. cypher-executor 線上健康 ────────────────────────────────────────
echo ""
echo "【cypher-executor（API 本體）】"
if command -v curl >/dev/null 2>&1; then
  CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 8 https://cypher.arcrun.dev/health 2>/dev/null || echo "000")
  if [ "$CODE" = "200" ]; then
    ok "https://cypher.arcrun.dev/health → 200"
  else
    warn "https://cypher.arcrun.dev/health → ${CODE}（部署中或不可達？）"
  fi
else
  info "無 curl，跳過線上健康檢查"
fi

# ── 4. MCP server（已搬進主庫 arcrun/mcp/）──────────────────────────────
echo ""
echo "【MCP server（arcrun/mcp/，已進主庫）】"
if [ -d mcp ] && [ -f mcp/wrangler.toml ]; then
  MCP_NAME=$(grep '^name' mcp/wrangler.toml | head -1 | sed -E 's/^name[[:space:]]*=[[:space:]]*"([^"]*)".*/\1/')
  ok "arcrun/mcp/ 存在（worker: ${MCP_NAME}）；由 local-deploy.sh 一併 wrangler deploy"
  info "薄殼一致性：MCP 工具集應對齊 cypher-executor 最新 API（rule 07）"
  info "用戶連哪台 MCP 由 .mcp.json 決定（acr mcp-setup 依 config mcp_url 產）"
else
  warn "找不到 arcrun/mcp/（應已搬進主庫）"
fi

echo ""
if [ "$GIT_BLOCK" = "1" ]; then
  echo "═══ ✗ Git 未同步 — deploy 前必須先 commit + git push（見上「0. Git 同步」）═══"
  echo "   流程見 RELEASE-CHECKLIST.md。push 完再跑本檢查確認全綠。"
  echo ""
  exit 1
fi
echo "═══ ✓ 可 deploy。⚠ = 需注意；· = 資訊；✗ = 失敗。流程見 RELEASE-CHECKLIST.md ═══"
echo ""
