// 查询文档状态脚本
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function checkDocuments() {
  try {
    console.log('\n📋 查询最近上传的文档...\n');
    
    const docs = await prisma.document.findMany({
      orderBy: { uploadedAt: 'desc' },
      take: 10,
      include: {
        knowledgeBase: {
          select: {
            name: true,
          }
        }
      }
    });
    
    if (docs.length === 0) {
      console.log('❌ 没有找到任何文档');
      return;
    }
    
    console.log(`找到 ${docs.length} 个文档：\n`);
    
    docs.forEach((doc, index) => {
      const statusEmoji = {
        'uploading': '📤',
        'parsing': '🔄',
        'indexing': '⚙️',
        'completed': '✅',
        'error': '❌'
      }[doc.status] || '❓';
      
      console.log(`${index + 1}. ${statusEmoji} ${doc.filename}`);
      console.log(`   知识库: ${doc.knowledgeBase.name}`);
      console.log(`   状态: ${doc.status}`);
      console.log(`   大小: ${(doc.fileSizeBytes / 1024).toFixed(2)} KB`);
      console.log(`   上传时间: ${doc.uploadedAt}`);
      
      if (doc.status === 'completed') {
        console.log(`   ✓ 段落数: ${doc.segmentsCount || 0}`);
        console.log(`   ✓ Token数: ${doc.tokensCount || 0}`);
      }
      
      if (doc.status === 'error' && doc.errorMessage) {
        console.log(`   ✗ 错误: ${doc.errorMessage}`);
      }
      
      console.log('');
    });
    
  } catch (error) {
    console.error('❌ 查询失败:', error.message);
  } finally {
    await prisma.$disconnect();
  }
}

checkDocuments();


