$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"; 'Content-Type' = 'application/json' }

# Test key his endpoints directly
$tests = @(
    @{ url = '/his/doctor/list'; desc = 'his/doctor - should work (fs-company own)' },
    @{ url = '/his/store/list'; desc = 'his/store - from fs-admin-saas' },
    @{ url = '/his/user/list'; desc = 'his/user - from fs-admin-saas' },
    @{ url = '/his/storeOrder/list'; desc = 'his/storeOrder - from fs-admin-saas' },
    @{ url = '/his/storeProduct/list'; desc = 'his/storeProduct - from fs-admin-saas' },
    @{ url = '/his/coupon/list'; desc = 'his/coupon - from fs-admin-saas' },
    @{ url = '/his/storeBill/list'; desc = 'his/storeBill - from fs-admin-saas' },
    @{ url = '/his/storeAfterSales/list'; desc = 'his/storeAfterSales - from fs-admin-saas' },
    @{ url = '/his/storeExtract/list'; desc = 'his/storeExtract - from fs-admin-saas' },
    @{ url = '/his/vessel/list'; desc = 'his/vessel - from fs-admin-saas' },
    @{ url = '/his/merchantAppConfig/list'; desc = 'his/merchantAppConfig - from fs-admin-saas' },
    @{ url = '/his/userCoupon/list'; desc = 'his/userCoupon - from fs-admin-saas' },
    @{ url = '/his/userAddress/list'; desc = 'his/userAddress - from fs-admin-saas' },
    @{ url = '/his/storePayment/list'; desc = 'his/storePayment - from fs-admin-saas' },
    @{ url = '/his/storeSubOrder/list'; desc = 'his/storeSubOrder - from fs-admin-saas' },
    @{ url = '/his/userBill/list'; desc = 'his/userBill - from fs-admin-saas' },
    @{ url = '/his/testReport/list'; desc = 'his/testReport - from fs-admin-saas' },
    @{ url = '/his/userRecharge/list'; desc = 'his/userRecharge - from fs-admin-saas' },
    @{ url = '/his/questions/list'; desc = 'his/questions - from fs-admin-saas' },
    @{ url = '/his/storeActivity/list'; desc = 'his/storeActivity - from fs-admin-saas' },
    @{ url = '/his/storeProductCategory/list'; desc = 'his/storeProductCategory - from fs-admin-saas' },
    @{ url = '/his/storeProductPackage/list'; desc = 'his/storeProductPackage - from fs-admin-saas' },
    @{ url = '/his/userIntegralLogs/list'; desc = 'his/userIntegralLogs - from fs-admin-saas' },
    @{ url = '/his/userExtract/list'; desc = 'his/userExtract - from fs-admin-saas' },
    @{ url = '/his/userOperationLog/list'; desc = 'his/userOperationLog - from fs-admin-saas (EXCLUDED)' },
    @{ url = '/his/userNewTask/list'; desc = 'his/userNewTask - from fs-admin-saas' },
    @{ url = '/his/testTemp/list'; desc = 'his/testTemp - from fs-admin-saas' },
    @{ url = '/his/testTempItem/list'; desc = 'his/testTempItem - from fs-admin-saas' }
)

foreach ($t in $tests) {
    try {
        $r = Invoke-WebRequest -Uri "http://localhost:8006$($t.url)" -Method POST -Headers $headers -Body '{}' -UseBasicParsing -TimeoutSec 5
        Write-Output "200 | $($t.desc)"
    } catch {
        $err = $_.Exception.Message
        if ($err -match '(\d{3})') { $code = $Matches[1] } else { $code = 'ERR' }
        Write-Output "$code | $($t.desc)"
    }
}

# Also test on fs-admin (8003)
Write-Output ""
Write-Output "=== Testing same endpoints on fs-admin (8003) ==="
foreach ($t in $tests) {
    try {
        $r = Invoke-WebRequest -Uri "http://localhost:8003$($t.url)" -Method POST -Headers $headers -Body '{}' -UseBasicParsing -TimeoutSec 5
        Write-Output "200 | $($t.desc)"
    } catch {
        $err = $_.Exception.Message
        if ($err -match '(\d{3})') { $code = $Matches[1] } else { $code = 'ERR' }
        Write-Output "$code | $($t.desc)"
    }
}
