export type CareerAnchor =
  | "technical"
  | "managerial"
  | "autonomy"
  | "security"
  | "entrepreneurial"
  | "service"
  | "challenge"
  | "lifestyle";

const anchorItems: Record<CareerAnchor, number[]> = {
  technical: [1, 2, 3, 4, 5],
  managerial: [6, 7, 8, 9, 10],
  autonomy: [11, 12, 13, 14, 15],
  security: [16, 17, 18, 19, 20],
  entrepreneurial: [21, 22, 23, 24, 25],
  service: [26, 27, 28, 29, 30],
  challenge: [31, 32, 33, 34, 35],
  lifestyle: [36, 37, 38, 39, 40],
};

export const anchorNames: Record<CareerAnchor, string> = {
  technical: "技术/职能能力",
  managerial: "综合管理能力",
  autonomy: "自主/独立",
  security: "安全/稳定",
  entrepreneurial: "创业创造",
  service: "服务/奉献",
  challenge: "纯粹挑战",
  lifestyle: "生活方式",
};

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

export function calculateCareerAnchorsResults(answers: string[]) {
  const scores = Object.fromEntries(
    Object.entries(anchorItems).map(([anchor, items]) => [
      anchor,
      average(items.map((item) => Number(answers[item - 1] || 1))),
    ]),
  ) as Record<CareerAnchor, number>;

  const ranked = (Object.keys(scores) as CareerAnchor[])
    .map((id) => ({ id, name: anchorNames[id], score: scores[id] }))
    .sort((a, b) => b.score - a.score);

  return {
    scores,
    ranked,
    primary: ranked[0],
    secondary: ranked[1],
  };
}
