import { NextResponse } from 'next/server'
import mysql from 'mysql2/promise'

export const dynamic = 'force-dynamic'

async function getPool() {
  return mysql.createPool({
    host: 'localhost',
    user: 'sitemente',
    password: 'pa0ndoQRPDYBHzTXX3rbWd6E',
    database: 'sitemente',
    waitForConnections: true,
    connectionLimit: 10
  })
}

export async function GET() {
  try {
    const pool = await getPool()
    const [rows] = await pool.query('SELECT * FROM hostpioneers_leads ORDER BY total_spent DESC LIMIT 100')
    await pool.end()
    return NextResponse.json({ leads: rows })
  } catch (error) {
    console.error('Error:', error)
    return NextResponse.json({ error: 'Failed to fetch' }, { status: 500 })
  }
}
