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

$BaseUrl = "http://localhost:3000"
$AdminToken = ""
$MemberToken = ""

Write-Host "🚀 开始测试WWJ Cloud核心模块..." -ForegroundColor Cyan

# 颜色输出函数
function Write-Success {
    param([string]$Message)
    Write-Host "✅ $Message" -ForegroundColor Green
}

function Write-Error {
    param([string]$Message)
    Write-Host "❌ $Message" -ForegroundColor Red
}

function Write-Info {
    param([string]$Message)
    Write-Host "ℹ️  $Message" -ForegroundColor Blue
}

# 测试基础连接
function Test-Connection {
    Write-Info "测试应用连接..."
    
    try {
        $response = Invoke-WebRequest -Uri $BaseUrl -Method GET -UseBasicParsing
        if ($response.StatusCode -eq 200) {
            Write-Success "应用连接成功"
        } else {
            Write-Error "应用连接失败: HTTP $($response.StatusCode)"
            exit 1
        }
    } catch {
        Write-Error "应用连接失败: $($_.Exception.Message)"
        exit 1
    }
}

# 测试Swagger文档
function Test-Swagger {
    Write-Info "测试Swagger文档..."
    
    # 测试主API文档
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/api" -Method GET -UseBasicParsing
        if ($response.StatusCode -eq 200) {
            Write-Success "主API文档可访问"
        } else {
            Write-Error "主API文档访问失败: HTTP $($response.StatusCode)"
        }
    } catch {
        Write-Error "主API文档访问失败: $($_.Exception.Message)"
    }
    
    # 测试管理API文档
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/api/admin" -Method GET -UseBasicParsing
        if ($response.StatusCode -eq 200) {
            Write-Success "管理API文档可访问"
        } else {
            Write-Error "管理API文档访问失败: HTTP $($response.StatusCode)"
        }
    } catch {
        Write-Error "管理API文档访问失败: $($_.Exception.Message)"
    }
}

# 测试Admin模块
function Test-AdminModule {
    Write-Info "测试Admin模块..."
    
    # 创建测试管理员
    $adminData = @{
        username = "testadmin"
        password = "123456"
        real_name = "测试管理员"
        status = 1
        site_id = 0
    } | ConvertTo-Json
    
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/admin" -Method POST -Body $adminData -ContentType "application/json" -UseBasicParsing
        if ($response.Content -match "uid") {
            Write-Success "创建管理员成功"
        } else {
            Write-Error "创建管理员失败: $($response.Content)"
        }
    } catch {
        Write-Error "创建管理员失败: $($_.Exception.Message)"
    }
    
    # 获取管理员列表
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/admin?page=1&limit=10" -Method GET -UseBasicParsing
        if ($response.Content -match "data") {
            Write-Success "获取管理员列表成功"
        } else {
            Write-Error "获取管理员列表失败: $($response.Content)"
        }
    } catch {
        Write-Error "获取管理员列表失败: $($_.Exception.Message)"
    }
}

# 测试Member模块
function Test-MemberModule {
    Write-Info "测试Member模块..."
    
    # 创建测试会员
    $memberData = @{
        username = "testmember"
        password = "123456"
        nickname = "测试会员"
        mobile = "13800138000"
        email = "test@example.com"
        status = 1
        site_id = 0
    } | ConvertTo-Json
    
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/member" -Method POST -Body $memberData -ContentType "application/json" -UseBasicParsing
        if ($response.Content -match "member_id") {
            Write-Success "创建会员成功"
        } else {
            Write-Error "创建会员失败: $($response.Content)"
        }
    } catch {
        Write-Error "创建会员失败: $($_.Exception.Message)"
    }
    
    # 获取会员列表
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/member?page=1&limit=10" -Method GET -UseBasicParsing
        if ($response.Content -match "data") {
            Write-Success "获取会员列表成功"
        } else {
            Write-Error "获取会员列表失败: $($response.Content)"
        }
    } catch {
        Write-Error "获取会员列表失败: $($_.Exception.Message)"
    }
}

