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

set -e

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

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

BASE_URL="http://localhost:3000/api/v1"

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

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

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

    if [ -n "$token" ]; then
        response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer $token" \
            -d "$data")
    else
        response=$(curl -s -w "\n%{http_code}" -X $method "$BASE_URL$endpoint" \
            -H "Content-Type: application/json" \
            -d "$data")
    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))
        echo "$body"
    else
        echo -e "${RED}✗ FAIL${NC} (Expected: $expected_status, Got: $status)"
        FAIL=$((FAIL + 1))
        echo "$body"
    fi
    echo ""
}

# 生成随机设备 ID
DEVICE_ID="test-device-$(date +%s)"

# 1. 测试账户创建
echo -e "${YELLOW}=== 1. 账户创建 ===${NC}"
CREATE_RESPONSE=$(curl -s -X POST "$BASE_URL/user/auto-create" \
    -H "Content-Type: application/json" \
    -d "{
        \"deviceId\": \"$DEVICE_ID\",
        \"deviceName\": \"Test Device\",
        \"provinceCode\": \"110000\",
        \"cityCode\": \"110100\"
    }")

echo "$CREATE_RESPONSE" | jq '.'

# 提取关键信息
ACCESS_TOKEN=$(echo "$CREATE_RESPONSE" | jq -r '.data.accessToken')
REFRESH_TOKEN=$(echo "$CREATE_RESPONSE" | jq -r '.data.refreshToken')
ACCOUNT_SEQUENCE=$(echo "$CREATE_RESPONSE" | jq -r '.data.accountSequence')
MNEMONIC=$(echo "$CREATE_RESPONSE" | jq -r '.data.mnemonic')
REFERRAL_CODE=$(echo "$CREATE_RESPONSE" | jq -r '.data.referralCode')

if [ "$ACCESS_TOKEN" != "null" ]; then
    echo -e "${GREEN}✓ 账户创建成功${NC}"
    echo "Account Sequence: $ACCOUNT_SEQUENCE"
    echo "Referral Code: $REFERRAL_CODE"
    PASS=$((PASS + 1))
else
    echo -e "${RED}✗ 账户创建失败${NC}"
    FAIL=$((FAIL + 1))
    exit 1
fi
echo ""

# 2. 测试获取个人资料
echo -e "${YELLOW}=== 2. 获取个人资料 ===${NC}"
test_api "获取个人资料" "GET" "/user/my-profile" "" 200 "$ACCESS_TOKEN"

# 3. 测试更新个人资料
echo -e "${YELLOW}=== 3. 更新个人资料 ===${NC}"
test_api "更新个人资料" "PUT" "/user/update-profile" \
    "{\"nickname\": \"测试用户\", \"address\": \"测试地址\"}" \
    200 "$ACCESS_TOKEN"

# 4. 测试获取设备列表
echo -e "${YELLOW}=== 4. 获取设备列表 ===${NC}"
test_api "获取设备列表" "GET" "/user/my-devices" "" 200 "$ACCESS_TOKEN"

# 5. 测试 Token 刷新
echo -e "${YELLOW}=== 5. Token 刷新 ===${NC}"
REFRESH_RESPONSE=$(curl -s -X POST "$BASE_URL/user/auto-login" \
    -H "Content-Type: application/json" \
    -d "{
        \"refreshToken\": \"$REFRESH_TOKEN\",
        \"deviceId\": \"$DEVICE_ID\"
    }")

NEW_ACCESS_TOKEN=$(echo "$REFRESH_RESPONSE" | jq -r '.data.accessToken')
if [ "$NEW_ACCESS_TOKEN" != "null" ] && [ "$NEW_ACCESS_TOKEN" != "$ACCESS_TOKEN" ]; then
    echo -e "${GREEN}✓ Token 刷新成功${NC}"
    PASS=$((PASS + 1))
else
    echo -e "${RED}✗ Token 刷新失败${NC}"
    FAIL=$((FAIL + 1))
fi
echo ""

# 6. 测试助记词恢复
echo -e "${YELLOW}=== 6. 助记词恢复 ===${NC}"
NEW_DEVICE_ID="test-device-recovery-$(date +%s)"
test_api "助记词恢复" "POST" "/user/recover-by-mnemonic" \
    "{
        \"accountSequence\": $ACCOUNT_SEQUENCE,
        \"mnemonic\": \"$MNEMONIC\",
        \"newDeviceId\": \"$NEW_DEVICE_ID\",
        \"deviceName\": \"Recovered Device\"
    }" \
    201 ""

# 7. 测试推荐码查询
echo -e "${YELLOW}=== 7. 推荐码查询 ===${NC}"
test_api "推荐码查询" "GET" "/user/by-referral-code/$REFERRAL_CODE" "" 200 ""

# 8. 测试 KYC 提交
echo -e "${YELLOW}=== 8. KYC 提交 ===${NC}"
test_api "KYC 提交" "POST" "/user/submit-kyc" \
    "{
        \"realName\": \"张三\",
        \"idCardNumber\": \"110101199001011234\",
        \"idCardFrontUrl\": \"https://example.com/front.jpg\",
        \"idCardBackUrl\": \"https://example.com/back.jpg\"
    }" \
    201 "$ACCESS_TOKEN"

# 9. 测试错误场景
echo -e "${YELLOW}=== 9. 错误场景测试 ===${NC}"
test_api "无效 Token" "GET" "/user/my-profile" "" 401 "invalid-token"
test_api "错误的助记词" "POST" "/user/recover-by-mnemonic" \
    "{
        \"accountSequence\": $ACCOUNT_SEQUENCE,
        \"mnemonic\": \"wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong\",
        \"newDeviceId\": \"wrong-device\",
        \"deviceName\": \"Wrong Device\"
    }" \
    400 ""

# 总结
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
