import { NextResponse } from 'next/server'
import { prisma } from '@/lib/database'

export async function GET() {
  try {
    console.log('🧪 Testing database connection...')
    
    // Test basic connection
    const result = await (prisma as any).$queryRaw`SELECT 1 as test`
    console.log('✅ Basic connection test:', result)
    
    // Check if luv2_country table exists and has data
    try {
      const countryCount = await (prisma as any).luv2_country.count()
      console.log('🌍 Country table count:', countryCount)
      
      if (countryCount > 0) {
        const sampleCountries = await (prisma as any).luv2_country.findMany({
          take: 5,
          select: {
            id: true,
            name: true,
            code: true
          }
        })
        console.log('📋 Sample countries:', sampleCountries)
      }
      
      return NextResponse.json({
        success: true,
        message: 'Database connection successful',
        countryCount,
        hasCountries: countryCount > 0
      })
    } catch (tableError) {
      console.error('❌ Error accessing country table:', tableError)
      return NextResponse.json({
        success: false,
        error: 'Country table access failed',
        details: tableError.message
      }, { status: 500 })
    }
    
  } catch (error) {
    console.error('💥 Database connection failed:', error)
    return NextResponse.json({
      success: false,
      error: 'Database connection failed',
      details: error.message
    }, { status: 500 })
  }
}
