# Comprehensive endpoint test
$loginBody = @{
    tenantCode = "T202605253515"
    username = "admin"
    password = "admin123"
} | ConvertTo-Json

$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 all endpoints on fs-company (8006) ==="
Write-Output ""

# All endpoints to test - using GET for list endpoints
$endpoints = @(
    # chat module
    @{path="/chat/chatMsg/list"; method="GET"},
    @{path="/chat/chatMsgLogs/list"; method="GET"},
    @{path="/chat/chatSession/list"; method="GET"},
    @{path="/chat/chatGroup/list"; method="GET"},
    @{path="/chat/chatLabel/list"; method="GET"},
    @{path="/chat/chatAutoSign/list"; method="GET"},
    @{path="/chat/chatAutoSignLog/list"; method="GET"},
    @{path="/chat/chatMaterial/list"; method="GET"},
    @{path="/chat/chatMaterialCategory/list"; method="GET"},
    # qw module  
    @{path="/qw/qwGroupLiveCode/list"; method="GET"},
    @{path="/qw/qwCustomerLink/list"; method="GET"},
    @{path="/qw/qwDrainageLink/list"; method="GET"},
    @{path="/qw/qwTag/list"; method="GET"},
    @{path="/qw/qwTagGroup/list"; method="GET"},
    @{path="/qw/qwContactWay/list"; method="GET"},
    @{path="/qw/qwContactWayLog/list"; method="GET"},
    @{path="/qw/qwBatchContact/list"; method="GET"},
    @{path="/qwSop/sop/list"; method="GET"},
    @{path="/qwSop/sopLog/list"; method="GET"},
    @{path="/qwSop/sopUser/list"; method="GET"},
    @{path="/qwSop/sopUserLogsWx/list"; method="GET"},
    # his module
    @{path="/his/redPacket/info"; method="GET"},
    @{path="/his/integralGoods/list"; method="GET"},
    @{path="/his/integralOrder/list"; method="GET"},
    @{path="/his/tongue/list"; method="GET"},
    @{path="/his/healthRecord/list"; method="GET"},
    @{path="/his/medicineReport/list"; method="GET"},
    @{path="/his/inquiryReport/list"; method="GET"},
    @{path="/his/exportTask/list"; method="GET"},
    # store (company)
    @{path="/store/storeStatistics/list"; method="GET"},
    @{path="/store/collection/list"; method="GET"},
    @{path="/store/store/list"; method="GET"},
    # store (hisStore SCRM)
    @{path="/store/store/storeOrderOffline/list"; method="GET"},
    @{path="/store/store/storeOrderAudit/list"; method="GET"},
    @{path="/store/store/statistics/list"; method="GET"},
    @{path="/store/store/integralGoods/list"; method="GET"},
    @{path="/store/store/integralOrder/list"; method="GET"},
    @{path="/store/store/fsStorePay/list"; method="GET"},
    # index
    @{path="/index/statistics/rechargeComsumption"; method="GET"},
    @{path="/index/statistics/trafficLog"; method="GET"},
    # crm
    @{path="/crm/statistics/customer"; method="GET"},
    # billing
    @{path="/billing/wallet/list"; method="GET"},
    @{path="/billing/walletLog/list"; method="GET"},
    # transfer
    @{path="/transfer/transfer/list"; method="GET"},
    @{path="/transfer/voiceLog/list"; method="GET"},
    # course
    @{path="/course/courseDomainName/list"; method="GET"},
    @{path="/course/courseQuestionBank/list"; method="GET"},
    @{path="/course/courseAnswerLog/list"; method="GET"},
    @{path="/course/courseRedPacketLog/list"; method="GET"},
    # device/watch (needs fs-watch 8010)
    @{path="/device/list"; method="GET"},
    @{path="/watch/deviceStatus/list"; method="GET"}
)

foreach ($ep in $endpoints) {
    $path = $ep.path
    $method = $ep.method
    try {
        $r = Invoke-WebRequest -Uri "http://localhost:8006$path" -Method $method -Headers $headers -ErrorAction Stop
        $body = $r.Content | ConvertFrom-Json
        $code = $body.code
        $msg = $body.msg
        if ($code -eq 200) {
            Write-Output "OK   $path => code=$code"
        } else {
            Write-Output "ERR  $path => code=$code msg=$msg"
        }
    } catch {
        $status = $_.Exception.Response.StatusCode.value__
        if ($status -eq 404) {
            Write-Output "404  $path => NOT FOUND"
        } elseif ($status -eq 405) {
            Write-Output "405  $path => Method Not Allowed"
        } else {
            Write-Output "ERR  $path => HTTP $status"
        }
    }
}
