/* Canonical column layout for the bulk admission CSV upload (portal →
   Admissions → Bulk Upload). The uploaded file's header row must match this
   exact arrangement — same columns, same order, no extras — or the upload is
   rejected on the client before submit and again on the server. */

export const BULK_ADMISSION_COLUMNS = [
  "studentName",
  "guardianPhone",
  "guardianName",
  "studentNameBn",
  "gender",
  "dob",
  "className",
  "email",
  "studentPhone",
  "address",
  "note",
] as const;

export type BulkAdmissionColumn = (typeof BULK_ADMISSION_COLUMNS)[number];

export const BULK_ADMISSION_REQUIRED: BulkAdmissionColumn[] = ["studentName", "guardianPhone"];

export function normalizeBulkHeader(h: string): string {
  return String(h ?? "")
    .replace(/\uFEFF/g, "") // Excel BOM on the first cell
    .trim()
    .toLowerCase()
    .replace(/\s+/g, "");
}

/* exact arrangement check: every template column present, in order, nothing else */
export function isValidBulkAdmissionHeaders(headers: string[]): boolean {
  if (!Array.isArray(headers) || headers.length !== BULK_ADMISSION_COLUMNS.length) return false;
  return BULK_ADMISSION_COLUMNS.every(
    (col, i) => normalizeBulkHeader(headers[i]) === normalizeBulkHeader(col)
  );
}

/* the downloadable template: header row only — users add their data below it */
export function buildBulkAdmissionTemplateCsv(): string {
  return BULK_ADMISSION_COLUMNS.join(",") + "\n";
}
