const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function monitorProgress() {
  console.log('开始监控书籍 #2 生成进度...\n');
  
  for (let i = 0; i < 60; i++) {
    const book = await prisma.book.findUnique({ 
      where: { id: 2 },
      include: { chapters: true }
    });
    
    if (!book) {
      console.log('书籍不存在');
      break;
    }
    
    const level1 = book.chapters.filter(c => c.level === 1).length;
    const level2 = book.chapters.filter(c => c.level === 2).length;
    const level3 = book.chapters.filter(c => c.level === 3).length;
    
    console.log(`[${new Date().toLocaleTimeString()}] 状态: ${book.status} | 进度: ${book.progress}% | 章: ${level1} | 节: ${level2} | 小节: ${level3}`);
    
    if (book.status === 'completed' || book.status === 'failed') {
      console.log('\n✅ 生成完成！');
      console.log('最终状态:', book.status);
      console.log('最终进度:', book.progress + '%');
      console.log('章节统计:', { level1, level2, level3, total: book.chapters.length });
      break;
    }
    
    await new Promise(resolve => setTimeout(resolve, 5000));
  }
  
  await prisma.$disconnect();
}

monitorProgress().catch(console.error);
