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

async function checkAndTriggerGeneration() {
  try {
    const book = await prisma.book.findUnique({
      where: { id: 1 }
    });

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

    // 检查大纲是否完整
    const chapters = await prisma.bookChapter.findMany({
      where: { bookId: 1 },
      orderBy: [
        { level: 'asc' },
        { number: 'asc' }
      ]
    });

    const level1 = chapters.filter(c => c.level === 1);
    const level2 = chapters.filter(c => c.level === 2);
    const level3 = chapters.filter(c => c.level === 3);

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

    // 检查内容生成状态
    const contentCompleted = level3.filter(c => c.contentStatus === 'completed').length;
    const contentPending = level3.filter(c => c.contentStatus !== 'completed').length;

    console.log('📝 内容生成状态:');
    console.log(`  已完成: ${contentCompleted}`);
    console.log(`  待生成: ${contentPending}`);
    console.log('');

    if (contentPending > 0) {
      console.log('💡 建议操作:');
      console.log('  需要通过API重新触发生成任务');
      console.log('  由于大纲已完成，会跳过大钢生成阶段，直接进入内容生成');
      console.log('');
      console.log('  触发方式:');
      console.log('  POST /api/book-generator/langgraph/books/1/generate');
    } else {
      console.log('✅ 所有内容已生成完成');
    }

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

checkAndTriggerGeneration();
