const LITELLM_URL = process.env.LITELLM_URL || 'https://llm-dev.meghsakha.com'
const LITELLM_MODEL = process.env.LITELLM_MODEL || 'gpt-oss-120b'
const LITELLM_API_KEY = process.env.LITELLM_API_KEY || ''

export async function translateText(text: string, from: 'de' | 'en'): Promise<string | null> {
  if (!text.trim()) return null
  const toLang = from === 'de' ? 'English' : 'German'
  try {
    const r = await fetch(`${LITELLM_URL}/chat/completions`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${LITELLM_API_KEY}` },
      body: JSON.stringify({
        model: LITELLM_MODEL,
        messages: [
          { role: 'system', content: `Translate the following text to ${toLang}. Output only the translated text, nothing else.` },
          { role: 'user', content: text },
        ],
        max_tokens: 1000,
        temperature: 0.1,
      }),
    })
    if (!r.ok) return null
    const data = await r.json()
    return (data.choices?.[0]?.message?.content as string | undefined)?.trim() || null
  } catch {
    return null
  }
}
