import { NextResponse } from "next/server"
import { testConnection, executeQuery, getPoolStats, healthCheck } from "@/lib/database"

export async function GET() {
  try {
    // Test basic connection
    const connectionTest = await testConnection()
    if (!connectionTest) {
      return NextResponse.json(
        {
          error: "Database connection failed",
          details: "Could not connect to MySQL database",
        },
        { status: 500 },
      )
    }

    // Test query execution with automatic connection management
    const result = await executeQuery("SELECT COUNT(*) as user_count FROM users")

    // Get pool statistics
    const poolStats = getPoolStats()

    // Get comprehensive health check
    const health = await healthCheck()

    return NextResponse.json({
      success: true,
      message: "Database connection successful",
      userCount: (result as any)[0].user_count,
      poolStats,
      health,
      timestamp: new Date().toISOString(),
    })
  } catch (error) {
    console.error("Database test error:", error)
    return NextResponse.json(
      {
        error: "Database test failed",
        details: error instanceof Error ? error.message : "Unknown error",
      },
      { status: 500 },
    )
  }
}
