/**
 * Detect user's preferred language from their existing notes
 * Uses a single DB-level GROUP BY query — no note content is loaded
 */

import { auth } from '@/auth'
import { prisma } from '@/lib/prisma'
import { unstable_cache } from 'next/cache'
import { SupportedLanguage } from './load-translations'

const SUPPORTED_LANGUAGES = new Set(['en', 'fr', 'es', 'de', 'fa', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'ar', 'hi', 'nl', 'pl'])

const getCachedUserLanguage = unstable_cache(
  async (userId: string): Promise<SupportedLanguage> => {
    try {
      // Single aggregated query — no notes are fetched, only language counts
      const result = await prisma.note.groupBy({
        by: ['language'],
        where: {
          userId,
          language: { not: null }
        },
        _sum: { languageConfidence: true },
        _count: true,
        orderBy: { _sum: { languageConfidence: 'desc' } },
        take: 1,
      })

      if (result.length > 0 && result[0].language) {
        const topLanguage = result[0].language as SupportedLanguage
        if (SUPPORTED_LANGUAGES.has(topLanguage)) {
          return topLanguage
        }
      }

      return 'en'
    } catch (error) {
      console.error('Error detecting user language:', error)
      return 'en'
    }
  },
  ['user-language'],
  { tags: ['user-language'] }
)

export async function detectUserLanguage(): Promise<SupportedLanguage> {
  const session = await auth()

  if (!session?.user?.id) {
    return 'en'
  }

  return getCachedUserLanguage(session.user.id)
}
