import { parseQuerySegment } from '../../../services/query-parser'
import { fetchTableMetadata } from '../../../services/table-service'

export default defineEventHandler(async (event) => {
  const table = event.context.params?.table

  if (!table) {
    throw createError({ statusCode: 400, statusMessage: 'Tabla no especificada' })
  }

  const query = getQuery(event)
  const parsedQuery = parseQuerySegment(query.query as string | undefined)

  return await fetchTableMetadata(table, { parsedQuery })
})
