import { ScoreMetric, ScoreSummary } from '@/lib/assessment-types';
import {
  calculateBigFiveResults,
  calculateIpipNeoResults,
  ipipNeoItemsByVersion,
} from '@/components/questionnaire/test/private/BigFiveCalculator';
import { calculateHEXACOResults } from '@/components/questionnaire/test/private/HEXACOCalculator';
import { calculateRIASECResults, riasecTypes } from '@/components/questionnaire/test/private/RIASECCalculator';
import {
  calculateSchwartzResults,
  higherOrderNames,
} from '@/components/questionnaire/test/private/SchwartzCalculator';

const levels: Record<string, string> = {
  minimal: '极轻或无', mild: '轻度', moderate: '中度', moderately_severe: '中重度',
  severe: '重度', normal: '正常范围', extremely_severe: '极重度', low: '较低',
  high: '较高', subthreshold: '亚阈值', no_insomnia: '无明显失眠',
};

const metric = (key: string, label: string, value: number, max?: number, level?: string): ScoreMetric => ({
  key, label, value, max, level: level ? levels[level] || level : undefined,
});

function sum(answers: string[]) {
  return answers.reduce((total, answer) => total + (Number(answer) || 0), 0);
}

function keyed(answers: string[]) {
  return Object.fromEntries(answers.map((answer, index) => [index + 1, answer]));
}

function severity(score: number, thresholds: Array<[number, string]>) {
  return thresholds.reduce((current, [minimum, label]) => score >= minimum ? label : current, thresholds[0][1]);
}

export function buildScoreSummary(questionnaireId: string, answers: string[]): ScoreSummary {
  if (questionnaireId === 'bigfive') {
    const result = calculateBigFiveResults(answers);
    const names: Record<string, string> = { extraversion: '外向性', agreeableness: '宜人性', conscientiousness: '尽责性', emotionalStability: '情绪稳定性', openness: '开放性' };
    return { metrics: Object.entries(result).map(([key, value]) => metric(key, names[key], value.score, 50)) };
  }
  if (questionnaireId === 'bigfive-120' || questionnaireId === 'bigfive-300') {
    const version = questionnaireId === 'bigfive-120' ? 120 : 300;
    const result = calculateIpipNeoResults(answers, [...ipipNeoItemsByVersion[version]]);
    const names: Record<string, string> = { neuroticism: '神经质', extraversion: '外向性', openness: '开放性', agreeableness: '宜人性', conscientiousness: '尽责性' };
    const max = version === 120 ? 120 : 300;
    return { metrics: Object.entries(result.domains).map(([key, value]) => metric(key, names[key], value.score, max)) };
  }
  if (questionnaireId === 'hexaco') {
    const names: Record<string, string> = { honestyHumility: '诚实谦逊', emotionality: '情绪性', extraversion: '外向性', agreeableness: '宜人性', conscientiousness: '尽责性', openness: '开放性' };
    return { metrics: Object.entries(calculateHEXACOResults(answers)).map(([key, value]) => metric(key, names[key], Number(value.toFixed(2)), 5)) };
  }
  if (questionnaireId === 'riasec') {
    const result = calculateRIASECResults(answers);
    return { primary: metric('holland', `霍兰德代码 ${result.hollandCode}`, result.ranking[0][1].score, 40), metrics: Object.entries(result.scores).map(([key, value]) => metric(key, riasecTypes[key as keyof typeof riasecTypes].name, value.score, 40)) };
  }
  if (questionnaireId === 'schwartz') {
    const result = calculateSchwartzResults(answers);
    return { metrics: Object.entries(result.higherOrderScores).map(([key, value]) => metric(key, higherOrderNames[key as keyof typeof higherOrderNames], Number(value.toFixed(2)), 5)) };
  }

  const raw = sum(answers);
  const single: Record<string, { label: string; max: number; score?: number; level?: string }> = {
    phq9: { label: 'PHQ-9 总分', max: 27, level: severity(raw, [[0, 'minimal'], [5, 'mild'], [10, 'moderate'], [15, 'moderately_severe'], [20, 'severe']]) },
    gad7: { label: 'GAD-7 总分', max: 21, level: severity(raw, [[0, 'minimal'], [5, 'mild'], [10, 'moderate'], [15, 'severe']]) },
    isi: { label: 'ISI 总分', max: 28, level: severity(raw, [[0, 'no_insomnia'], [8, 'subthreshold'], [15, 'moderate'], [22, 'severe']]) },
    bdi2: { label: 'BDI-II 总分', max: 63, level: severity(raw, [[0, 'minimal'], [14, 'mild'], [20, 'moderate'], [29, 'severe']]) },
    who5: { label: 'WHO-5 百分制得分', max: 100, score: raw * 4 },
  };
  if (single[questionnaireId]) {
    const item = single[questionnaireId];
    const primary = metric('total', item.label, item.score ?? raw, item.max, item.level);
    return { primary, metrics: [primary] };
  }
  if (questionnaireId === 'dass21') {
    const groups = { depression: [3, 5, 10, 13, 16, 17, 21], anxiety: [2, 4, 7, 9, 15, 19, 20], stress: [1, 6, 8, 11, 12, 14, 18] };
    const names = { depression: '抑郁', anxiety: '焦虑', stress: '压力' };
    return { metrics: Object.entries(groups).map(([key, ids]) => metric(key, names[key as keyof typeof names], ids.reduce((total, id) => total + Number(keyed(answers)[id] || 0), 0) * 2, 42)) };
  }
  if (questionnaireId === 'pss10') {
    const reverse = new Set([4, 5, 7, 8]);
    const score = answers.reduce((total, answer, index) => total + (reverse.has(index + 1) ? 4 - Number(answer) : Number(answer)), 0);
    const primary = metric('total', 'PSS-10 总分', score, 40, severity(score, [[0, 'low'], [14, 'moderate'], [27, 'high']]));
    return { primary, metrics: [primary] };
  }
  if (questionnaireId === 'sds') {
    const reverse = new Set([2, 5, 6, 11, 12, 14, 16, 17, 18, 20]);
    const original = answers.reduce((total, answer, index) => total + (reverse.has(index + 1) ? 5 - Number(answer) : Number(answer)), 0);
    const score = Math.round(original * 1.25);
    const primary = metric('total', 'SDS 标准分', score, 100, severity(score, [[0, 'normal'], [53, 'mild'], [63, 'moderate'], [73, 'severe']]));
    return { primary, metrics: [primary] };
  }

  const primary = metric('raw', '原始作答总和', raw);
  return { primary, metrics: [primary], note: '该数值仅用于保存和核对作答，不替代量表结果页中的正式解释。' };
}
