# Test frontend paths through proxy (port 80) using correct VUE_APP_BASE_API = /prod-api
$base = 'http://localhost:80'
$apiPrefix = '/prod-api'

# Login first - use Invoke-WebRequest to avoid PowerShell JSON parsing issues
$body = @{tenantCode='T202605253515';username='admin';password='admin123'} | ConvertTo-Json
$loginResp = Invoke-WebRequest -Uri "$base$apiPrefix/company/login" -Method Post -ContentType 'application/json' -Body $body -UseBasicParsing
$loginJson = $loginResp.Content | ConvertFrom-Json
$token = $loginJson.token
Write-Host "LOGIN: code=$($loginJson.code), token length=$($token.Length)"

if ([string]::IsNullOrEmpty($token)) {
    Write-Host "Token is empty! Trying direct backend login..."
    $loginResp2 = Invoke-WebRequest -Uri 'http://localhost:8006/company/login' -Method Post -ContentType 'application/json' -Body $body -UseBasicParsing
    $loginJson2 = $loginResp2.Content | ConvertFrom-Json
    $token = $loginJson2.token
    Write-Host "Direct login: code=$($loginJson2.code), token length=$($token.Length)"
}

if ([string]::IsNullOrEmpty($token)) {
    Write-Host "Cannot get token, aborting"
    exit 1
}

# Test endpoints
$tests = @(
    # HIS direct
    @{name='HIS:healthTongue'; path='/his/healthTongue/list'},
    @{name='HIS:healthRecord'; path='/his/healthRecord/list'},
    @{name='HIS:drugReport'; path='/his/drugReport/list'},
    @{name='HIS:exportTask'; path='/his/exportTask/list'},
    @{name='HIS:inquiryOrder'; path='/his/inquiryOrder/list'},
    @{name='HIS:redPacket'; path='/his/redPacket/info'},
    @{name='HIS:integralGoods'; path='/his/integralGoods/list'},
    @{name='HIS:doctor'; path='/his/doctor/list'},
    @{name='HIS:prescription'; path='/his/prescription/list'},
    @{name='HIS:diagnosis'; path='/his/diagnosis/list'},
    # HIS/fs* (rewritten to /store/*)
    @{name='HIS/fsPatient'; path='/his/fsPatient/list'},
    @{name='HIS/fsUser'; path='/his/fsUser/list'},
    @{name='HIS/fsMaterial'; path='/his/fsMaterial/list'},
    @{name='HIS/fsCoupon'; path='/his/fsCoupon/list'},
    @{name='HIS/fsStoreOrderOffline'; path='/his/fsStoreOrderOffline/list'},
    @{name='HIS/fsStoreOrderAudit'; path='/his/fsStoreOrderAudit/list'},
    @{name='HIS/fsStoreStatistics'; path='/his/fsStoreStatistics/list'},
    @{name='HIS/fsCollection'; path='/his/fsCollection/list'},
    # COURSE
    @{name='COURSE:courseDomainName'; path='/course/courseDomainName/list'},
    @{name='COURSE:courseQuestionBank'; path='/course/courseQuestionBank/list'},
    @{name='COURSE:courseAnswerlogs'; path='/course/courseAnswerlogs/list'},
    @{name='COURSE:courseInfo'; path='/course/courseInfo/list'},
    @{name='COURSE:coursePeriod'; path='/course/coursePeriod/list'},
    # BILLING
    @{name='BILLING:wallet'; path='/billing/wallet/list'},
    @{name='BILLING:bill'; path='/billing/bill/list'},
    # TRANSFER
    @{name='TRANSFER:fsTransfer'; path='/transfer/fsTransfer/list'},
    @{name='TRANSFER:fsTransferConfig'; path='/transfer/fsTransferConfig/list'},
    # QW
    @{name='QW:tag'; path='/qw/tag/list'},
    @{name='QW:sop'; path='/qw/sop/list'},
    @{name='QW:material'; path='/qw/material/list'},
    @{name='QW:user'; path='/qw/user/list'},
    @{name='QW:groupMsg'; path='/qw/groupMsg/list'},
    # QW Sop
    @{name='QWSop:sopUserLogs'; path='/qwSop/sopUserLogs/list'},
    # WX/wxSop (rewritten)
    @{name='WX:wxSop'; path='/wx/wxSop/list'},
    @{name='WX:wxSopUser'; path='/wx/wxSopUser/list'},
    @{name='WX:wxSopUserInfo'; path='/wx/wxSopUserInfo/list'},
    @{name='WX:wxSopLogs'; path='/wx/wxSopLogs/list'},
    # CHAT
    @{name='CHAT:chatMsg'; path='/chat/chatMsg/list'},
    @{name='CHAT:chatMsgLogs'; path='/chat/chatMsgLogs/list'},
    @{name='CHAT:chatSession'; path='/chat/chatSession/list'},
    # INDEX
    @{name='INDEX:statistics'; path='/index/statistics/rechargeComsumption'},
    # CRM
    @{name='CRM:statistics'; path='/crm/statistics/customer'}
)

$ok = 0; $fail = 0; $err = 0
foreach ($t in $tests) {
    $url = "$base$apiPrefix$($t.path)"
    try {
        $r = Invoke-WebRequest -Uri $url -Method Get -Headers @{Authorization="Bearer $token"} -UseBasicParsing -ErrorAction Stop
        $content = $r.Content | ConvertFrom-Json
        if ($content.code -eq 200) {
            Write-Host "OK  $($t.name) => code=200"
            $ok++
        } elseif ($content.code -eq 500) {
            Write-Host "ERR $($t.name) => code=500 msg=$($content.msg)"
            $err++
        } else {
            Write-Host "WRN $($t.name) => code=$($content.code) msg=$($content.msg)"
            $fail++
        }
    } catch {
        $code = $_.Exception.Response.StatusCode.value__
        if ($code -eq 404) {
            Write-Host "404 $($t.name) => NOT FOUND"
        } else {
            Write-Host "$code $($t.name) => ERROR"
        }
        $fail++
    }
}

Write-Host "`n=== SUMMARY: OK=$ok, ERR(500)=$err, FAIL=$fail ==="
