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

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

  const count = await prisma.fundRequest.count({
    where: {
      requestNo: {
        startsWith: prefix
      }
    }
  });

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