const fs = require('fs');

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

// 1. 添加 queueService 导入
content = content.replace(
  "import { langGraphGenerator, getScaleConfig } from './langgraph-generator';",
  `import { langGraphGenerator, getScaleConfig } from './langgraph-generator';
import { queueService, QueueType } from '../../services/queue.service';`
);

// 2. 修改书籍创建接口，使用队列
content = content.replace(
  `    // 启动 LangGraph 生成（异步，自动完成：大纲→章节→前言/后记）
    langGraphGenerator.generate(
      book.id,
      body.description,
      bookScale
    ).catch(err => {
      console.error('[LangGraph] 生成失败:', err);
    });`,
  `    // 将生成任务加入队列（由队列处理器异步执行）
    const jobId = await queueService.addBookGenerationTask({
      bookId: book.id,
      topic: body.description,
      bookScale,
    });
    
    console.log(\`[LangGraph] 生成任务已加入队列: bookId=\${book.id}, jobId=\${jobId}\`);`
);

fs.writeFileSync(file, content, 'utf8');
console.log('✅ 控制器已修改为使用队列');
