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

export const runtime = 'nodejs'

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    const locale = (searchParams.get('locale') || 'ro').toLowerCase()

    const langCandidates = Array.from(new Set([locale, locale.toLowerCase(), locale.toUpperCase()]))

    const versions = await prisma.bibleVersion.findMany({
      where: { language: { in: langCandidates } },
      orderBy: [{ isDefault: 'desc' }, { name: 'asc' }]
    })

    return NextResponse.json({
      success: true,
      versions: versions.map(v => ({
        id: v.id,
        name: v.name,
        abbreviation: v.abbreviation,
        language: v.language,
        isDefault: v.isDefault,
      }))
    })
  } catch (error) {
    console.error('Error fetching versions:', error)
    return NextResponse.json({ success: false, versions: [] }, { status: 500 })
  }
}

