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

async function resetAndTrigger() {
  const bookId = 1;
  
  console.log(`重置书籍 ${bookId} 的生成状态...\n`);
  
  // 1. 清空所有章节数据（保留book记录）
  const deleted = await prisma.bookChapter.deleteMany({
    where: { bookId }
  });
  console.log(`已清空章节数据: ${deleted.count} 条`);
  
  // 2. 重置书籍状态
  await prisma.book.update({
    where: { id: bookId },
    data: {
      status: 'draft',
      progress: 0,
      errorMsg: null,
    }
  });
  console.log('书籍状态已重置为 draft\n');
  
  // 3. 触发重新生成
  console.log('发送生成请求...');
  
  const response = await fetch(`http://localhost:3000/api/book-generator/langgraph/books/${bookId}/generate`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({})
  });
  
  const result = await response.json();
  console.log('\n生成响应:', JSON.stringify(result, null, 2));
  
  await prisma.$disconnect();
}

resetAndTrigger().catch(console.error);
