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

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

  const count = await prisma.consignment.count({
    where: {
      consignmentNo: {
        startsWith: basePrefix
      }
    }
  });

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