#!/usr/bin/env bash
# 验证爬虫 → 数据库 → API → 前端 全链路
# 用法: ./scripts/verify-pipeline.sh [--start-crawler]
set -e

API_URL="${API_URL:-http://localhost:3001}"
CRAWLER_URL="${CRAWLER_URL:-http://localhost:8000}"
START_CRAWLER=false
[[ "${1:-}" = "--start-crawler" ]] && START_CRAWLER=true

PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"

echo "=========================================="
echo "US-Iran 态势面板 链路验证"
echo "API: $API_URL | Crawler: $CRAWLER_URL"
echo "=========================================="
echo ""

# 可选：启动爬虫
if $START_CRAWLER; then
  echo "[0/6] 启动爬虫..."
  if curl -sf "$CRAWLER_URL/crawler/status" >/dev/null 2>&1; then
    echo "  ✓ 爬虫已在运行"
  else
    cd "$PROJECT_ROOT/crawler"
    python3 -c "import uvicorn" 2>/dev/null || { echo "  需安装: pip install uvicorn"; exit 1; }
    uvicorn realtime_conflict_service:app --host 127.0.0.1 --port 8000 &
    echo "  等待爬虫就绪..."
    for i in $(seq 1 15); do
      sleep 2
      if curl -sf "$CRAWLER_URL/crawler/status" >/dev/null 2>&1; then
        echo "  ✓ 爬虫已启动"
        echo "  等待首次 RSS 抓取（约 70 秒）..."
        sleep 70
        break
      fi
    done
    if ! curl -sf "$CRAWLER_URL/crawler/status" >/dev/null 2>&1; then
      echo "  ✗ 爬虫启动超时"
      exit 1
    fi
  fi
  echo ""
fi

# 1. API 健康检查
echo "[1/6] API 健康检查..."
if curl -sf "$API_URL/api/health" > /dev/null; then
  echo "  ✓ API 正常"
else
  echo "  ✗ API 无响应，请先运行: npm run api"
  exit 1
fi

# 2. 态势数据
echo "[2/6] 态势数据..."
SIT=$(curl -sf "$API_URL/api/situation" 2>/dev/null || echo "{}")
if echo "$SIT" | grep -q "lastUpdated"; then
  echo "  ✓ 态势数据可读"
  LAST=$(echo "$SIT" | grep -o '"lastUpdated":"[^"]*"' | head -1)
  echo "    $LAST"
else
  echo "  ✗ 态势数据异常"
  exit 1
fi

# 3. 爬虫状态
echo "[3/6] 爬虫状态..."
CRAWLER=$(curl -sf "$CRAWLER_URL/crawler/status" 2>/dev/null || echo "{}")
if echo "$CRAWLER" | grep -q "db_path\|db_exists"; then
  echo "  ✓ 爬虫服务可访问"
  if command -v jq &>/dev/null; then
    CNT=$(echo "$CRAWLER" | jq -r '.situation_update_count // "?"')
    echo "    situation_update 条数: $CNT"
  fi
else
  echo "  ⚠ 爬虫未启动或不可达（可选，需单独运行爬虫）"
fi

# 4. 资讯表
echo "[4/6] 资讯表 news_content..."
NEWS=$(curl -sf "$API_URL/api/news?limit=3" 2>/dev/null || echo '{"items":[]}')
if echo "$NEWS" | grep -q '"items"'; then
  if command -v jq &>/dev/null; then
    N=$(echo "$NEWS" | jq '.items | length')
    echo "  ✓ 最近 $N 条资讯"
  else
    echo "  ✓ 资讯接口可读"
  fi
else
  echo "  ⚠ news_content 可能为空（爬虫未跑或刚启动）"
fi

# 5. 战损数据
echo "[5/6] 战损数据 combat_losses..."
if echo "$SIT" | grep -q "personnelCasualties"; then
  echo "  ✓ 战损字段存在"
  if command -v jq &>/dev/null; then
    US_K=$(echo "$SIT" | jq -r '.usForces.combatLosses.personnelCasualties.killed // "?"')
    IR_K=$(echo "$SIT" | jq -r '.iranForces.combatLosses.personnelCasualties.killed // "?"')
    echo "    美军阵亡: $US_K | 伊朗阵亡: $IR_K"
  fi
else
  echo "  ✗ 战损结构异常"
fi

# 6. 通知接口（仅验证可调用）
echo "[6/6] 通知接口 POST /api/crawler/notify..."
NOTIFY=$(curl -sf -X POST "$API_URL/api/crawler/notify" 2>/dev/null || echo "{}")
if echo "$NOTIFY" | grep -q '"ok"'; then
  echo "  ✓ 通知接口正常"
else
  echo "  ⚠ 通知接口可能异常"
fi

echo ""
echo "=========================================="
echo "验证完成。"
echo ""
echo "建议："
echo "  - 访问 $API_URL/db 查看各表数据"
echo "  - 爬虫未启动时: ./scripts/verify-pipeline.sh --start-crawler"
echo "  - 或手动启动: cd crawler && uvicorn realtime_conflict_service:app --port 8000"
echo "=========================================="
