const fs = require('fs');

const file = 'src/modules/book-generator/langgraph-controller.ts';
let content = fs.readFileSync(file, 'utf8');

// 第2处：重新生成
content = content.replace(
  /    \/\/ 启动 LangGraph 生成\n    langGraphGenerator\.generate\([\s\S]{0,200}?\);/m,
  `    // 将生成任务加入队列
    const jobId = await queueService.addBookGenerationTask({
      bookId,
      topic: book.description,
      bookScale,
    });
    
    console.log(\`[LangGraph] 重新生成任务已加入队列: bookId=\${bookId}, jobId=\${jobId}\`);`
);

// 第3处：续生成  
content = content.replace(
  /    \/\/ 重新启动生成[\s\S]{0,300}?\);/m,
  `    // 将续生成任务加入队列
    const jobId = await queueService.addBookGenerationTask({
      bookId,
      topic: book.description || book.title,
      bookScale: book.bookScale || '130000',
    });
    
    console.log(\`[LangGraph] 续生成任务已加入队列: bookId=\${bookId}, jobId=\${jobId}\`);`
);

fs.writeFileSync(file, content, 'utf8');
console.log('✅ 第2、3处已修改');
