/**
 * LangGraph 状态定义和工作流
 */

import { Annotation, StateGraph, END } from '@langchain/langgraph';

// ============ 状态定义（借鉴 OpenMAIC Annotation 模式）============

/**
 * 进度 reducer：只增不减，防止中间步骤回退导致进度丢失
 */
const maxReducer = (prev: number, update: number) => Math.max(prev, update);

/**
 * 章节完成数 reducer：累加而非覆盖
 */
const appendReducer = <T>(prev: T[], update: T | T[] | undefined) => {
  if (!update) return prev;
  const items = Array.isArray(update) ? update : [update];
  return [...prev, ...items];
};

export const GraphState = Annotation.Root({
  bookId: Annotation<string>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => '' as string,
  }),
  userId: Annotation<string | number>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => '' as string,
  }),
  topic: Annotation<string>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => '' as string,
  }),
  bookScale: Annotation<string>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => '1000' as string,
  }),
  /**
   * 大纲层级：1=仅章，2=章→节，3=章→节→小节。
   * reducer: update ?? _prev — 仅当 update 非 null/undefined 时才覆盖旧值。
   * 0/false 被视为 falsy 会保留旧值；合法的 genLevel 值是 1/2/3，不受此限制。
   */
  genLevel: Annotation<number>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => 2,
  }),
  /** 用户明确指定的大纲层级（undefined=未指定/自动，1/2/3=用户主动选择）。
   *  用于区分"用户主动选了2"和"系统默认2"，防止 AI 规划时误覆盖。 */
  userSpecifiedGenLevel: Annotation<number | undefined>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => undefined as number | undefined,
  }),
  description: Annotation<string>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => '' as string,
  }),
  /** AI 书籍规划结果（planBookNode 输出） */
  bookPlan: Annotation<string | undefined>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => undefined,
  }),
  /** 当前正在处理的章节号 */
  currentChapter: Annotation<number>({
    reducer: maxReducer,
    default: () => 0,
  }),
  /** 已成功完成的章节数（只增不减） */
  completedChapters: Annotation<number[]>({
    reducer: appendReducer,
    default: () => [] as number[],
  }),
  finished: Annotation<boolean>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => false,
  }),
  error: Annotation<string | undefined>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => undefined,
  }),
  /** 生成进度 0-100，只增不减 */
  progress: Annotation<number>({
    reducer: maxReducer,
    default: () => 0,
  }),
  /** 失败章节列表，通过 reducer 合并而非覆盖 */
  failedChapters: Annotation<number[]>({
    reducer: appendReducer,
    default: () => [] as number[],
  }),
  /** 质量校验是否通过（quality_check 节点设置） */
  qualityPassed: Annotation<boolean>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => true,
  }),
  /** 重写次数计数器（每次 rewrite 节点执行 +1，用于限制最大循环次数） */
  rewriteCount: Annotation<number>({
    reducer: (prev, update) => prev + (update || 0),
    default: () => 0,
  }),
  /** 最大重写次数 */
  maxRewriteCount: Annotation<number>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => 3,
  }),
  /** 质量校验结果（JSON字符串，quality_check → rewrite 传递） */
  qualityResult: Annotation<string | undefined>({
    reducer: (_prev, update) => update ?? _prev,
    default: () => undefined,
  }),
});
