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

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

  const count = await prisma.lotWashing.count({
    where: {
      washingNo: {
        startsWith: prefix
      }
    }
  });

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