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

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

async function generateDealId() {
  return nextId(DEAL_ID_KEY)
}

module.exports = { ensureDealIdCounter, generateDealId }
