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

# Login on fs-company first to get token
$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 his 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",
    "/course/courseDomainName/list",
    "/course/courseQuestionBank/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(50, $body.msg.Length)) } else { "" }
        if ($code -eq 200) {
            Write-Output "OK   8003 $path => code=$code"
        } 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 => Method Not Allowed"
        } else {
            Write-Output "ERR  8003 $path => HTTP $status"
        }
    }
}

# Also test on fs-company (8006) for comparison
Write-Output ""
Write-Output "=== Testing same endpoints on fs-company (8006) ==="
$companyEndpoints = @(
    "/his/healthTongue/list",
    "/his/healthRecord/list",
    "/his/drugReport/list",
    "/his/exportTask/list",
    "/his/inquiryOrderReport/list"
)

foreach ($path in $companyEndpoints) {
    try {
        $r = Invoke-WebRequest -Uri "http://localhost:8006$path" -Method GET -Headers $headers -UseBasicParsing -ErrorAction Stop
        $body = $r.Content | ConvertFrom-Json
        $code = $body.code
        Write-Output "ERR  8006 $path => code=$code msg=$($body.msg)"
    } catch {
        $status = $_.Exception.Response.StatusCode.value__
        Write-Output "$status  8006 $path"
    }
}
