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

export async function generateStockAdjustmentNo(adjustmentDate: Date) {
  const yy = String(adjustmentDate.getUTCFullYear()).slice(-2);
  const mm = String(adjustmentDate.getUTCMonth() + 1).padStart(2, "0");
  const dd = String(adjustmentDate.getUTCDate()).padStart(2, "0");
  const settings = await getAppSettings();
  const prefix = `${settings.adjustment_prefix.toUpperCase()}-${yy}${mm}${dd}`;

  const count = await prisma.stockAdjustment.count({
    where: {
      adjustmentNo: {
        startsWith: prefix
      }
    }
  });

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