const MONTH_NAMES = [
  "January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December",
];
const TERMS = ["winter", "spring", "fall"] as const;

// v1 prototype simplification: formats using the browser's local clock
// fields directly (no timezone conversion library on the frontend). This
// is only correct when the person filling out the form is in the same
// timezone as the events (America/Toronto) -- fine for CSC exec submitting
// from campus, not fine as a general-purpose client.
function pad2(n: number): string {
  return n.toString().padStart(2, "0");
}

export function formatEventDate(datetimeLocalValue: string): string {
  const date = new Date(datetimeLocalValue);
  return `${MONTH_NAMES[date.getMonth()]} ${pad2(date.getDate())} ${date.getFullYear()} ${pad2(
    date.getHours()
  )}:${pad2(date.getMinutes())}`;
}

export function deriveYearAndTerm(datetimeLocalValue: string): { year: number; term: (typeof TERMS)[number] } {
  const date = new Date(datetimeLocalValue);
  return { year: date.getFullYear(), term: TERMS[Math.trunc(date.getMonth() / 4)] };
}

export function slugify(name: string): string {
  const slug = name
    .trim()
    .replace(/[^A-Za-z0-9\s-]/g, "")
    .replace(/\s+/g, "-");
  return slug.length > 0 ? slug : "event";
}

export function generateRequestId(slug: string): string {
  return `event-${slug}-${Date.now()}`.toLowerCase();
}
