import { getAppSettings } from "@/lib/app-settings";
import { prisma } from "@/lib/prisma";

export async function generateJitSaleNo(saleDate: Date) {
  const settings = await getAppSettings();
  const y = saleDate.getUTCFullYear().toString();
  const m = String(saleDate.getUTCMonth() + 1).padStart(2, "0");
  const d = String(saleDate.getUTCDate()).padStart(2, "0");
  const basePrefix = `${settings.jit_sale_prefix}-${y}${m}${d}`;

  const count = await prisma.jitSale.count({
    where: {
      saleNo: {
        startsWith: basePrefix
      }
    }
  });

  return `${basePrefix}-${String(count + 1).padStart(3, "0")}`;
}
