$ErrorActionPreference = "SilentlyContinue"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Login
$body = '{"username":"admin","password":"admin123","tenantCode":"T202605253515"}'
$resp = Invoke-WebRequest -Uri 'http://localhost:8006/login' -Method POST -ContentType 'application/json' -Body $body -UseBasicParsing -TimeoutSec 10
$json = $resp.Content | ConvertFrom-Json
$token = $json.token
Write-Output "Token: $($token.Substring(0,20))..."

$headers = @{ 'Authorization' = "Bearer $token" }

# Test his endpoints with GET method
$tests = @(
    '/his/doctor/list',
    '/his/store/list',
    '/his/user/list',
    '/his/storeOrder/list',
    '/his/storeProduct/list',
    '/his/coupon/list',
    '/his/storeBill/list',
    '/his/storeAfterSales/list',
    '/his/storeExtract/list',
    '/his/vessel/list',
    '/his/merchantAppConfig/list',
    '/his/userCoupon/list',
    '/his/userAddress/list',
    '/his/storePayment/list',
    '/his/storeSubOrder/list',
    '/his/userBill/list',
    '/his/testReport/list',
    '/his/userRecharge/list',
    '/his/questions/list',
    '/his/storeActivity/list',
    '/his/storeProductCategory/list',
    '/his/storeProductPackage/list',
    '/his/userIntegralLogs/list',
    '/his/userExtract/list',
    '/his/userOperationLog/list',
    '/his/userNewTask/list',
    '/his/testTemp/list',
    '/his/testTempItem/list',
    '/his/doctorBill/list',
    '/his/doctorExtract/list',
    '/his/doctorOperLog/list',
    '/his/doctorPrescribe/list',
    '/his/doctorPrescribeDrug/list',
    '/his/doctorProduct/list',
    '/his/drugReport/list',
    '/his/drugReportCount/list',
    '/his/exportTask/list',
    '/his/express/list',
    '/his/famousPrescribe/list',
    '/his/followTemp/list',
    '/his/fsFirstDiagnosis/list',
    '/his/healthData/list',
    '/his/healthHistoryTemp/list',
    '/his/healthLife/list',
    '/his/healthRecord/list',
    '/his/healthTongue/list',
    '/his/hfpayConfig/list',
    '/his/homeArticle/list',
    '/his/homeCategory/list',
    '/his/homeView/list',
    '/his/hospital/list',
    '/his/icd/list',
    '/his/illnessLibrary/list',
    '/his/inquiryDisease/list',
    '/his/inquiryOrder/list',
    '/his/inquiryOrderPing/list',
    '/his/inquiryOrderReport/list',
    '/his/inquiryTemp/list',
    '/his/logs/list',
    '/his/medicatedFood/list',
    '/his/department/list',
    '/his/company/list',
    '/his/companyDeduct/list',
    '/his/companyRecharge/list',
    '/his/complaint/list',
    '/his/template/list',
    '/his/patient/list',
    '/his/package/list',
    '/his/packageCate/list',
    '/his/packageFavorite/list',
    '/his/packageOrder/list',
    '/his/packageSolarTerm/list',
    '/his/adv/list',
    '/his/disease/list',
    '/his/divItem/list',
    '/his/dfAccount/list',
    '/his/price/list',
    '/his/appVersion/list',
    '/his/redPacket/list',
    '/his/promotionActive/list',
    '/his/promotionActiveLog/list',
    '/his/userNewTask/list'
)

Write-Output "=== Testing HIS endpoints with GET on fs-company (8006) ==="
$okCount = 0
$notFoundCount = 0
$errCount = 0
$okList = [System.Collections.ArrayList]::new()
$notFoundList = [System.Collections.ArrayList]::new()

foreach ($url in $tests) {
    try {
        $r = Invoke-WebRequest -Uri "http://localhost:8006$url" -Method GET -Headers $headers -UseBasicParsing -TimeoutSec 5
        $okCount++
        [void]$okList.Add("200|$url")
    } catch {
        $err = $_.Exception.Message
        if ($err -match '404') {
            $notFoundCount++
            [void]$notFoundList.Add("404|$url")
        } else {
            $errCount++
            if ($err -match '(\d{3})') { $code = $Matches[1] } else { $code = 'ERR' }
            [void]$notFoundList.Add("$code|$url")
        }
    }
}

Write-Output ""
Write-Output "HIS GET Results: OK=$okCount 404=$notFoundCount ERR=$errCount"
Write-Output ""
Write-Output "=== OK ==="
$okList | ForEach-Object { Write-Output $_ }
Write-Output ""
Write-Output "=== 404/ERR ==="
$notFoundList | ForEach-Object { Write-Output $_ }
