export type VIAStrength =
  | "creativity" | "curiosity" | "judgment" | "loveOfLearning" | "perspective"
  | "bravery" | "perseverance" | "honesty" | "zest"
  | "love" | "kindness" | "socialIntelligence"
  | "teamwork" | "fairness" | "leadership"
  | "forgiveness" | "humility" | "prudence" | "selfRegulation"
  | "appreciation" | "gratitude" | "hope" | "humor" | "spirituality";

export type VIAVirtue = "wisdom" | "courage" | "humanity" | "justice" | "temperance" | "transcendence";

const strengthItems: Record<VIAStrength, number[]> = {
  creativity: [1, 2],
  curiosity: [3, 4],
  judgment: [5, 6],
  loveOfLearning: [7, 8],
  perspective: [9, 10],
  bravery: [11, 12],
  perseverance: [13, 14],
  honesty: [15, 16],
  zest: [17, 18],
  love: [19, 20],
  kindness: [21, 22],
  socialIntelligence: [23, 24],
  teamwork: [25, 26],
  fairness: [27, 28],
  leadership: [29, 30],
  forgiveness: [31, 32],
  humility: [33, 34],
  prudence: [35, 36],
  selfRegulation: [37, 38],
  appreciation: [39, 40],
  gratitude: [41, 42],
  hope: [43, 44],
  humor: [45, 46],
  spirituality: [47, 48],
};

const virtueStrengths: Record<VIAVirtue, VIAStrength[]> = {
  wisdom: ["creativity", "curiosity", "judgment", "loveOfLearning", "perspective"],
  courage: ["bravery", "perseverance", "honesty", "zest"],
  humanity: ["love", "kindness", "socialIntelligence"],
  justice: ["teamwork", "fairness", "leadership"],
  temperance: ["forgiveness", "humility", "prudence", "selfRegulation"],
  transcendence: ["appreciation", "gratitude", "hope", "humor", "spirituality"],
};

export const strengthNames: Record<VIAStrength, string> = {
  creativity: "创造力",
  curiosity: "好奇心",
  judgment: "判断力",
  loveOfLearning: "热爱学习",
  perspective: "洞察力",
  bravery: "勇敢",
  perseverance: "坚持",
  honesty: "真诚",
  zest: "热情",
  love: "爱",
  kindness: "善良",
  socialIntelligence: "社交智慧",
  teamwork: "团队精神",
  fairness: "公平",
  leadership: "领导力",
  forgiveness: "宽恕",
  humility: "谦逊",
  prudence: "审慎",
  selfRegulation: "自我调节",
  appreciation: "审美",
  gratitude: "感恩",
  hope: "希望",
  humor: "幽默",
  spirituality: "灵性",
};

export const virtueNames: Record<VIAVirtue, string> = {
  wisdom: "智慧",
  courage: "勇气",
  humanity: "仁爱",
  justice: "正义",
  temperance: "节制",
  transcendence: "超越",
};

function average(values: number[]) {
  return values.reduce((sum, value) => sum + value, 0) / values.length;
}

export function calculateVIAResults(answers: string[]) {
  const strengthScores = Object.fromEntries(
    Object.entries(strengthItems).map(([strength, items]) => [
      strength,
      average(items.map((item) => Number(answers[item - 1] || 1))),
    ]),
  ) as Record<VIAStrength, number>;

  const virtueScores = Object.fromEntries(
    Object.entries(virtueStrengths).map(([virtue, strengths]) => [
      virtue,
      average(strengths.map((strength) => strengthScores[strength])),
    ]),
  ) as Record<VIAVirtue, number>;

  const rankedStrengths = (Object.keys(strengthScores) as VIAStrength[])
    .map((id) => ({ id, name: strengthNames[id], score: strengthScores[id] }))
    .sort((a, b) => b.score - a.score);

  const rankedVirtues = (Object.keys(virtueScores) as VIAVirtue[])
    .map((id) => ({ id, name: virtueNames[id], score: virtueScores[id] }))
    .sort((a, b) => b.score - a.score);

  return { strengthScores, virtueScores, rankedStrengths, rankedVirtues };
}
