import { NextResponse } from 'next/server'
import { Pool } from 'pg'

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
})

export async function GET() {
  try {
    console.log('Test Vector - Starting database connection test')

    const client = await pool.connect()
    try {
      // Test basic connection
      const testQuery = await client.query('SELECT NOW() as current_time')
      console.log('Test Vector - Database connection successful:', testQuery.rows[0])

      // Check if ai_bible schema exists
      const schemaCheck = await client.query(`
        SELECT EXISTS (
          SELECT 1 FROM information_schema.schemata
          WHERE schema_name = 'ai_bible'
        ) AS exists
      `)
      console.log('Test Vector - ai_bible schema exists:', schemaCheck.rows[0].exists)

      // List all vector tables
      const VECTOR_SCHEMA = process.env.VECTOR_SCHEMA || 'ai_bible'
      const tables = await client.query(`
        SELECT table_name FROM information_schema.tables
        WHERE table_schema = $1 AND table_name LIKE 'bv_%'
        ORDER BY table_name
      `, [VECTOR_SCHEMA])

      console.log('Test Vector - Found vector tables:', tables.rows.length)
      tables.rows.forEach(row => console.log('- ' + row.table_name))

      // Check for English tables specifically
      const englishTables = await client.query(`
        SELECT table_name FROM information_schema.tables
        WHERE table_schema = $1 AND table_name LIKE 'bv_en_%'
        ORDER BY table_name
      `, [VECTOR_SCHEMA])

      console.log('Test Vector - English tables found:', englishTables.rows.length)

      // Check BibleVersion table for available versions
      const versions = await client.query(`
        SELECT language, abbreviation, "isDefault", name
        FROM "BibleVersion"
        WHERE language IN ('en', 'ro')
        ORDER BY language, "isDefault" DESC, "createdAt" ASC
      `)

      console.log('Test Vector - Bible versions available:')
      versions.rows.forEach(v => console.log(`- ${v.language}: ${v.abbreviation} (${v.name}) - default: ${v.isDefault}`))

      return NextResponse.json({
        success: true,
        database_connected: true,
        ai_bible_schema_exists: schemaCheck.rows[0].exists,
        total_vector_tables: tables.rows.length,
        english_vector_tables: englishTables.rows.length,
        vector_tables: tables.rows.map(r => r.table_name),
        english_tables: englishTables.rows.map(r => r.table_name),
        bible_versions: versions.rows,
        current_time: testQuery.rows[0].current_time
      })

    } finally {
      client.release()
    }
  } catch (error) {
    console.error('Test Vector - Database connection failed:', error)
    return NextResponse.json({
      success: false,
      error: error instanceof Error ? error.message : String(error),
      database_connected: false
    }, { status: 500 })
  }
}