export type ModelQuirkOverrides = Readonly<{
  temperature?: number
  interleavedThinking?: boolean
}>

const KIMI_K25_TAIL = 'kimi-k2.5'
const KIMI_K25_ALIAS = 'k2p5'
const KIMI_K2_THINKING_TAIL = 'kimi-k2-thinking'

function stripWrappingQuotes(value: string): string {
  const trimmed = value.trim()
  if (
    (trimmed.startsWith('"') && trimmed.endsWith('"'))
    || (trimmed.startsWith('\'') && trimmed.endsWith('\''))
  ) {
    return trimmed.slice(1, -1).trim()
  }
  return trimmed
}

function normalizeModelId(raw: string | undefined | null): string {
  if (typeof raw !== 'string')
    return ''
  const cleaned = stripWrappingQuotes(raw)
  return cleaned.trim().toLowerCase()
}

export function resolveModelQuirkOverrides(
  modelId: string | undefined | null,
): ModelQuirkOverrides | null {
  const normalized = normalizeModelId(modelId)
  if (!normalized)
    return null
  const tail = normalized.split('/').filter(Boolean).pop() || ''

  if (normalized === KIMI_K25_ALIAS || tail === KIMI_K25_TAIL) {
    return {
      temperature: 1,
      interleavedThinking: true,
    }
  }

  if (tail === KIMI_K2_THINKING_TAIL) {
    return {
      interleavedThinking: true,
    }
  }

  return null
}
