const { PrismaClient } = require('@prisma/client');
const { langGraphGenerator } = require('./src/modules/book-generator');

async function testContentGeneration() {
  const prisma = new PrismaClient();
  
  console.log('创建超迷你测试书籍（2章）测试内容生成...\n');
  
  // 1. 创建书籍
  const book = await prisma.book.create({
    data: {
      title: 'Git入门',
      description: 'Git版本控制基础教程',
      bookScale: '2000',
      targetAudience: '开发者',
      style: '实用教程',
      totalChapters: 2,
    }
  });
  
  console.log(`✅ 书籍创建成功: bookId=${book.id}\n`);
  
  // 2. 同步调用生成（不使用队列）
  console.log('开始生成（同步模式）...\n');
  
  try {
    await langGraphGenerator.generate(
      book.id.toString(),
      book.description,
      book.bookScale
    );
    
    console.log('\n🎉 生成完成！');
    
    // 3. 显示结果
    const finalBook = await prisma.book.findUnique({
      where: { id: book.id },
      select: {
        id: true,
        title: true,
        status: true,
        progress: true,
      }
    });
    
    console.log(`\n最终状态: ${finalBook.status}, 进度: ${finalBook.progress}%`);
    
    // 4. 显示第一个完成的内容
    const firstContent = await prisma.bookChapter.findFirst({
      where: { bookId: book.id, level: 3, contentStatus: 'completed' },
      select: {
        title: true,
        content: true,
        wordCount: true,
      },
      orderBy: { id: 'asc' }
    });
    
    if (firstContent) {
      console.log(`\n📝 第一个完成的小节:`);
      console.log(`标题: ${firstContent.title}`);
      console.log(`字数: ${firstContent.wordCount}`);
      console.log(`\n内容预览（前500字）:`);
      console.log(firstContent.content.substring(0, 500));
      console.log('...\n');
    } else {
      console.log('\n⚠️ 没有找到已完成的内容');
    }
    
  } catch (error) {
    console.error('\n❌ 生成失败:', error.message);
    
    await prisma.book.update({
      where: { id: book.id },
      data: {
        status: 'failed',
        errorMsg: error.message,
      }
    });
  }
  
  await prisma.$disconnect();
}

testContentGeneration().catch(console.error);
