import { updateTemplate } from '../../utils/templates'

export default defineEventHandler(async (event) => {
  const id = getRouterParam(event, 'id')
  const body = await readBody(event)

  if (!id) {
    throw createError({
      statusCode: 400,
      message: 'ID requerido'
    })
  }

  const template = await updateTemplate(id, {
    name: body.name,
    description: body.description,
    operations: body.operations
  })

  if (!template) {
    throw createError({
      statusCode: 404,
      message: 'Template no encontrado'
    })
  }

  return template
})
