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

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

async function generateCategoryId() {
  return nextId(CATEGORY_ID_KEY)
}

module.exports = { ensureCategoryIdCounter, generateCategoryId }
