import { ThinkLevel } from '../plugins/plugin_entry.js';

export function resolveThinkingDefault(model: string): ThinkLevel {
  if (model.includes('claude-3-5') || model.includes('claude-4')) return 'medium';
  if (model.includes('deepseek-r1') || model.includes('o1') || model.includes('o3')) return 'high';
  return 'off';
}

export function isThinkingSupported(model: string): boolean {
  const thinkingModels = ['claude-3-5-sonnet', 'claude-4', 'deepseek-r1', 'o1', 'o3', 'qwq'];
  return thinkingModels.some(m => model.includes(m));
}

// Anthropic 思考预算映射（参考 hermes THINKING_BUDGET）
export const ANTHROPIC_BUDGET_MAP: Record<string, number> = {
  minimal: 2000,
  low: 4000,
  medium: 8000,
  high: 16000,
};

// OpenAI reasoning effort 映射
export const OPENAI_EFFORT_MAP: Record<string, string> = {
  minimal: 'low',
  low: 'low',
  medium: 'medium',
  high: 'high',
};

/** 检测模型是否支持 adaptive thinking（Claude 4.6+） */
export function supportsAdaptiveThinking(model: string): boolean {
  return /4[-.]6/.test(model);
}

/** 模型思考能力描述 */
export interface ModelThinkingCapability {
  supported: boolean;
  adaptive: boolean;
  levels: ThinkLevel[];
  defaultLevel: ThinkLevel;
}

/** 获取模型的思考能力 */
export function getModelThinkingCapability(supplier: string, model: string): ModelThinkingCapability {
  if (supplier === 'anthropic' && supportsAdaptiveThinking(model)) {
    return { supported: true, adaptive: true, levels: ['off', 'minimal', 'low', 'medium', 'high', 'adaptive'], defaultLevel: 'adaptive' };
  }
  if (supplier === 'anthropic') {
    return { supported: true, adaptive: false, levels: ['off', 'low', 'medium', 'high'], defaultLevel: 'medium' };
  }
  if (supplier === 'openai' && /^(o1|o3)/.test(model)) {
    return { supported: true, adaptive: false, levels: ['off', 'low', 'medium', 'high'], defaultLevel: 'medium' };
  }
  if (supplier === 'deepseek' && model.includes('r1')) {
    return { supported: true, adaptive: false, levels: ['off', 'low', 'medium', 'high'], defaultLevel: 'medium' };
  }
  return { supported: false, adaptive: false, levels: ['off'], defaultLevel: 'off' };
}

/** 构建 Anthropic 思考参数 */
export function buildAnthropicThinkingParams(model: string, level: ThinkLevel) {
  if (level === 'off') return null;
  if (supportsAdaptiveThinking(model)) {
    return {
      thinking: { type: 'adaptive' as const },
      betas: ['interleaved-thinking-2025-05-14'],
    };
  }
  const budget = ANTHROPIC_BUDGET_MAP[level] || 8000;
  return {
    thinking: { type: 'enabled' as const, budget_tokens: budget },
    temperature: 1.0,
    maxTokensAdjust: budget + 4096,
    betas: ['interleaved-thinking-2025-05-14'],
  };
}

/** 构建 OpenAI/DeepSeek 思考参数 */
export function buildOpenAIThinkingParams(level: ThinkLevel) {
  if (level === 'off') return null;
  return {
    reasoning_effort: OPENAI_EFFORT_MAP[level] || 'medium',
  };
}

/** 模型上下文窗口上限映射 */
const MODEL_MAX_TOKENS: Record<string, number> = {
  'claude-opus-4-6': 200000,
  'claude-sonnet-4-6': 200000,
  'gpt-4o': 128000,
  'gpt-4o-mini': 128000,
  'o1': 200000,
  'o3': 200000,
  'deepseek-r1': 64000,
  'deepseek-chat': 64000,
};

export function getModelMaxTokens(model: string): number {
  for (const [key, val] of Object.entries(MODEL_MAX_TOKENS)) {
    if (model.includes(key)) return val;
  }
  return 128000; // 默认
}
