#!/bin/bash
# =============================================
# 充值功能测试脚本
# =============================================

BASE_URL="http://localhost:5010"
TOKEN=""

echo "=========================================="
echo "Monisuo 充值功能测试"
echo "=========================================="
echo ""

# 1. 测试健康检查
echo "【1】测试后端服务健康检查..."
curl -s "$BASE_URL/health" | jq . 2>/dev/null || curl -s "$BASE_URL/health"
echo ""
echo ""

# 2. 登录获取Token
echo "【2】登录获取Token..."
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/user/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "testuser",
    "password": "test123456"
  }')

echo "$LOGIN_RESPONSE" | jq . 2>/dev/null || echo "$LOGIN_RESPONSE"
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.data.token' 2>/dev/null)

if [ -z "$TOKEN" ] || [ "$TOKEN" = "null" ]; then
  echo "❌ 登录失败，尝试注册新用户..."
  
  REGISTER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/user/register" \
    -H "Content-Type: application/json" \
    -d '{
      "username": "testuser",
      "password": "test123456",
      "phone": "13800138000"
    }')
  
  echo "$REGISTER_RESPONSE" | jq . 2>/dev/null || echo "$REGISTER_RESPONSE"
  TOKEN=$(echo "$REGISTER_RESPONSE" | jq -r '.data.token' 2>/dev/null)
fi

if [ -z "$TOKEN" ] || [ "$TOKEN" = "null" ]; then
  echo "❌ 无法获取Token，测试终止"
  exit 1
fi

echo "✅ Token获取成功: ${TOKEN:0:20}..."
echo ""

# 3. 获取默认钱包地址
echo "【3】获取默认钱包地址..."
WALLET_RESPONSE=$(curl -s -X GET "$BASE_URL/api/wallet/default" \
  -H "Authorization: Bearer $TOKEN")

echo "$WALLET_RESPONSE" | jq . 2>/dev/null || echo "$WALLET_RESPONSE"

WALLET_ADDRESS=$(echo "$WALLET_RESPONSE" | jq -r '.data.address' 2>/dev/null)
if [ -z "$WALLET_ADDRESS" ] || [ "$WALLET_ADDRESS" = "null" ]; then
  echo "❌ 钱包地址获取失败，请检查数据库中是否有冷钱包数据"
  echo "提示：请执行 sql/patch_cold_wallet.sql 脚本"
fi
echo ""

# 4. 申请充值
echo "【4】申请充值 100 USDT..."
DEPOSIT_RESPONSE=$(curl -s -X POST "$BASE_URL/api/fund/deposit" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "amount": "100",
    "remark": "测试充值"
  }')

echo "$DEPOSIT_RESPONSE" | jq . 2>/dev/null || echo "$DEPOSIT_RESPONSE"

ORDER_NO=$(echo "$DEPOSIT_RESPONSE" | jq -r '.data.orderNo' 2>/dev/null)
if [ -z "$ORDER_NO" ] || [ "$ORDER_NO" = "null" ]; then
  echo "❌ 充值申请失败"
else
  echo "✅ 充值申请成功，订单号: $ORDER_NO"
fi
echo ""

# 5. 确认打款
if [ -n "$ORDER_NO" ] && [ "$ORDER_NO" != "null" ]; then
  echo "【5】确认已打款..."
  CONFIRM_RESPONSE=$(curl -s -X POST "$BASE_URL/api/fund/confirmPay" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $TOKEN" \
    -d "{
      \"orderNo\": \"$ORDER_NO\"
    }")
  
  echo "$CONFIRM_RESPONSE" | jq . 2>/dev/null || echo "$CONFIRM_RESPONSE"
  echo ""
fi

# 6. 查看充提记录
echo "【6】查看充提记录..."
ORDERS_RESPONSE=$(curl -s -X GET "$BASE_URL/api/fund/orders?pageNum=1&pageSize=10" \
  -H "Authorization: Bearer $TOKEN")

echo "$ORDERS_RESPONSE" | jq . 2>/dev/null || echo "$ORDERS_RESPONSE"
echo ""

echo "=========================================="
echo "测试完成！"
echo "=========================================="
