import { INSTITUTION_TYPES } from "./constants";

export interface ClassTemplateItem {
  name: string;
  nameBn: string;
  /** year-level classes for college/university/alim carry the session */
  withSession?: boolean;
}

/** Bangladesh academic session label for the given date — "2026-27". */
export function bdSessionLabel(date = new Date()): string {
  const y = date.getFullYear();
  return `${y}-${String((y + 1) % 100).padStart(2, "0")}`;
}

const PRIMARY_CLASSES: ClassTemplateItem[] = [
  { name: "Play", nameBn: "প্লে" },
  { name: "Nursery", nameBn: "নার্সারি" },
  { name: "One", nameBn: "প্রথম" },
  { name: "Two", nameBn: "দ্বিতীয়" },
  { name: "Three", nameBn: "তৃতীয়" },
  { name: "Four", nameBn: "চতুর্থ" },
  { name: "Five", nameBn: "পঞ্চম" },
];

const HIGH_CLASSES: ClassTemplateItem[] = [
  { name: "Six", nameBn: "ষষ্ঠ" },
  { name: "Seven", nameBn: "সপ্তম" },
  { name: "Eight", nameBn: "অষ্টম" },
  { name: "Nine", nameBn: "নবম" },
  { name: "Ten", nameBn: "দশম" },
];

/** coaching centers operate across both school levels */
const COACHING_CLASSES: ClassTemplateItem[] = [...PRIMARY_CLASSES, ...HIGH_CLASSES];

const college = (prefix = ""): ClassTemplateItem[] => [
  { name: `${prefix}1st Year`, nameBn: `${prefix ? prefix + " " : ""}প্রথম বর্ষ`, withSession: true },
  { name: `${prefix}2nd Year`, nameBn: `${prefix ? prefix + " " : ""}দ্বিতীয় বর্ষ`, withSession: true },
];

const UNIVERSITY_CLASSES: ClassTemplateItem[] = [
  { name: "1st Year", nameBn: "প্রথম বর্ষ", withSession: true },
  { name: "2nd Year", nameBn: "দ্বিতীয় বর্ষ", withSession: true },
  { name: "3rd Year", nameBn: "তৃতীয় বর্ষ", withSession: true },
  { name: "4th Year", nameBn: "চতুর্থ বর্ষ", withSession: true },
];

/** Madrasa (Bangladesh context): Ebtedayee 1–5, Dakhil 6–10, Alim 1st/2nd Year */
const MADRASHA_CLASSES: ClassTemplateItem[] = [
  { name: "Ebtedayee One", nameBn: "এবতেদায়ী প্রথম" },
  { name: "Ebtedayee Two", nameBn: "এবতেদায়ী দ্বিতীয়" },
  { name: "Ebtedayee Three", nameBn: "এবতেদায়ী তৃতীয়" },
  { name: "Ebtedayee Four", nameBn: "এবতেদায়ী চতুর্থ" },
  { name: "Ebtedayee Five", nameBn: "এবতেদায়ী পঞ্চম" },
  { name: "Dakhil Six", nameBn: "দাখিল ষষ্ঠ" },
  { name: "Dakhil Seven", nameBn: "দাখিল সপ্তম" },
  { name: "Dakhil Eight", nameBn: "দাখিল অষ্টম" },
  { name: "Dakhil Nine", nameBn: "দাখিল নবম" },
  { name: "Dakhil Ten", nameBn: "দাখিল দশম" },
  { name: "Alim 1st Year", nameBn: "আলিম প্রথম বর্ষ", withSession: true },
  { name: "Alim 2nd Year", nameBn: "আলিম দ্বিতীয় বর্ষ", withSession: true },
];

/** Standard class set for an institution type (govt/non-govt share the same levels).
    style "digit" renders numeric class names ("Class 1"), "word" spells them ("One"). */
export function classTemplateFor(type: string, style: "word" | "digit" = "word"): ClassTemplateItem[] {
  const base = ((): ClassTemplateItem[] => {
    switch (type as (typeof INSTITUTION_TYPES)[number]) {
      case "PRIMARY_AND_HIGH_SCHOOL":
      case "COACHING":
        // primary + high school (and coaching, which spans both) run
        // Play/Nursery through Ten in one institution
        return COACHING_CLASSES;
      case "COLLEGE_AND_UNIVERSITY":
        // year-based levels; HSC colleges use 1st/2nd Year, honours
        // universities continue to 3rd/4th — extras can be dropped
        return UNIVERSITY_CLASSES;
      case "MADRASHA":
        return MADRASHA_CLASSES;
      default:
        return PRIMARY_CLASSES;
    }
  })();
  if (style === "word") return base;
  return base.map((item) => digitize(item));
}

const WORD_TO_N: Record<string, number> = {
  One: 1, Two: 2, Three: 3, Four: 4, Five: 5,
  Six: 6, Seven: 7, Eight: 8, Nine: 9, Ten: 10,
};

const BN_DIGITS = ["০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯"];
const bnNum = (n: number) => String(n).replace(/[0-9]/g, (d) => BN_DIGITS[Number(d)]);

/** "One" → "Class 1", "Ebtedayee One" → "Ebtedayee 1" (BN: শ্রেণি ১). */
function digitize(item: ClassTemplateItem): ClassTemplateItem {
  const parts = item.name.split(" ");
  const last = parts[parts.length - 1];
  const n = WORD_TO_N[last];
  if (n === undefined) return item; // Play / Nursery / 1st Year… stay as-is
  const prefix = parts.slice(0, -1).join(" ");
  const name = prefix ? `${prefix} ${n}` : `Class ${n}`;
  const bnPrefix = item.nameBn.replace(/\s*[^০-৯\sa-zA-Z]+$/u, "");
  const nameBn = prefix
    ? `${bnPrefix.replace(/[^০-৯a-zA-Z ]+$/u, "").trim()} ${bnNum(n)}`
    : `শ্রেণি ${bnNum(n)}`;
  return { ...item, name, nameBn };
}

/** Full display name — year-levels append the session ("1st Year — 2026-27"). */
export function templateItemName(item: ClassTemplateItem, session: string, bn = false): string {
  const base = bn ? item.nameBn : item.name;
  return item.withSession ? `${base} — ${session}` : base;
}
