const prisma = require("../../db/client")
const { ensureCounterAtLeast, nextId } = require("./idGenerator.service")
const COMMENT_ID_KEY = "ids:comment"

async function ensureCommentIdCounter() {
  const latest = await prisma.comment.findFirst({
    select: { id: true },
    orderBy: { id: "desc" },
  })
  const maxId = latest?.id ?? 0
  await ensureCounterAtLeast(COMMENT_ID_KEY, maxId)
}

async function generateCommentId() {
  return nextId(COMMENT_ID_KEY)
}

module.exports = { ensureCommentIdCounter, generateCommentId }
