const { prisma } = require('../src/models');
(async () => {
  // All books status
  const books = await prisma.book.findMany({ 
    where: { id: { gte: 56 } }, 
    orderBy: { id: 'desc' }, take: 5,
    select: { id: true, title: true, genStage: true, progress: true }
  });
  console.log('=== Recent Books ===');
  for (const b of books) console.log(`  #${b.id} "${(b.title||'').substring(0,20)}" stage=${b.genStage} progress=${b.progress}`);
  
  // AiCallLog
  const total = await prisma.aiCallLog.count();
  const byType = await prisma.aiCallLog.groupBy({ by: ['callType'], _count: { id: true } });
  byType.sort((a,b) => b._count.id - a._count.id);
  console.log('\n=== AiCallLog (' + total + ' total) ===');
  for (const t of byType) console.log(`  ${t.callType.padEnd(25)} ${t._count.id} 次`);
  
  // Bug check: any chapters stuck?
  const stuckContent = await prisma.bookChapter.count({ where: { genStage: 'content_generating', content: null } });
  const stuckAudio = await prisma.bookChapter.count({ where: { genStage: 'audio_generating', audioUrl: '' } });
  console.log('\n=== Stuck chapters ===');
  console.log(`  content_generating (no content): ${stuckContent}`);
  console.log(`  audio_generating (no audioUrl): ${stuckAudio}`);
  
  await prisma.$disconnect();
})();
