#!/bin/bash

# WWJ Cloud 模块测试脚本
# 测试4个核心模块的API接口

BASE_URL="http://localhost:3000"
ADMIN_TOKEN=""
MEMBER_TOKEN=""

echo "🚀 开始测试WWJ Cloud核心模块..."

# 颜色输出函数
print_success() {
    echo -e "\033[32m✅ $1\033[0m"
}

print_error() {
    echo -e "\033[31m❌ $1\033[0m"
}

print_info() {
    echo -e "\033[34mℹ️  $1\033[0m"
}

# 测试基础连接
test_connection() {
    print_info "测试应用连接..."
    
    response=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL")
    if [ "$response" = "200" ]; then
        print_success "应用连接成功"
    else
        print_error "应用连接失败: HTTP $response"
        exit 1
    fi
}

# 测试Swagger文档
test_swagger() {
    print_info "测试Swagger文档..."
    
    # 测试主API文档
    response=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/api")
    if [ "$response" = "200" ]; then
        print_success "主API文档可访问"
    else
        print_error "主API文档访问失败: HTTP $response"
    fi
    
    # 测试管理API文档
    response=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/api/admin")
    if [ "$response" = "200" ]; then
        print_success "管理API文档可访问"
    else
        print_error "管理API文档访问失败: HTTP $response"
    fi
}

# 测试Admin模块
test_admin_module() {
    print_info "测试Admin模块..."
    
    # 创建测试管理员
    response=$(curl -s -X POST "$BASE_URL/adminapi/admin" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "testadmin",
            "password": "123456",
            "real_name": "测试管理员",
            "status": 1,
            "site_id": 0
        }')
    
    if echo "$response" | grep -q "uid"; then
        print_success "创建管理员成功"
    else
        print_error "创建管理员失败: $response"
    fi
    
    # 获取管理员列表
    response=$(curl -s "$BASE_URL/adminapi/admin?page=1&limit=10")
    if echo "$response" | grep -q "data"; then
        print_success "获取管理员列表成功"
    else
        print_error "获取管理员列表失败: $response"
    fi
}

# 测试Member模块
test_member_module() {
    print_info "测试Member模块..."
    
    # 创建测试会员
    response=$(curl -s -X POST "$BASE_URL/adminapi/member" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "testmember",
            "password": "123456",
            "nickname": "测试会员",
            "mobile": "13800138000",
            "email": "test@example.com",
            "status": 1,
            "site_id": 0
        }')
    
    if echo "$response" | grep -q "member_id"; then
        print_success "创建会员成功"
    else
        print_error "创建会员失败: $response"
    fi
    
    # 获取会员列表
    response=$(curl -s "$BASE_URL/adminapi/member?page=1&limit=10")
    if echo "$response" | grep -q "data"; then
        print_success "获取会员列表成功"
    else
        print_error "获取会员列表失败: $response"
    fi
}

# 测试RBAC模块
test_rbac_module() {
    print_info "测试RBAC模块..."
    
    # 创建测试角色
    response=$(curl -s -X POST "$BASE_URL/adminapi/role" \
        -H "Content-Type: application/json" \
        -d '{
            "roleName": "测试角色",
            "roleDesc": "测试角色描述",
            "status": 1,
            "appType": "admin"
        }')
    
    if echo "$response" | grep -q "roleId"; then
        print_success "创建角色成功"
    else
        print_error "创建角色失败: $response"
    fi
    
    # 创建测试菜单
    response=$(curl -s -X POST "$BASE_URL/adminapi/menu" \
        -H "Content-Type: application/json" \
        -d '{
            "menuName": "测试菜单",
            "menuType": 1,
            "status": 1,
            "appType": "admin",
            "path": "/test",
            "sort": 1
        }')
    
    if echo "$response" | grep -q "menuId"; then
        print_success "创建菜单成功"
    else
        print_error "创建菜单失败: $response"
    fi
}

# 测试Auth模块
test_auth_module() {
    print_info "测试Auth模块..."
    
    # 测试管理员登录
    response=$(curl -s -X POST "$BASE_URL/auth/admin/login" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "admin",
            "password": "123456",
            "siteId": 0
        }')
    
    if echo "$response" | grep -q "accessToken"; then
        print_success "管理员登录成功"
        # 提取token用于后续测试
        ADMIN_TOKEN=$(echo "$response" | grep -o '"accessToken":"[^"]*"' | cut -d'"' -f4)
    else
        print_error "管理员登录失败: $response"
    fi
    
    # 测试会员登录
    response=$(curl -s -X POST "$BASE_URL/auth/member/login" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "member",
            "password": "123456",
            "siteId": 0
        }')
    
    if echo "$response" | grep -q "accessToken"; then
        print_success "会员登录成功"
        # 提取token用于后续测试
        MEMBER_TOKEN=$(echo "$response" | grep -o '"accessToken":"[^"]*"' | cut -d'"' -f4)
    else
        print_error "会员登录失败: $response"
    fi
}

# 测试带认证的接口
test_authenticated_apis() {
    print_info "测试需要认证的接口..."
    
    if [ -n "$ADMIN_TOKEN" ]; then
        # 测试获取管理员统计信息
        response=$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" \
            "$BASE_URL/adminapi/admin/stats/overview")
        
        if echo "$response" | grep -q "total"; then
            print_success "获取管理员统计信息成功"
        else
            print_error "获取管理员统计信息失败: $response"
        fi
    fi
    
    if [ -n "$MEMBER_TOKEN" ]; then
        # 测试获取会员信息
        response=$(curl -s -H "Authorization: Bearer $MEMBER_TOKEN" \
            "$BASE_URL/auth/profile")
        
        if echo "$response" | grep -q "userId"; then
            print_success "获取会员信息成功"
        else
            print_error "获取会员信息失败: $response"
        fi
    fi
}

# 主测试流程
main() {
    echo "=========================================="
    echo "WWJ Cloud 核心模块测试"
    echo "=========================================="
    
    test_connection
    test_swagger
    test_admin_module
    test_member_module
    test_rbac_module
    test_auth_module
    test_authenticated_apis
    
    echo "=========================================="
    print_success "所有模块测试完成！"
    echo "=========================================="
}

# 运行测试
main 