# 测试RBAC模块
function Test-RbacModule {
    Write-Info "测试RBAC模块..."
    
    # 创建测试角色
    $roleData = @{
        roleName = "测试角色"
        roleDesc = "测试角色描述"
        status = 1
        appType = "admin"
    } | ConvertTo-Json
    
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/role" -Method POST -Body $roleData -ContentType "application/json" -UseBasicParsing
        if ($response.Content -match "roleId") {
            Write-Success "创建角色成功"
        } else {
            Write-Error "创建角色失败: $($response.Content)"
        }
    } catch {
        Write-Error "创建角色失败: $($_.Exception.Message)"
    }
    
    # 创建测试菜单
    $menuData = @{
        menuName = "测试菜单"
        menuType = 1
        status = 1
        appType = "admin"
        path = "/test"
        sort = 1
    } | ConvertTo-Json
    
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/menu" -Method POST -Body $menuData -ContentType "application/json" -UseBasicParsing
        if ($response.Content -match "menuId") {
            Write-Success "创建菜单成功"
        } else {
            Write-Error "创建菜单失败: $($response.Content)"
        }
    } catch {
        Write-Error "创建菜单失败: $($_.Exception.Message)"
    }
}

# 测试Auth模块
function Test-AuthModule {
    Write-Info "测试Auth模块..."
    
    # 测试管理员登录
    $adminLoginData = @{
        username = "admin"
        password = "123456"
        siteId = 0
    } | ConvertTo-Json
    
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/auth/admin/login" -Method POST -Body $adminLoginData -ContentType "application/json" -UseBasicParsing
        if ($response.Content -match "accessToken") {
            Write-Success "管理员登录成功"
            # 提取token用于后续测试
            $script:AdminToken = ($response.Content | ConvertFrom-Json).accessToken
        } else {
            Write-Error "管理员登录失败: $($response.Content)"
        }
    } catch {
        Write-Error "管理员登录失败: $($_.Exception.Message)"
    }
    
    # 测试会员登录
    $memberLoginData = @{
        username = "member"
        password = "123456"
        siteId = 0
    } | ConvertTo-Json
    
    try {
        $response = Invoke-WebRequest -Uri "$BaseUrl/auth/member/login" -Method POST -Body $memberLoginData -ContentType "application/json" -UseBasicParsing
        if ($response.Content -match "accessToken") {
            Write-Success "会员登录成功"
            # 提取token用于后续测试
            $script:MemberToken = ($response.Content | ConvertFrom-Json).accessToken
        } else {
            Write-Error "会员登录失败: $($response.Content)"
        }
    } catch {
        Write-Error "会员登录失败: $($_.Exception.Message)"
    }
}

# 测试带认证的接口
function Test-AuthenticatedApis {
    Write-Info "测试需要认证的接口..."
    
    if ($AdminToken) {
        # 测试获取管理员统计信息
        $headers = @{
            "Authorization" = "Bearer $AdminToken"
        }
        
        try {
            $response = Invoke-WebRequest -Uri "$BaseUrl/adminapi/admin/stats/overview" -Method GET -Headers $headers -UseBasicParsing
            if ($response.Content -match "total") {
                Write-Success "获取管理员统计信息成功"
            } else {
                Write-Error "获取管理员统计信息失败: $($response.Content)"
            }
        } catch {
            Write-Error "获取管理员统计信息失败: $($_.Exception.Message)"
        }
    }
    
    if ($MemberToken) {
        # 测试获取会员信息
        $headers = @{
            "Authorization" = "Bearer $MemberToken"
        }
        
        try {
            $response = Invoke-WebRequest -Uri "$BaseUrl/auth/profile" -Method GET -Headers $headers -UseBasicParsing
            if ($response.Content -match "userId") {
                Write-Success "获取会员信息成功"
            } else {
                Write-Error "获取会员信息失败: $($response.Content)"
            }
        } catch {
            Write-Error "获取会员信息失败: $($_.Exception.Message)"
        }
    }
}

# 主测试流程
function Main {
    Write-Host "==========================================" -ForegroundColor Yellow
    Write-Host "WWJ Cloud 核心模块测试" -ForegroundColor Yellow
    Write-Host "==========================================" -ForegroundColor Yellow
    
    Test-Connection
    Test-Swagger
    Test-AdminModule
    Test-MemberModule
    Test-RbacModule
    Test-AuthModule
    Test-AuthenticatedApis
    
    Write-Host "==========================================" -ForegroundColor Yellow
    Write-Success "所有模块测试完成！"
    Write-Host "==========================================" -ForegroundColor Yellow
}

# 运行测试
Main 