# Test his endpoints on fs-admin (8003) with proper token
$loginBody = @{
    tenantCode = "T202605253515"
    username = "admin"
    password = "admin123"
} | ConvertTo-Json

$resp = Invoke-RestMethod -Uri "http://localhost:8006/login" -Method POST -ContentType "application/json" -Body $loginBody
$token = $resp.token
$headers = @{ Authorization = "Bearer $token" }

Write-Output "=== Testing all his/health/course endpoints on fs-admin (8003) ==="
$adminEndpoints = @(
    "/his/healthTongue/list",
    "/his/healthRecord/list",
    "/his/drugReport/list",
    "/his/drugReportCount/list",
    "/his/exportTask/list",
    "/his/inquiryOrderReport/list",
    "/his/tongue/list",
    "/his/user/list",
    "/his/userCoupon/list",
    "/his/storePayment/list",
    "/his/storeOrder/list",
    "/his/storeProduct/list",
    "/his/coupon/list",
    "/his/doctor/list",
    "/his/package/list",
    "/his/testReport/list",
    "/his/testTemp/list",
    "/his/testTempItem/list",
    "/his/inquiryOrder/list",
    "/his/inquiryTemp/list",
    "/his/inquiryDisease/list",
    "/his/physicalReportTemplate/list",
    "/his/physicalReportTemplateField/list",
    "/course/courseDomainName/list",
    "/course/courseQuestionBank/list",
    "/course/courseAnswerLog/list",
    "/course/courseRedPacketLog/list",
    "/course/courseWatchLog/list",
    "/billing/wallet/list",
    "/qw/qwGroupLiveCode/list",
    "/api/fee/wallet/1"
)

foreach ($path in $adminEndpoints) {
    try {
        $r = Invoke-WebRequest -Uri "http://localhost:8003$path" -Method GET -Headers $headers -UseBasicParsing -ErrorAction Stop
        $body = $r.Content | ConvertFrom-Json
        $code = $body.code
        $msg = if ($body.msg) { $body.msg.Substring(0, [Math]::Min(60, $body.msg.Length)) } else { "" }
        if ($code -eq 200) {
            Write-Output "OK   8003 $path"
        } else {
            Write-Output "ERR  8003 $path => code=$code msg=$msg"
        }
    } catch {
        $status = $_.Exception.Response.StatusCode.value__
        if ($status -eq 404) {
            Write-Output "404  8003 $path"
        } elseif ($status -eq 405) {
            Write-Output "405  8003 $path"
        } elseif ($status -eq 401) {
            Write-Output "401  8003 $path (auth issue)"
        } else {
            Write-Output "$status  8003 $path"
        }
    }
}
