#!/bin/bash

BASE_URL="http://localhost:5010"

echo "================================"
echo "充值审批测试"
echo "================================"

# 1. 管理员登录
echo -e "\n步骤1: 管理员登录..."
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/admin/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}')

echo "登录响应: $LOGIN_RESPONSE"
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败"
    exit 1
fi

echo "✅ 登录成功"
echo "Token: ${TOKEN:0:30}..."

# 2. 获取待审批订单
echo -e "\n步骤2: 获取待审批订单..."
PENDING_RESPONSE=$(curl -s "$BASE_URL/admin/order/pending?type=1&status=2" \
  -H "Authorization: Bearer $TOKEN")

echo "待审批订单:"
echo "$PENDING_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$PENDING_RESPONSE"

# 提取第一个订单号
ORDER_NO=$(echo $PENDING_RESPONSE | grep -o '"orderNo":"[^"]*' | head -1 | cut -d'"' -f4)

if [ -z "$ORDER_NO" ]; then
    echo "❌ 没有待审批的充值订单"
    echo "请先创建一个充值订单并确认打款"
    exit 1
fi

echo -e "\n选择的订单号: $ORDER_NO"

# 3. 查询订单详情（审批前）
echo -e "\n步骤3: 查询订单详情（审批前）..."
ORDER_DETAIL=$(curl -s "$BASE_URL/admin/order/list?orderNo=$ORDER_NO" \
  -H "Authorization: Bearer $TOKEN")

echo "订单详情:"
echo "$ORDER_DETAIL" | python3 -m json.tool 2>/dev/null || echo "$ORDER_DETAIL"

USER_ID=$(echo $ORDER_DETAIL | grep -o '"userId":[0-9]*' | head -1 | cut -d':' -f2)
AMOUNT=$(echo $ORDER_DETAIL | grep -o '"amount":[0-9.]*' | head -1 | cut -d':' -f2)

echo -e "\n用户ID: $USER_ID"
echo "充值金额: $AMOUNT"

# 4. 执行审批
echo -e "\n步骤4: 执行审批（通过）..."
echo "查看后端日志（新开终端）:"
echo "tail -f ~/Desktop/projects/monisuo/logs/app.log | grep -A30 'FundService.approve'"

read -p "按回车继续执行审批..."

APPROVE_RESPONSE=$(curl -s -X POST "$BASE_URL/admin/order/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d "{\"orderNo\":\"$ORDER_NO\",\"status\":2,\"adminRemark\":\"测试审批通过\"}")

echo -e "\n审批响应:"
echo "$APPROVE_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$APPROVE_RESPONSE"

# 5. 等待事务提交
echo -e "\n步骤5: 等待事务提交..."
sleep 3

# 6. 验证订单状态
echo -e "\n步骤6: 验证订单状态..."
ORDER_AFTER=$(curl -s "$BASE_URL/admin/order/list?orderNo=$ORDER_NO" \
  -H "Authorization: Bearer $TOKEN")

ORDER_STATUS=$(echo $ORDER_AFTER | grep -o '"status":[0-9]*' | head -1 | cut -d':' -f2)
echo "订单状态: $ORDER_STATUS (期望: 3)"

# 7. 查询用户余额（通过管理后台）
echo -e "\n步骤7: 查询用户资金账户..."
USER_DETAIL=$(curl -s "$BASE_URL/admin/user/detail?userId=$USER_ID" \
  -H "Authorization: Bearer $TOKEN")

echo "用户详情:"
echo "$USER_DETAIL" | python3 -m json.tool 2>/dev/null || echo "$USER_DETAIL"

BALANCE=$(echo $USER_DETAIL | grep -o '"balance":[0-9.]*' | head -1 | cut -d':' -f2)
TOTAL_DEPOSIT=$(echo $USER_DETAIL | grep -o '"totalDeposit":[0-9.]*' | head -1 | cut -d':' -f2)

echo -e "\n================================"
echo "验证结果"
echo "================================"
echo "订单状态: $ORDER_STATUS (期望: 3)"
echo "账户余额: $BALANCE"
echo "累计充值: $TOTAL_DEPOSIT"
echo "充值金额: $AMOUNT"

if [ "$ORDER_STATUS" == "3" ]; then
    echo "✅ 订单状态正确"
else
    echo "❌ 订单状态错误"
fi

echo -e "\n请检查后端日志确认余额是否更新:"
echo "tail -50 ~/Desktop/projects/monisuo/logs/app.log | grep -A20 '充值审批'"
