import mysql from 'mysql2/promise' async function getDbConnection() { if (!process.env.MYSQL_HOST) return null return await mysql.createConnection({ host: process.env.MYSQL_HOST, user: process.env.MYSQL_USER, password: process.env.MYSQL_PASSWORD, database: process.env.MYSQL_DATABASE, }) } export async function GET(request) { try { const { searchParams } = new URL(request.url) const eventId = searchParams.get('id') const connection = await getDbConnection() if (!connection) { return Response.json({ error: 'Database not configured' }, { status: 500 }) } if (eventId) { // Get specific event with tickets const [eventRows] = await connection.execute( 'SELECT * FROM events WHERE id = ?', [eventId] ) const [ticketRows] = await connection.execute( 'SELECT * FROM tickets WHERE event_id = ? ORDER BY price ASC', [eventId] ) if (eventRows.length === 0) { return Response.json({ error: 'Event not found' }, { status: 404 }) } await connection.end() return Response.json({ event: eventRows[0], tickets: ticketRows }) } else { // Get all events const [eventRows] = await connection.execute( 'SELECT e.*, COUNT(t.id) as ticket_types FROM events e LEFT JOIN tickets t ON e.id = t.event_id GROUP BY e.id ORDER BY e.created_at DESC' ) await connection.end() return Response.json({ events: eventRows }) } } catch (error) { console.error('Events API error:', error) return Response.json({ error: 'Internal server error' }, { status: 500 }) } }