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

async function checkCurrentStatus() {
  try {
    const book = await prisma.book.findUnique({
      where: { id: 1 },
      include: {
        chapters: {
          orderBy: [
            { level: 'asc' },
            { number: 'asc' }
          ]
        }
      }
    });

    console.log('📖 书籍状态:');
    console.log(`  ID: ${book.id}`);
    console.log(`  标题: ${book.title}`);
    console.log(`  状态: ${book.status}`);
    console.log(`  进度: ${book.progress}%`);
    console.log('');

    // 统计各级章节
    const level1 = book.chapters.filter(c => c.level === 1);
    const level2 = book.chapters.filter(c => c.level === 2);
    const level3 = book.chapters.filter(c => c.level === 3);

    console.log('📊 大纲统计:');
    console.log(`  章: ${level1.length}`);
    console.log(`  节: ${level2.length}`);
    console.log(`  小节: ${level3.length}`);
    console.log('');

    // 检查内容生成状态
    const contentStatus = {
      null: level3.filter(c => c.contentStatus === null).length,
      pending: level3.filter(c => c.contentStatus === 'pending').length,
      generating: level3.filter(c => c.contentStatus === 'generating').length,
      completed: level3.filter(c => c.contentStatus === 'completed').length,
      failed: level3.filter(c => c.contentStatus === 'failed').length,
    };

    console.log('📝 内容生成状态 (小节级别):');
    console.log(`  未开始: ${contentStatus.null}`);
    console.log(`  待生成: ${contentStatus.pending}`);
    console.log(`  生成中: ${contentStatus.generating}`);
    console.log(`  已完成: ${contentStatus.completed}`);
    console.log(`  失败: ${contentStatus.failed}`);
    console.log('');

    // 检查是否有正在生成的
    if (contentStatus.generating > 0) {
      const generating = level3.filter(c => c.contentStatus === 'generating');
      console.log('🔄 正在生成的小节:');
      generating.forEach(c => {
        console.log(`  - ${c.title}`);
      });
      console.log('');
    }

    // 显示前3个已完成和未完成的小节
    const completed = level3.filter(c => c.contentStatus === 'completed').slice(0, 3);
    const notCompleted = level3.filter(c => c.contentStatus !== 'completed').slice(0, 3);

    if (completed.length > 0) {
      console.log('✅ 已完成的小节示例:');
      completed.forEach(c => {
        console.log(`  - ${c.title} (${c.wordCount}字)`);
      });
      console.log('');
    }

    if (notCompleted.length > 0) {
      console.log('⏳ 待生成的小节示例:');
      notCompleted.forEach(c => {
        console.log(`  - ${c.title} [${c.contentStatus || 'null'}]`);
      });
    }

  } catch (error) {
    console.error('错误:', error);
  } finally {
    await prisma.$disconnect();
  }
}

checkCurrentStatus();
