#!/bin/bash
# 验证 API 契约一致性
# 依赖: curl, jq
# 用法: ./verify-api-contracts.sh [API_BASE_URL]

set -e

API_BASE="${1:-http://localhost:8080}"

echo "=== 验证 API 契约 ==="
echo "API 地址: $API_BASE"
echo ""

# 检查依赖
command -v curl >/dev/null 2>&1 || { echo "[ERROR] 需要安装 curl"; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "[ERROR] 需要安装 jq"; exit 1; }

FAILED=0
PASSED=0

# 必需的接口列表
ENDPOINTS=(
    "/api/workflow/create"
    "/api/workflow/detail"
    "/api/workflow/status"
    "/api/workflow/transition"
    "/api/workflow/assign"
    "/api/workflow/delegate"
    "/api/workflow/revoke"
    "/api/workflow/callback"
    "/api/workflow/list"
    "/api/workflow/my/created"
    "/api/workflow/my/assigned"
    "/api/workflow/my/pending"
    "/api/workflow/history"
    "/api/workflow/diagram"
)

echo "--- 检查接口可访问性 ---"
for endpoint in "${ENDPOINTS[@]}"; do
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$API_BASE$endpoint" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer test-token" \
        -d '{}' 2>/dev/null || echo "000")

    if [ "$HTTP_CODE" == "000" ]; then
        echo "[FAIL] 接口 $endpoint 无法连接"
        ((FAILED++))
    elif [ "$HTTP_CODE" == "404" ]; then
        echo "[FAIL] 接口 $endpoint 不存在 (404)"
        ((FAILED++))
    elif [ "$HTTP_CODE" == "401" ] || [ "$HTTP_CODE" == "403" ]; then
        echo "[PASS] 接口 $endpoint 存在 (需要认证: $HTTP_CODE)"
        ((PASSED++))
    else
        echo "[PASS] 接口 $endpoint 可访问 (HTTP $HTTP_CODE)"
        ((PASSED++))
    fi
done

echo ""
echo "--- 检查响应格式 ---"

# 测试 /api/workflow/list 接口响应格式
RESP=$(curl -s -X POST "$API_BASE/api/workflow/list" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer test-token" \
    -d '{"page": 1, "page_size": 1}' 2>/dev/null || echo '{}')

# 检查 code 字段
if echo "$RESP" | jq -e '.code' > /dev/null 2>&1; then
    CODE=$(echo "$RESP" | jq '.code')
    echo "[PASS] 响应包含 code 字段 (值: $CODE)"
    ((PASSED++))
else
    echo "[FAIL] 响应缺少 code 字段"
    ((FAILED++))
fi

# 检查 message 字段
if echo "$RESP" | jq -e '.message' > /dev/null 2>&1; then
    echo "[PASS] 响应包含 message 字段"
    ((PASSED++))
else
    echo "[FAIL] 响应缺少 message 字段"
    ((FAILED++))
fi

# 检查 data 字段
if echo "$RESP" | jq -e '.data' > /dev/null 2>&1; then
    echo "[PASS] 响应包含 data 字段"
    ((PASSED++))
else
    echo "[WARN] 响应可能缺少 data 字段（空响应或错误）"
fi

echo ""
echo "--- 检查错误码规范 ---"

# 测试 404 错误码
RESP_404=$(curl -s -X POST "$API_BASE/api/workflow/detail" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer test-token" \
    -d '{"workflow_id": "non-existent-id"}' 2>/dev/null || echo '{}')

if echo "$RESP_404" | jq -e '.code' > /dev/null 2>&1; then
    ERROR_CODE=$(echo "$RESP_404" | jq '.code')
    if [ "$ERROR_CODE" == "40401" ] || [ "$ERROR_CODE" == "404" ]; then
        echo "[PASS] 404 错误码符合规范 (code: $ERROR_CODE)"
        ((PASSED++))
    else
        echo "[INFO] 404 错误码: $ERROR_CODE"
    fi
fi

echo ""
echo "--- 检查版本冲突处理 ---"

# 测试 409 版本冲突
RESP_409=$(curl -s -X POST "$API_BASE/api/workflow/transition" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer test-token" \
    -d '{"workflow_id": "test", "version": -1, "event": "approve"}' 2>/dev/null || echo '{}')

if echo "$RESP_409" | jq -e '.code' > /dev/null 2>&1; then
    ERROR_CODE=$(echo "$RESP_409" | jq '.code')
    echo "[INFO] 版本冲突响应码: $ERROR_CODE"
fi

echo ""
echo "=== 验证结果 ==="
echo "通过: $PASSED"
echo "失败: $FAILED"

if [ $FAILED -gt 0 ]; then
    echo ""
    echo "部分 API 契约验证失败，请检查接口实现"
    exit 1
else
    echo ""
    echo "API 契约验证通过！"
    exit 0
fi
