import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'

export const runtime = 'nodejs'

export async function GET(request: Request) {
  try {
    const { searchParams } = new URL(request.url)
    const locale = (searchParams.get('locale') || searchParams.get('language') || 'ro').toLowerCase()
    const showAll = searchParams.get('all') === 'true'
    const limit = parseInt(searchParams.get('limit') || '0') || undefined
    const search = searchParams.get('search')

    let whereClause: any = {}

    if (!showAll) {
      const langCandidates = Array.from(new Set([locale, locale.toLowerCase(), locale.toUpperCase()]))
      whereClause = { language: { in: langCandidates } }
    }

    // Add search functionality
    if (search && search.length > 0) {
      whereClause.OR = [
        { name: { contains: search, mode: 'insensitive' } },
        { abbreviation: { contains: search, mode: 'insensitive' } }
      ]
    }

    // Use select to only fetch needed fields and improve performance
    const versions = await prisma.bibleVersion.findMany({
      where: whereClause,
      select: {
        id: true,
        name: true,
        abbreviation: true,
        language: true,
        isDefault: true,
        // Don't fetch description, country, etc. unless needed
      },
      orderBy: [{ isDefault: 'desc' }, { language: 'asc' }, { name: 'asc' }],
      ...(limit && { take: limit })
    })

    // Get total count for pagination info
    const totalCount = showAll
      ? (limit ? await prisma.bibleVersion.count({ where: whereClause }) : await prisma.bibleVersion.count())
      : versions.length

    const response = NextResponse.json({
      success: true,
      versions: versions, // Already selected only needed fields
      total: totalCount,
      hasMore: limit ? versions.length >= limit : false
    })

    // Add caching headers - versions don't change often
    response.headers.set('Cache-Control', 'public, s-maxage=3600, stale-while-revalidate=7200')

    return response
  } catch (error) {
    console.error('Error fetching versions:', error)
    return NextResponse.json({ success: false, versions: [] }, { status: 500 })
  }
}
