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

# Login first
$body = @{tenantCode='T202605253515';username='admin';password='admin123'} | ConvertTo-Json
try {
    $resp = Invoke-RestMethod -Uri "$base$apiPrefix/company/login" -Method Post -ContentType 'application/json' -Body $body
    $token = $resp.token
    Write-Host "LOGIN OK, token length: $($token.Length)"
} catch {
    Write-Host "LOGIN FAILED: $_"
    exit 1
}

# Test HIS direct paths (from fs-admin-saas, now in fs-company)
$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→store/patient'; path='/his/fsPatient/list'},
    @{name='HIS/fsUser→store/user'; path='/his/fsUser/list'},
    @{name='HIS/fsMaterial→store/material'; path='/his/fsMaterial/list'},
    @{name='HIS/fsCoupon→store/coupon'; path='/his/fsCoupon/list'},
    @{name='HIS/fsStoreOrderOffline→hisStore'; path='/his/fsStoreOrderOffline/list'},
    @{name='HIS/fsStoreOrderAudit→hisStore'; path='/his/fsStoreOrderAudit/list'},
    @{name='HIS/fsStoreStatistics→hisStore'; path='/his/fsStoreStatistics/list'},
    @{name='HIS/fsCollection→store/collection'; 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→qwSop/sopUserLogs'; path='/wx/wxSop/list'},
    @{name='WX:wxSopUser→qwSop/sopUserLogs'; path='/wx/wxSopUser/list'},
    @{name='WX:wxSopUserInfo→qwSop/sopUserLogsInfo'; path='/wx/wxSopUserInfo/list'},
    @{name='WX:wxSopLogs→qwSop/sopUserLogsInfo'; 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'},
    # STORE (direct)
    @{name='STORE:storeOrderOffline'; path='/store/storeOrderOffline/list'},
    @{name='STORE:storeOrderAudit'; path='/store/storeOrderAudit/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 "WARN $($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 ==="
