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

async function testStatus() {
  console.log('=== 测试双阶段状态管理 ===\n');

  // 查询最新的书籍
  const book = await prisma.book.findFirst({
    orderBy: { id: 'desc' },
    include: {
      chapters: {
        orderBy: [{ level: 'asc' }, { number: 'asc' }]
      }
    }
  });

  if (!book) {
    console.log('❌ 未找到测试书籍');
    return;
  }

  console.log(`📖 书籍: ${book.title} (id=${book.id})`);
  console.log(`状态: ${book.status}\n`);

  // 统计各级节点
  const level1Chapters = book.chapters.filter(c => c.level === 1);
  const level2Chapters = book.chapters.filter(c => c.level === 2);
  const level3Chapters = book.chapters.filter(c => c.level === 3);

  console.log(`📊 统计:`);
  console.log(`  章(level=1): ${level1Chapters.length} 个`);
  console.log(`  节(level=2): ${level2Chapters.length} 个`);
  console.log(`  小节(level=3): ${level3Chapters.length} 个\n`);

  // 检查status字段（大纲状态）
  console.log(`📝 大纲状态 (status):`);
  const statusStats = {
    pending: book.chapters.filter(c => c.status === 'pending').length,
    completed: book.chapters.filter(c => c.status === 'completed').length,
    failed: book.chapters.filter(c => c.status === 'failed').length,
  };
  console.log(`  completed: ${statusStats.completed}`);
  console.log(`  pending: ${statusStats.pending}`);
  console.log(`  failed: ${statusStats.failed}\n`);

  // 检查contentStatus字段（内容状态）
  console.log(`📄 内容状态 (contentStatus):`);
  const contentStats = {
    null: book.chapters.filter(c => c.contentStatus === null).length,
    pending: book.chapters.filter(c => c.contentStatus === 'pending').length,
    generating: book.chapters.filter(c => c.contentStatus === 'generating').length,
    completed: book.chapters.filter(c => c.contentStatus === 'completed').length,
    failed: book.chapters.filter(c => c.contentStatus === 'failed').length,
  };
  console.log(`  null (未开始): ${contentStats['null']}`);
  console.log(`  pending: ${contentStats.pending}`);
  console.log(`  generating: ${contentStats.generating}`);
  console.log(`  completed: ${contentStats.completed}`);
  console.log(`  failed: ${contentStats.failed}\n`);

  // 显示前2个小节的详细信息
  if (level3Chapters.length > 0) {
    console.log(`🔍 示例小节 (前2个):`);
    level3Chapters.slice(0, 2).forEach((c, i) => {
      console.log(`\n  ${i + 1}. ${c.title}`);
      console.log(`     level: ${c.level}`);
      console.log(`     status (大纲): ${c.status}`);
      console.log(`     contentStatus (内容): ${c.contentStatus || 'null'}`);
      console.log(`     content: ${c.content ? `有 (${c.wordCount}字)` : '无'}`);
    });
  }

  // 验证断点续传逻辑
  console.log(`\n✅ 断点续传验证:`);
  const leafNodes = level3Chapters;
  const completedLeafNodes = leafNodes.filter(c => c.contentStatus === 'completed');
  const pendingLeafNodes = leafNodes.filter(c => c.contentStatus !== 'completed');
  
  console.log(`  叶节点总数: ${leafNodes.length}`);
  console.log(`  已完成内容: ${completedLeafNodes.length} (应跳过)`);
  console.log(`  待生成内容: ${pendingLeafNodes.length} (应生成)`);
  
  if (completedLeafNodes.length > 0 && pendingLeafNodes.length > 0) {
    console.log(`  ✅ 断点续传逻辑正确：能区分已完成和待生成`);
  } else if (completedLeafNodes.length === 0 && pendingLeafNodes.length > 0) {
    console.log(`  ℹ️  内容还未开始生成`);
  }

  await prisma.$disconnect();
}

testStatus().catch(console.error);
