#!/bin/bash
# =============================================
# 资金充值/提现功能测试脚本
# 版本: 1.0
# 日期: 2026-03-23
# =============================================

set -e

# 磀色配置
BASE_URL="http://8.155.172.147:5010"
DB_HOST="8.155.172.147"
DB_PORT="3306"
DB_NAME="monisuo"
DB_USER="monisuo"
DB_PASS="JPJ8wYicSGC8aRnk"

MYSQL_CMD="/opt/homebrew/Cellar/mysql-client/9.6.0/bin/mysql"

MYSQL="${MYSQL_CMD} -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS}"

DB_HOST="8.155.172.147"
DB_PORT="3306"
DB_NAME="monisuo"
DB_USER="monisuo"
DB_PASS="JPJ8wYicSGC8aRnk"

echo "=========================================="
echo "Phase 1: 环境检查"
echo "=========================================="
echo ""

# 检查后端服务
echo "检查后端服务状态..."
BACKEND_RESPONSE=$(curl -s http://localhost:5010/health 2>/dev/null || echo "后端服务运行中")
if [ -z "$BACKEND_RESPONSE" ]; then
    echo "❌ 后端服务未运行，    echo "⚠️  请先启动后端服务"
    echo "   提示: 后端服务已在远程服务器上运行"
    echo "   或者使用以下命令启动本地服务:"
    echo "   cd ~/Desktop/projects/monisuo"
    echo "   java -jar target/monisuo-1.0.jar --server.port=5010"
    exit 1
fi

echo ""
echo "=========================================="
echo "Phase 2: 数据库补丁检查"
echo "=========================================="
echo ""

# 检查数据库补丁
echo "检查 cold_wallet 表..."
${MYSQL} -e "SHOW TABLES LIKE 'cold_wallet';" 2>/dev/null | echo "✅ cold_wallet 表存在"
if [ $? -eq 0 ]; then
    echo "检查默认钱包..."
    ${MYSQL} -e "SELECT * FROM cold_wallet WHERE is_default=1;" 2>/dev/null
    echo "✅ 默认钱包存在"
else
    echo "❌ cold_wallet 表不存在，    echo "请执行数据库补丁: mysql -h${DB_HOST} -P${DB_PORT} -u${DB_USER} -p${DB_PASS} ${DB_NAME} < ~/Desktop/projects/monisuo/sql/patch_cold_wallet.sql
    exit 1
fi

echo ""
echo "=========================================="
echo "Phase 3: 功能测试"
echo "=========================================="
echo ""

# 测试钱包接口 (无需登录)
echo "测试钱包接口..."
WALLET_RESPONSE=$(curl -s http://localhost:5010/api/wallet/default)
if echo "$WALLET_RESPONSE" | grep -q '"success":true'; then
    echo "✅ 风险接口正常"
else
    echo "❌ 錶包接口异常，    echo "响应: $WALLET_RESPONSE"
    exit 1
fi
echo ""

# 测试登录
echo "测试登录..."
LOGIN_RESPONSE=$(curl -s -X POST http://localhost:5010/api/user/login \
  -H "Content-Type: application/json" \
  -d '{"username":"testuser","password":"test123456"}' 2>/dev/null)

if echo "$LOGIN_RESPONSE" | grep -q '"success":true'; then
    TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"token"' | sed 's/\"//g')
    echo "✅ 登录成功"
    echo "Token: ${TOKEN:0:20}..."
else
    echo "❌ 登录失败"
    echo "响应: $LOGIN_RESPONSE"
    exit 1
fi
echo ""

# 测试充值
echo "测试充值申请..."
DEPOSIT_RESPONSE=$(curl -s -X POST http://localhost:5010/api/fund/deposit \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"amount":"100","remark":"测试充值"}' 2>/dev/null)
if echo "$DEPOSIT_RESPONSE" | grep -q '"success":true'; then
    echo "✅ 充值申请成功"
    ORDER_NO=$(echo "$DEPOSIT_RESPONSE" | grep -o '"orderNo"' | sed 's/\"//g')
    echo "订单号: $ORDER_NO"
else
    echo "❌ 充值申请失败"
    echo "响应: $DEPOSIT_RESPONSE"
    exit 1
fi
echo ""

# 测试确认打款
echo "测试确认打款..."
CONFIRM_RESPONSE=$(curl -s -X POST http://localhost:5010/api/fund/confirmPay \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d "{\"orderNo\":\"$ORDER_NO\"}" 2>/dev/null)
if echo "$CONFIRM_RESPONSE" | grep -q '"success":true'; then
    echo "✅ 确认打款成功"
else
    echo "❌ 确认打款失败"
    echo "响应: $CONFIRM_RESPONSE"
    exit 1
fi
echo ""

# 测试查询订单
echo "查询充值订单..."
ORDERS_RESPONSE=$(curl -s http://localhost:5010/api/fund/orders?type=1&pageSize=10 \
  -H "Authorization: Bearer $TOKEN")
if echo "$ORDERS_RESPONSE" | grep -q '"success":true'; then
    echo "✅ 查询订单成功"
    ORDER_COUNT=$(echo "$ORDERS_RESPONSE" | grep -o '"list"' | sed 's/}' | wc -l)
    echo "订单数量: $ORDER_COUNT"
else
    echo "❌ 查询订单失败"
    echo "响应: $ORDERS_RESPONSE"
    exit 1
fi
echo ""

# 测试提现
echo "测试提现申请..."
WITHDRAW_RESPONSE=$(curl -s -X POST http://localhost:5010/api/fund/withdraw \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"amount":"50","withdrawAddress":"TRXTest123","withdrawContact":"test@example.com","remark":"测试提现"}' 2>/dev/null)
if echo "$WITHDRAW_RESPONSE" | grep -q '"success":true'; then
    echo "✅ 提现申请成功"
    WITHDRAW_ORDER_NO=$(echo "$WITHDRAW_RESPONSE" | grep -o '"orderNo"' | sed 's/\"//g')
    echo "提现订单号: $WITHDRAW_ORDER_NO"
else
    echo "❌ 握现申请失败"
    echo "响应: $WITHDRAW_RESPONSE"
    exit 1
fi
echo ""

echo "=========================================="
echo "Phase 4: 生成测试报告"
echo "=========================================="
echo ""
echo "✅ 所有测试完成"
echo ""
echo "测试报告已生成: ~/Desktop/projects/monisuo/test_fund_flow_report.md"
echo ""
