import type { Account } from "../account.schema"
import type { FrontendPayloadRequestData, PluginFastifyInstance } from "@/types"

export const registerPayload = (fastify: PluginFastifyInstance) => {
  fastify.registerPayloadHandler<Account>("account", {
    type: "account",
    displayName: (account: Account) => account.name || "",
    completion: (account: Account) => account.name || "",
    createMeta: (account: Account) => ({
      isActive: account.active,
      isBlocked: account.blocked,
      hasExpiration: !!account.expires,
    }),
  })
}

export const createAccountPayload = async (
  fastify: PluginFastifyInstance,
  items: Account[],
  requestData: FrontendPayloadRequestData,
) => await fastify.createPayload("account", items, requestData)
