/**
 * Execute a Metabase card query (POST)
 */
export default defineEventHandler(async (event) => {
  try {
    const id = getRouterParam(event, 'id')

    if (!id) {
      throw createError({
        statusCode: 400,
        statusMessage: 'Card ID is required'
      })
    }

    const body = await readBody(event)
    const parameters = body?.parameters

    const result = await executeCardQuery(parseInt(id), parameters)
    return result
  } catch (error: any) {
    console.error('[API] Failed to execute Metabase card query:', error)
    throw createError({
      statusCode: error.statusCode || 500,
      statusMessage: error.statusMessage || 'Failed to execute query'
    })
  }
})
