/**
 * Canonical model IDs for the AI Ticket Analyzer pipeline.
 *
 * Use these constants — never hardcode the strings elsewhere.
 *
 * Verify quarterly against https://docs.claude.com/en/docs/about-claude/models
 * and OpenRouter's model list. Model IDs change rarely but pricing and
 * capability tiers can shift.
 */

// Anthropic
export const HAIKU = 'claude-haiku-4-5' as const;
export const SONNET = 'claude-sonnet-4-6' as const;
export const OPUS = 'claude-opus-4-7' as const;

// OpenRouter / DeepSeek
export const DEEPSEEK_V4_FLASH = 'deepseek/deepseek-v4-flash' as const;
export const DEEPSEEK_V4_PRO = 'deepseek/deepseek-v4-pro' as const;
export const DEEPSEEK_R1 = 'deepseek/deepseek-r1-0528' as const;

export type AnthropicModelId = typeof HAIKU | typeof SONNET | typeof OPUS;
export type OpenRouterModelId =
  | typeof DEEPSEEK_V4_FLASH
  | typeof DEEPSEEK_V4_PRO
  | typeof DEEPSEEK_R1;
export type ModelId = AnthropicModelId | OpenRouterModelId;

export type Provider = 'anthropic' | 'openrouter';

/**
 * Per-stage model picks for each provider. The pipeline reads from this when
 * the user picks a provider — every stage knows what model to call without
 * the caller having to wire up four constants.
 */
export interface StageModels {
  triage: ModelId;
  deep_analysis: ModelId;
  deep_reasoning: ModelId;
  fingerprint: ModelId;
  /** Aggregate-reduce step (cross-ticket bundle reports). */
  aggregate_reduce: ModelId;
  /** Haiku-suggested-links arm in link-discovery. */
  link_suggest: ModelId;
}

export const ANTHROPIC_STAGE_MODELS: StageModels = {
  triage: HAIKU,
  deep_analysis: SONNET,
  deep_reasoning: OPUS,
  fingerprint: HAIKU,
  aggregate_reduce: SONNET,
  link_suggest: HAIKU,
};

export const OPENROUTER_STAGE_MODELS: StageModels = {
  triage: DEEPSEEK_V4_FLASH,
  deep_analysis: DEEPSEEK_V4_PRO,
  deep_reasoning: DEEPSEEK_R1,
  fingerprint: DEEPSEEK_V4_FLASH,
  aggregate_reduce: DEEPSEEK_V4_PRO,
  link_suggest: DEEPSEEK_V4_FLASH,
};

export function stageModelsFor(provider: Provider): StageModels {
  return provider === 'openrouter'
    ? OPENROUTER_STAGE_MODELS
    : ANTHROPIC_STAGE_MODELS;
}

/**
 * Detect provider from a model id. Anthropic ids start with `claude-`;
 * OpenRouter ids contain a `/`. Used by the LLM call layer to dispatch.
 */
export function providerForModel(model: ModelId): Provider {
  return model.includes('/') ? 'openrouter' : 'anthropic';
}
