/**
 * OpenRouter client configuration for Vercel AI SDK
 * Handles initialization of the OpenRouter provider and model selection
 */

import { createOpenRouter } from '@openrouter/ai-sdk-provider'

/**
 * Get the OpenRouter client instance
 * Uses OPENROUTER_API_KEY from environment variables
 */
function getOpenRouterClient() {
  const apiKey = process.env.OPENROUTER_API_KEY
  if (!apiKey) {
    throw new Error('OPENROUTER_API_KEY environment variable is not set')
  }

  return createOpenRouter({
    apiKey,
    baseURL: 'https://openrouter.ai/api/v1',
  })
}

/**
 * Get the configured language model from environment
 * Falls back to gpt-oss-120b if OPENROUTER_MODEL is not set
 */
export function getConfiguredModel() {
  const modelId = process.env.OPENROUTER_MODEL || 'openai/gpt-oss-120b'
  const client = getOpenRouterClient()
  return client(modelId)
}

/**
 * Get a specific model by ID from OpenRouter
 * Useful for overriding the default model
 */
export function getModelById(modelId: string) {
  const client = getOpenRouterClient()
  return client(modelId)
}

/**
 * Get the embedding model for RAG (text-embedding-3-large)
 */
export function getEmbeddingModel() {
  const client = getOpenRouterClient()
  return client.textEmbeddingModel('openai/text-embedding-3-large')
}
