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

async function monitorGeneration() {
  const bookId = 1;
  
  console.log('开始监控书籍生成进度...\n');
  
  for (let i = 0; i < 10; i++) {
    const book = await prisma.book.findUnique({
      where: { id: bookId },
      select: {
        id: true,
        title: true,
        status: true,
        progress: true,
        errorMsg: true,
      }
    });
    
    const chapterCount = await prisma.bookChapter.count({
      where: { bookId, level: 1 }
    });
    
    const sectionCount = await prisma.bookChapter.count({
      where: { bookId, level: 2 }
    });
    
    const subsectionCount = await prisma.bookChapter.count({
      where: { bookId, level: 3 }
    });
    
    const completedContent = await prisma.bookChapter.count({
      where: { bookId, level: 3, contentStatus: 'completed' }
    });
    
    console.log(`[${new Date().toLocaleTimeString()}]`);
    console.log(`  状态: ${book.status}`);
    console.log(`  进度: ${book.progress}%`);
    console.log(`  章: ${chapterCount}, 节: ${sectionCount}, 小节: ${subsectionCount}`);
    console.log(`  已完成内容: ${completedContent}`);
    if (book.errorMsg) {
      console.log(`  错误: ${book.errorMsg}`);
    }
    console.log('');
    
    if (book.status === 'completed' || book.status === 'failed') {
      console.log('生成已结束！');
      break;
    }
    
    // 等待30秒
    await new Promise(resolve => setTimeout(resolve, 30000));
  }
  
  await prisma.$disconnect();
}

monitorGeneration().catch(console.error);
