#!/bin/bash

BASE_URL="http://localhost:5010"

echo "================================"
echo "测试资产API接口"
echo "================================"

# 1. 用户登录
echo -e "\n步骤1: 用户登录..."
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/user/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"abcd","password":"abcd123"}')

echo "登录响应: $LOGIN_RESPONSE"
TOKEN=$(echo $LOGIN_RESPONSE | python3 -c "
import sys, json
data = json.load(sys.stdin)
if data.get('success'):
    print(data['data']['token'])
" 2>/dev/null)

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

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

# 2. 测试资产总览接口
echo -e "\n================================"
echo "测试 /api/asset/overview 接口"
echo "================================"
OVERVIEW_RESPONSE=$(curl -s "$BASE_URL/api/asset/overview" \
  -H "Authorization: Bearer $TOKEN")

echo "$OVERVIEW_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$OVERVIEW_RESPONSE"

# 分析字段
echo -e "\n字段分析:"
echo "$OVERVIEW_RESPONSE" | python3 -c "
import sys, json
data = json.load(sys.stdin)
if data.get('success') and data.get('data'):
    print('后端返回的字段:')
    for key in data['data'].keys():
        print(f'  - {key}: {type(data[\"data\"][key]).__name__}')
    print('\nFlutter期望的字段:')
    print('  - totalAsset (总资产)')
    print('  - fundBalance (资金余额)')
    print('  - tradeBalance (交易余额)')
    print('  - totalProfit (总盈亏)')
"

# 3. 测试资金账户接口
echo -e "\n================================"
echo "测试 /api/asset/fund 接口"
echo "================================"
FUND_RESPONSE=$(curl -s "$BASE_URL/api/asset/fund" \
  -H "Authorization: Bearer $TOKEN")

echo "$FUND_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$FUND_RESPONSE"

# 4. 测试交易账户接口
echo -e "\n================================"
echo "测试 /api/asset/trade 接口"
echo "================================"
TRADE_RESPONSE=$(curl -s "$BASE_URL/api/asset/trade" \
  -H "Authorization: Bearer $TOKEN")

echo "$TRADE_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$TRADE_RESPONSE"

echo -e "\n================================"
echo "问题诊断"
echo "================================"
echo "$OVERVIEW_RESPONSE" | python3 -c "
import sys, json
data = json.load(sys.stdin)
if data.get('success') and data.get('data'):
    backend_fields = set(data['data'].keys())
    flutter_expected = {'totalAsset', 'fundBalance', 'tradeBalance', 'totalProfit'}
    
    print('后端返回字段:', sorted(backend_fields))
    print('Flutter期望字段:', sorted(flutter_expected))
    print()
    
    missing = flutter_expected - backend_fields
    extra = backend_fields - flutter_expected
    
    if missing:
        print('❌ 缺失字段:', sorted(missing))
    if extra:
        print('⚠️  额外字段:', sorted(extra))
    
    # 检查字段名称不匹配
    if 'totalAssets' in backend_fields and 'totalAsset' in flutter_expected:
        print('⚠️  字段名称不匹配: totalAssets vs totalAsset')
    if 'tradeValue' in backend_fields and 'tradeBalance' in flutter_expected:
        print('⚠️  字段名称不匹配: tradeValue vs tradeBalance')
    if 'totalProfit' not in backend_fields:
        print('⚠️  缺失字段: totalProfit')
"

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