# Test login with correct path
$loginBody = @{
    tenantCode = "T202605253515"
    username = "admin"
    password = "FFuxUo5bC^ui"
} | ConvertTo-Json

Write-Output "=== Testing Login with /login ==="
try {
    $resp = Invoke-RestMethod -Uri "http://localhost:8006/login" -Method POST -ContentType "application/json" -Body $loginBody
    Write-Output "Login code: $($resp.code)"
    Write-Output "Login msg: $($resp.msg)"
    if ($resp.token) {
        Write-Output "Token: $($resp.token.Substring(0,30))..."
        $script:token = $resp.token
    }
} catch {
    Write-Output "Login error: $($_.Exception.Message)"
    if ($_.Exception.Response) {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $reader.BaseStream.Position = 0
        Write-Output "Response body: $($reader.ReadToEnd())"
    }
}

if ($script:token) {
    Write-Output ""
    Write-Output "=== Testing key endpoints with token ==="
    $headers = @{ Authorization = "Bearer $script:token" }
    
    $endpoints = @(
        "/chat/chatMsg/list",
        "/chat/chatMsgLogs/list", 
        "/chat/chatSession/list",
        "/his/redPacket/info",
        "/store/storeStatistics/list",
        "/store/store/storeOrderOffline/list",
        "/store/store/storeOrderAudit/list",
        "/store/store/statistics/list",
        "/his/integralGoods/list",
        "/his/integralOrder/list",
        "/billing/wallet/list",
        "/course/courseDomainName/list",
        "/course/courseQuestionBank/list",
        "/qwSop/sopUserLogsWx/list",
        "/index/statistics/rechargeComsumption",
        "/crm/statistics/customer",
        "/qw/qwGroupLiveCode/list",
        "/store/collection/list",
        "/course/courseAnswerLog/list",
        "/transfer/transfer/list",
        "/transfer/voiceLog/list",
        "/his/tongue/list",
        "/his/healthRecord/list",
        "/his/medicineReport/list",
        "/his/inquiryReport/list"
    )
    
    foreach ($ep in $endpoints) {
        try {
            $r = Invoke-WebRequest -Uri "http://localhost:8006$ep" -Method GET -Headers $headers -ErrorAction Stop
            $body = $r.Content | ConvertFrom-Json
            Write-Output "$ep => HTTP $($r.StatusCode) code=$($body.code) msg=$($body.msg)"
        } catch {
            $status = $_.Exception.Response.StatusCode.value__
            if ($status -eq 404) {
                Write-Output "$ep => HTTP 404 NOT FOUND"
            } elseif ($status -eq 405) {
                Write-Output "$ep => HTTP 405 Method Not Allowed"
            } else {
                Write-Output "$ep => HTTP $status ERROR: $($_.Exception.Message)"
            }
        }
    }
}
