import { getSystemConfig } from '@/lib/config'
import { getAISettings } from '@/app/actions/ai-settings'
import { auth } from '@/auth'
import { redirect } from 'next/navigation'
import { AdminAIPageClient } from './admin-ai-client'

export default async function AdminAIPage() {
  const session = await auth()
  if (!session?.user?.id) redirect('/api/auth/signin')

  const config = await getSystemConfig()
  const settings = await getAISettings(session.user.id)

  const openaiKey = config.OPENAI_API_KEY
  const ollamaUrl = config.OLLAMA_BASE_URL || config.OLLAMA_BASE_URL_TAGS || config.OLLAMA_BASE_URL_EMBEDDING

  const providers = [
    {
      name: 'OpenAI',
      status: openaiKey ? 'Connected' : 'Not Configured',
    },
    {
      name: 'Ollama',
      status: ollamaUrl ? 'Available' : 'Not Configured',
    },
  ]

  return (
    <AdminAIPageClient
      initialFeatures={{
        titleSuggestions: settings.titleSuggestions,
        semanticSearch: settings.semanticSearch,
        paragraphRefactor: settings.paragraphRefactor,
        memoryEcho: settings.memoryEcho,
        languageDetection: settings.languageDetection ?? true,
        autoLabeling: settings.autoLabeling ?? true,
      }}
      providers={providers}
    />
  )
}
