/**
 * Get list of databases from Metabase
 */
export default defineEventHandler(async (event) => {
  try {
    const response = await getMetabaseDatabases()

    // Metabase returns {data: [...], total: number}
    // Extract the data array
    if (response && typeof response === 'object' && 'data' in response) {
      return response.data
    }

    return response
  } catch (error: any) {
    console.error('[API] Failed to get Metabase databases:', error)
    throw createError({
      statusCode: error.statusCode || 500,
      statusMessage: error.statusMessage || 'Failed to fetch databases'
    })
  }
})
