export interface PersonalityType {
  readonly code: string;
  readonly name: string;
  readonly nickname: string;
  readonly description: string;
  readonly strengths: readonly string[];
  readonly workRelationships: string;
  readonly workplaceLanguage: string;
  readonly idealWorkEnvironment: string;
  readonly communicationStyle: string;
  readonly color: string;
  readonly bgColor: string;
  readonly borderColor: string;
  readonly icon: string;
}

export interface QuizQuestion {
  readonly id: number;
  readonly dimension: 'EI' | 'SN' | 'TF' | 'JP';
  readonly question: string;
  readonly optionA: { readonly text: string; readonly value: string };
  readonly optionB: { readonly text: string; readonly value: string };
}

export function calculateMBTI(
  answers: Record<number, string>,
  questions: readonly QuizQuestion[],
): string {
  const dimensions: Record<string, number> = { E: 0, I: 0, S: 0, N: 0, T: 0, F: 0, J: 0, P: 0 };

  questions.forEach((q) => {
    const answer = answers[q.id];
    if (answer && answer in dimensions) {
      dimensions[answer] += 1;
    }
  });

  const e_i = dimensions.E >= dimensions.I ? 'E' : 'I';
  const s_n = dimensions.S >= dimensions.N ? 'S' : 'N';
  const t_f = dimensions.T >= dimensions.F ? 'T' : 'F';
  const j_p = dimensions.J >= dimensions.P ? 'J' : 'P';

  return `${e_i}${s_n}${t_f}${j_p}`;
}

export function getPersonalityType(
  code: string,
  personalityTypes: readonly PersonalityType[],
): PersonalityType | undefined {
  return personalityTypes.find((type) => type.code === code);
}
