/**
 * 策略选择器
 * 通过配置决定使用哪个生成策略
 */

import { GenerationStrategy, StrategyConfig, StrategyName } from './types';
import { SequentialStrategy } from './sequential.strategy';
import { OneStepOutlineStrategy } from './one-step-outline.strategy';
import { PerChapterStrategy } from './per-chapter.strategy';
import { DeepPlanParallelStrategy } from './deep-plan-parallel.strategy';

// ============ 策略注册 ============

const STRATEGIES: Record<StrategyName, GenerationStrategy> = {
  'sequential': new SequentialStrategy(),
  'one-step-outline': new OneStepOutlineStrategy(),
  'per-chapter': new PerChapterStrategy(),
  'deep-plan-parallel': new DeepPlanParallelStrategy(),
};

// ============ 默认配置 ============

/**
 * 当前激活的策略名称
 * 可通过修改此配置切换生成方案，然后重新运行
 *
 * 可选值：
 *   'sequential'          - 串行（原始方案）
 *   'one-step-outline'     - 一步大纲 + 并行内容
 *   'per-chapter'          - 逐章内聚（内容质量好）
 *   'deep-plan-parallel'   - DeepPlan+RichOutline+Concurrent+Edit（当前推荐，最新策略）
 */
const DEFAULT_STRATEGY: StrategyName = 'deep-plan-parallel';

// ============ 选择 ============

let currentStrategy: StrategyName = DEFAULT_STRATEGY;

/**
 * 获取当前策略名称
 */
export function getCurrentStrategyName(): StrategyName {
  return currentStrategy;
}

/**
 * 切换当前策略（运行时切换，立即生效）
 */
export function setCurrentStrategy(name: StrategyName): void {
  if (STRATEGIES[name]) {
    currentStrategy = name;
    console.log(`[StrategySelector] 切换到策略: ${name} - ${STRATEGIES[name].description}`);
  } else {
    console.warn(`[StrategySelector] 未知策略: ${name}，保持当前: ${currentStrategy}`);
  }
}

/**
 * 获取当前策略实例
 */
export function getCurrentStrategy(): GenerationStrategy {
  return STRATEGIES[currentStrategy] || STRATEGIES['sequential'];
}

/**
 * 获取所有可用策略列表
 */
export function getAllStrategies(): GenerationStrategy[] {
  return Object.values(STRATEGIES);
}

/**
 * 根据名称获取策略
 */
export function getStrategy(name: StrategyName): GenerationStrategy {
  return STRATEGIES[name] || STRATEGIES['sequential'];
}

/** 重新导出类型 */
export type { StrategyName, StrategyConfig } from './types';
