export type MaturityLevel =
  | "Critical Gap"
  | "Priority Gap"
  | "Growth Opportunity"
  | "Strength"
  | "Excellence";

export const LEVELS: {
  level: MaturityLevel;
  min: number;
  max: number;
  action: string;
  blurb: string;
}[] = [
  {
    level: "Critical Gap",
    min: 0,
    max: 39,
    action: "Foundation course + action plan",
    blurb:
      "The fundamentals in this area are not yet in place. Start with a Foundation course and a written action plan before adding complexity anywhere else.",
  },
  {
    level: "Priority Gap",
    min: 40,
    max: 59,
    action: "Professional course",
    blurb:
      "Some practices exist but they are inconsistent. A Professional-level course will turn what you already do into a repeatable system.",
  },
  {
    level: "Growth Opportunity",
    min: 60,
    max: 74,
    action: "Advanced course",
    blurb:
      "Solid foundations. An Advanced course here is optional but high-return if you are actively pursuing growth.",
  },
  {
    level: "Strength",
    min: 75,
    max: 89,
    action: "Advanced / mastery",
    blurb:
      "This is a genuine strength of the business. Consider a mastery track to turn it into competitive advantage.",
  },
  {
    level: "Excellence",
    min: 90,
    max: 100,
    action: "Mentor / Executive pathway",
    blurb:
      "Excellent. The next step is the Executive MSME pathway, mentorship, or formal certification rather than another course.",
  },
];

export function levelFor(score: number): MaturityLevel {
  const match = LEVELS.find((l) => score >= l.min && score <= l.max);
  return match ? match.level : "Critical Gap";
}

export function levelBlurb(level: string): string {
  return LEVELS.find((l) => l.level === level)?.blurb ?? "";
}

export function levelAction(level: string): string {
  return LEVELS.find((l) => l.level === level)?.action ?? "";
}

/** Tailwind-safe token class for a score band. */
export function bandClass(score: number): string {
  if (score < 40) return "bg-destructive text-destructive-foreground";
  if (score < 60) return "bg-primary text-primary-foreground";
  if (score < 75) return "bg-gold text-gold-foreground";
  if (score < 90) return "bg-terracotta text-terracotta-foreground";
  return "bg-secondary text-secondary-foreground";
}

/** Convert a 1-5 Likert answer set into a 0-100 score. */
export function scoreFromAnswers(answers: number[]): number {
  if (answers.length === 0) return 0;
  const avg = answers.reduce((a, b) => a + b, 0) / answers.length;
  return Math.round(((avg - 1) / 4) * 100);
}

export const CREDENTIAL_LEVELS = [
  { level: 1, name: "UZIA Foundation Certificate", note: "Core practices in a single domain." },
  { level: 2, name: "UZIA Professional Certificate", note: "Applied systems across a discipline." },
  { level: 3, name: "UZIA Advanced Certificate", note: "Growth and scale-stage capability." },
  { level: 4, name: "UZIA Executive MSME Certificate", note: "Executive leadership of a scaling MSME." },
];

export const LIKERT_LABELS = [
  "Not at all",
  "Rarely",
  "Sometimes",
  "Mostly",
  "Fully in place",
];
