#!/usr/bin/env bash
# 检查爬虫数据与面板数据是否联通
# 用法: ./scripts/check-crawler-panel-connectivity.sh
# 需先启动: npm run api；可选: npm run gdelt
set -e

API_URL="${API_URL:-http://localhost:3001}"
CRAWLER_URL="${CRAWLER_URL:-http://localhost:8000}"

echo "=========================================="
echo "爬虫 ↔ 面板 联通检查"
echo "API: $API_URL | Crawler: $CRAWLER_URL"
echo "=========================================="

# 1. 爬虫侧：situation_update 条数
CRAWLER_SU_COUNT=""
if curl -sf "$CRAWLER_URL/crawler/status" >/dev/null 2>&1; then
  if command -v jq &>/dev/null; then
    CRAWLER_SU_COUNT=$(curl -sf "$CRAWLER_URL/crawler/status" | jq -r '.situation_update_count // "?"')
  else
    CRAWLER_SU_COUNT="(需 jq 查看)"
  fi
  echo "[爬虫] situation_update 条数: $CRAWLER_SU_COUNT"
else
  echo "[爬虫] 未启动或不可达 (curl $CRAWLER_URL/crawler/status 失败)"
fi

# 2. 面板侧：API 返回的 recentUpdates 条数、lastUpdated
if ! curl -sf "$API_URL/api/health" >/dev/null 2>&1; then
  echo "[API] 未启动，请先运行: npm run api"
  exit 1
fi

SIT=$(curl -sf "$API_URL/api/situation" 2>/dev/null || echo "{}")
if command -v jq &>/dev/null; then
  RU_LEN=$(echo "$SIT" | jq '.recentUpdates | length')
  LAST=$(echo "$SIT" | jq -r '.lastUpdated // "?"')
  echo "[面板] recentUpdates 条数: $RU_LEN | lastUpdated: $LAST"
else
  echo "[面板] 态势数据已获取 (安装 jq 可显示条数)"
fi

# 3. 一致性：爬虫写的是 server/data.db，Node 通过 notify 重载后应一致
echo ""
echo "--- 联动说明 ---"
echo "  • 事件脉络 (recentUpdates) ← situation_update 表，由爬虫 write_updates() 写入"
echo "  • 爬虫每次抓取后会 POST $API_URL/api/crawler/notify，Node 会 reloadFromFile() 后广播"
echo "  • 若爬虫有数据但面板 recentUpdates 很少/为空：检查 Node 终端是否出现 [crawler/notify] DB 已重载"
echo "  • 若从未出现：检查 API_BASE 是否指向当前 API（默认 http://localhost:3001）"
echo "  • 战损/基地/力量指数：仅当 AI/规则从新闻中提取到数字时才会更新，多数新闻不会触发"
echo "=========================================="

# 4. 可选：触发一次 notify 看 Node 是否重载（不启动爬虫时可用于测试）
# 非交互时跳过；交互时可用: echo y | ./scripts/check-crawler-panel-connectivity.sh
if [[ -t 0 ]]; then
  echo ""
  read -r -p "是否发送一次 POST /api/crawler/notify 测试 Node 重载? [y/N] " ans
  if [[ "${ans,,}" = "y" ]]; then
    curl -sf -X POST "$API_URL/api/crawler/notify" && echo "  已发送 notify，请看 Node 终端是否打印 [crawler/notify] DB 已重载"
  fi
fi
