#!/bin/bash
# 快速测试脚本 - 在本地环境快速验证核心功能

set -e

echo "🚀 开始快速测试 Blockchain Service..."
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

BASE_URL="http://localhost:3012"

# 测试结果统计
PASS=0
FAIL=0

# 测试函数
test_api() {
    local test_name=$1
    local method=$2
    local endpoint=$3
    local data=$4
    local expected_status=$5

    echo -n "Testing: $test_name ... "

    if [ -n "$data" ]; then
        response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
            -H "Content-Type: application/json" \
            -d "$data")
    else
        response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
            -H "Content-Type: application/json")
    fi

    status=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n-1)

    if [ "$status" -eq "$expected_status" ]; then
        echo -e "${GREEN}✓ PASS${NC}"
        PASS=$((PASS + 1))
        if command -v jq &> /dev/null && [ -n "$body" ]; then
            echo "$body" | jq '.' 2>/dev/null || echo "$body"
        else
            echo "$body"
        fi
    else
        echo -e "${RED}✗ FAIL${NC} (Expected: $expected_status, Got: $status)"
        FAIL=$((FAIL + 1))
        echo "$body"
    fi
    echo ""
}

# 1. 健康检查
echo -e "${YELLOW}=== 1. 健康检查 ===${NC}"
test_api "Health Check" "GET" "/health" "" 200
test_api "Ready Check" "GET" "/health/ready" "" 200

# 2. 余额查询测试
echo -e "${YELLOW}=== 2. 余额查询 ===${NC}"
# 使用一个已知的测试地址 (Binance Hot Wallet)
TEST_ADDRESS="0x8894E0a0c962CB723c1976a4421c95949bE2D4E3"

test_api "Query KAVA Balance" "GET" "/balance?chainType=KAVA&address=$TEST_ADDRESS" "" 200
test_api "Query Multi-Chain Balance" "GET" "/balance/multi-chain?address=$TEST_ADDRESS" "" 200

# 3. 地址派生测试
echo -e "${YELLOW}=== 3. 地址派生测试 ===${NC}"
# 测试用压缩公钥 (仅用于测试)
TEST_PUBLIC_KEY="0x02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737"
TEST_USER_ID="999999"

test_api "Derive Address" "POST" "/internal/derive-address" \
    "{\"userId\": \"$TEST_USER_ID\", \"publicKey\": \"$TEST_PUBLIC_KEY\"}" \
    201

# 4. 获取用户地址
echo -e "${YELLOW}=== 4. 获取用户地址 ===${NC}"
test_api "Get User Addresses" "GET" "/internal/user/$TEST_USER_ID/addresses" "" 200

# 5. 错误场景测试
echo -e "${YELLOW}=== 5. 错误场景测试 ===${NC}"

# 无效地址格式
test_api "Invalid Address Format" "GET" "/balance?chainType=KAVA&address=invalid" "" 400

# 无效链类型
test_api "Invalid Chain Type" "GET" "/balance?chainType=INVALID&address=$TEST_ADDRESS" "" 400

# 无效公钥格式
test_api "Invalid Public Key" "POST" "/internal/derive-address" \
    "{\"userId\": \"1\", \"publicKey\": \"invalid\"}" \
    400

# 6. API 文档测试
echo -e "${YELLOW}=== 6. API 文档 ===${NC}"
test_api "Swagger API Docs" "GET" "/api" "" 200

# 总结
echo ""
echo -e "${YELLOW}======================================${NC}"
echo -e "${YELLOW}测试完成！${NC}"
echo -e "${GREEN}通过: $PASS${NC}"
echo -e "${RED}失败: $FAIL${NC}"
echo -e "${YELLOW}======================================${NC}"

if [ $FAIL -eq 0 ]; then
    echo -e "${GREEN}✓ 所有测试通过！${NC}"
    exit 0
else
    echo -e "${RED}✗ 存在失败的测试！${NC}"
    exit 1
fi
