import { prisma } from './lib/db'

async function testApiLogic() {
  try {
    console.log('Testing API logic...')

    const locale = 'en'
    const versionAbbr = null

    console.log(`Looking for locale: ${locale}, version: ${versionAbbr}`)

    // Get the appropriate Bible version
    let bibleVersion
    if (versionAbbr) {
      // Use specific version if provided
      bibleVersion = await prisma.bibleVersion.findFirst({
        where: {
          abbreviation: versionAbbr,
          language: locale
        }
      })
    } else {
      // Use default version for the language
      bibleVersion = await prisma.bibleVersion.findFirst({
        where: {
          language: locale,
          isDefault: true
        }
      })
    }

    console.log('Bible version found:', bibleVersion)

    if (!bibleVersion) {
      console.log(`No Bible version found for language: ${locale}`)
      return
    }

    // Get books for this version
    const books = await prisma.bibleBook.findMany({
      where: {
        versionId: bibleVersion.id
      },
      orderBy: {
        orderNum: 'asc'
      },
      include: {
        chapters: {
          orderBy: {
            chapterNum: 'asc'
          },
          include: {
            _count: {
              select: {
                verses: true
              }
            }
          }
        }
      }
    })

    console.log(`Found ${books.length} books`)

    for (const book of books.slice(0, 3)) {
      console.log(`Book: ${book.name} (${book.chapters.length} chapters)`)
    }

  } catch (error) {
    console.error('API test failed:', error)
  } finally {
    await prisma.$disconnect()
  }
}

testApiLogic()