import type { FastifyInstance } from 'fastify'
import bcrypt from 'bcrypt'
import { z } from 'zod'

export default function (fastify: FastifyInstance) {
  fastify.post('/attachments/upload', async (req, reply) => {
    try {
      const schema = z.object({
        file: z.file(),
      })
      const input = schema.parse(req.body)

      // const file = req.file({ limits: { } })

      const id = await bcrypt.hash(input.file, 10)

      return {
        id,
      }
    }
    catch (err) {
      fastify.log.error(err)
      reply.code(400)

      if (err instanceof z.ZodError) {
        reply.send({ error: z.prettifyError(err) })
      }
      else {
        reply.send({ error: err.message })
      }
    }
  })
}
