# 通过API查询文档状态
Write-Host "`n📋 查询知识库和文档状态...`n" -ForegroundColor Green

# 获取知识库列表
try {
    $response = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases" -Method GET
    
    if ($response.data.Count -eq 0) {
        Write-Host "❌ 没有找到任何知识库" -ForegroundColor Red
        exit
    }
    
    Write-Host "找到 $($response.data.Count) 个知识库：`n" -ForegroundColor Cyan
    
    foreach ($kb in $response.data) {
        Write-Host "📚 知识库: $($kb.name)" -ForegroundColor Yellow
        Write-Host "   ID: $($kb.id)"
        Write-Host "   描述: $($kb.description)"
        Write-Host "   文件数: $($kb.fileCount)"
        Write-Host "   创建时间: $($kb.createdAt)"
        
        # 获取该知识库的文档列表
        try {
            $docsResponse = Invoke-RestMethod -Uri "http://localhost:3001/api/v1/knowledge-bases/$($kb.id)/documents" -Method GET
            
            if ($docsResponse.data.Count -gt 0) {
                Write-Host "`n   📄 文档列表：" -ForegroundColor Cyan
                
                foreach ($doc in $docsResponse.data) {
                    $statusEmoji = switch ($doc.status) {
                        "uploading" { "📤" }
                        "parsing" { "🔄" }
                        "indexing" { "⚙️" }
                        "completed" { "✅" }
                        "error" { "❌" }
                        default { "❓" }
                    }
                    
                    Write-Host "   $statusEmoji $($doc.filename)" -ForegroundColor White
                    Write-Host "      状态: $($doc.status)"
                    Write-Host "      大小: $([math]::Round($doc.fileSizeBytes / 1024, 2)) KB"
                    Write-Host "      上传时间: $($doc.uploadedAt)"
                    
                    if ($doc.status -eq "completed") {
                        Write-Host "      ✓ 段落数: $($doc.segmentsCount)" -ForegroundColor Green
                        Write-Host "      ✓ Token数: $($doc.tokensCount)" -ForegroundColor Green
                    }
                    
                    if ($doc.status -eq "error" -and $doc.errorMessage) {
                        Write-Host "      ✗ 错误: $($doc.errorMessage)" -ForegroundColor Red
                    }
                    
                    Write-Host ""
                }
            } else {
                Write-Host "   （该知识库暂无文档）`n"
            }
            
        } catch {
            Write-Host "   ❌ 获取文档列表失败: $($_.Exception.Message)" -ForegroundColor Red
        }
        
        Write-Host ("-" * 60)
        Write-Host ""
    }
    
} catch {
    Write-Host "❌ 查询失败: $($_.Exception.Message)" -ForegroundColor Red
}


