/**
 * Model factory — creates Vercel AI SDK LanguageModel instances from a resolved profile.
 *
 * Uses dynamic imports so unused provider packages don't prevent startup.
 */

import type { LanguageModel } from 'ai'
import type { ResolvedProfile } from '../../core/config.js'
import { resolveAnthropicAuthMode } from '../../core/credential-inference.js'

/** Result includes the model plus a cache key for change detection. */
export interface ModelFromConfig {
  model: LanguageModel
  /** `provider:modelId:baseUrl` — use this to detect config changes. */
  key: string
}

export async function createModelFromProfile(profile: ResolvedProfile): Promise<ModelFromConfig> {
  const p = profile.provider ?? 'anthropic'
  const m = profile.model
  const url = profile.baseUrl
  const apiKey = profile.apiKey
  const key = `${p}:${m}:${url ?? ''}`

  switch (p) {
    case 'anthropic': {
      const { createAnthropic } = await import('@ai-sdk/anthropic')
      // @ai-sdk/anthropic sends `Authorization: Bearer` when given `authToken`,
      // else `x-api-key` from `apiKey` — exactly one, mirroring the raw SDK and
      // the agent-sdk env split. Bearer is required by anthropic-compatible
      // gateways (e.g. MiniMax international); see resolveAnthropicAuthMode.
      const bearer = resolveAnthropicAuthMode(profile) === 'bearer'
      const client = createAnthropic({
        apiKey: bearer ? undefined : (apiKey || undefined),
        authToken: bearer ? (apiKey || undefined) : undefined,
        baseURL: url || undefined,
      })
      return { model: client(m), key }
    }
    case 'openai': {
      const { createOpenAI } = await import('@ai-sdk/openai')
      const client = createOpenAI({ apiKey: apiKey || undefined, baseURL: url || undefined })
      return { model: client(m), key }
    }
    case 'google': {
      const { createGoogleGenerativeAI } = await import('@ai-sdk/google')
      const client = createGoogleGenerativeAI({ apiKey: apiKey || undefined, baseURL: url || undefined })
      return { model: client(m), key }
    }
    default:
      throw new Error(`Unsupported model provider: "${p}". Supported: anthropic, openai, google`)
  }
}
