import { type User, toPublicUser } from "../user.schema"
import type { FrontendPayloadRequestData, PluginFastifyInstance } from "types"

export const registerPayload = (fastify: PluginFastifyInstance) => {
  fastify.registerPayloadHandler<User>("user", {
    type: "user",
    displayName: (user: User) => `${user.firstname} ${user.lastname}`.trim(),
    completion: (user: User) =>
      `${user.firstname} ${user.lastname} ${user.email}`,

    // Sanitization to remove sensitive data
    sanitize: (user: User) => toPublicUser(user),
  })
}

export const createUserPayload = async (
  fastify: PluginFastifyInstance,
  items: User[],
  requestData: FrontendPayloadRequestData,
) => await fastify.createPayload("user", items, requestData)
