/** Predefined weekly time values used across course overview pages. */
export const COURSE_HERO_WEEKLY_TIME_OPTIONS = [
  { value: "", label: "Select weekly time…" },
  { value: "25 minutes", label: "25 minutes" },
  { value: "45 minutes", label: "45 minutes" },
  { value: "70 minutes", label: "70 minutes" },
  { value: "90 minutes", label: "90 minutes" },
  { value: "120 minutes", label: "120 minutes" },
  { value: "150 minutes", label: "150 minutes" },
  { value: "200 minutes", label: "200 minutes" },
  { value: "225 minutes", label: "225 minutes" },
  { value: "250 minutes", label: "250 minutes" },
  { value: "300 minutes", label: "300 minutes" },
  { value: "Integrated across subjects", label: "Integrated across subjects" },
] as const;

export const COURSE_HERO_WEEKLY_TIME_VALUES = COURSE_HERO_WEEKLY_TIME_OPTIONS.map((option) => option.value).filter(
  Boolean
);

export const COURSE_HERO_NAME_PLACEHOLDER = "{subject}";

export const COURSE_HERO_LEGACY_DEFAULT_NAME = "Course Name";

export const defaultCourseHeroContent = {
  backLabel: "SPARK Dashboard",
  backHref: "/",
  emoji: "📚",
  name: COURSE_HERO_NAME_PLACEHOLDER,
  tagline:
    "Empowering your child to learn, grow, and thrive through hands-on exploration and real-world connections.",
  courseType: "Core Course",
  duration: "Full Year",
  weeklyTimeK5: "",
  weeklyTime612: "",
} as const;

export type CourseHeroContent = typeof defaultCourseHeroContent;

/** Normalize stored weekly time to a valid dropdown value when possible. */
export function normalizeWeeklyTimeValue(value: unknown): string {
  if (typeof value !== "string") return "";
  const trimmed = value.trim();
  if (!trimmed) return "";

  if (COURSE_HERO_WEEKLY_TIME_VALUES.includes(trimmed)) {
    return trimmed;
  }

  const numericOnly = trimmed.match(/^(\d+)\s*min(?:ute)?s?$/i);
  if (numericOnly) {
    const normalized = `${numericOnly[1]} minutes`;
    if (COURSE_HERO_WEEKLY_TIME_VALUES.includes(normalized)) {
      return normalized;
    }
  }

  return trimmed;
}

export function isValidWeeklyTimeValue(value: string): boolean {
  return !value || COURSE_HERO_WEEKLY_TIME_VALUES.includes(value);
}
