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

export default defineEventHandler(async (event) => {
  const body = await readBody(event)

  if (!body.name) {
    throw createError({
      statusCode: 400,
      message: 'El nombre es requerido'
    })
  }

  if (!body.operations || !Array.isArray(body.operations)) {
    throw createError({
      statusCode: 400,
      message: 'Las operaciones son requeridas'
    })
  }

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

  return template
})
