import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { sql } from 'drizzle-orm';

export async function GET() {
  try {
    // 测试数据库连接和 schema
    const result = await db.execute(sql`SELECT current_schema(), current_database()`);
    
    // 测试查询 library schema 中的表
    const tablesResult = await db.execute(sql`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'library'
    `);
    
    return NextResponse.json({
      success: true,
      data: {
        connection: result,
        tables: tablesResult,
        message: 'Database connection successful'
      }
    });
  } catch (error) {
    console.error('Database test error:', error);
    return NextResponse.json(
      { 
        success: false, 
        error: error instanceof Error ? error.message : 'Database connection failed',
        details: error 
      },
      { status: 500 }
    );
  }
}
