/**
 * 策略2 - One-Step Outline（一步大纲 + 并行内容）
 *
 * 工作流：
 *   plan_book → generate_full_outline → write_chapters
 *
 * 特点：
 *   一次 AI 调用生成完整的树形大纲（章+节+小节）
 *   然后将 chapter 分组并行写内容
 *   调用量：~1(规划) + 1(大纲) + N(内容) 次 AI
 */

import { StateGraph, END } from '@langchain/langgraph';
import { GraphState } from '../graph';
import { planBookNode } from '../nodes/plan.node';
import { generateFullOutlineNode } from '../nodes/full-outline.node';
import { writeChaptersNode } from '../nodes/content.node';
import { runGraphWorkflow } from './base';
import { GenerationStrategy } from './types';

export class OneStepOutlineStrategy implements GenerationStrategy {
  readonly name = 'one-step-outline' as const;
  readonly description = '一步大纲+并行内容：一次AI生成完整树形大纲，然后批量写内容';

  async generate(
    bookId: string,
    topic: string,
    bookScale: string,
    genLevel: number,
    userSpecifiedGenLevel?: number
  ): Promise<void> {
    console.log(`[Strategy-one-step-outline] 开始, bookId=${bookId}, scale=${bookScale}, genLevel=${genLevel}, userSpecified=${userSpecifiedGenLevel}`);

    const workflow = new StateGraph(GraphState)
      .addNode('plan_book', planBookNode)
      .addNode('generate_full_outline', generateFullOutlineNode)
      .addNode('write_chapters', writeChaptersNode)
      .setEntryPoint('plan_book')
      .addEdge('plan_book', 'generate_full_outline')
      .addEdge('generate_full_outline', 'write_chapters')
      .addEdge('write_chapters', END);

    const initialState: typeof GraphState.State = {
      bookId,
      topic,
      bookScale,
      genLevel,
      userSpecifiedGenLevel,
      userId: '',
      description: undefined,
      bookPlan: undefined,
      currentChapter: 0,
      completedChapters: [],
      finished: false,
      error: undefined,
      progress: 0,
      failedChapters: [],
      qualityPassed: true,
      rewriteCount: 0,
      maxRewriteCount: 3,
      qualityResult: undefined,
    };

    await runGraphWorkflow(bookId, initialState, workflow);
  }
}
