import type { FastifyReply, FastifyRequest } from "fastify"
import type { PluginFastifyInstance } from "types"

const permissions = ["user.isAdmin"]

export default (fastify: PluginFastifyInstance) => {
  const interactionLinkService = fastify.custom.interactionLinkService

  const handler = async (req: FastifyRequest, reply: FastifyReply) => {
    try {
      const payload = await interactionLinkService.getAllInteractionLinks(
        fastify,
        req.requestParams,
        req.query,
      )

      return payload
    } catch (error: any) {
      fastify.log.error(error.message)
      return reply.internalServerError(error.message)
    }
  }

  return {
    handler,
    onRequest: [fastify.authenticate, fastify.checkPermissions(permissions)],
    config: {
      type: "dataRead",
    },
    schema: {
      summary: "Get all interactionLinks",
      tags: ["core/interactionLink"],
      params: {},
    },
  }
}
