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

export async function generateTransformationNo(transformationDate: Date, transformationType: string) {
  const yy = String(transformationDate.getUTCFullYear()).slice(-2);
  const mm = String(transformationDate.getUTCMonth() + 1).padStart(2, "0");
  const dd = String(transformationDate.getUTCDate()).padStart(2, "0");
  const settings = await getAppSettings();
  const typeCode =
    transformationType.toUpperCase() === "REGRADE"
      ? "RGD"
      : settings.transformation_prefix.toUpperCase();
  const prefix = `${typeCode}-${yy}${mm}${dd}`;

  const count = await prisma.lotTransformation.count({
    where: {
      transformationNo: {
        startsWith: prefix
      }
    }
  });

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