import { ScoreDirection, ScoreThreshold } from '@/lib/assessment-types';

export interface MetricDefinition {
  key: string;
  label: string;
  min: number;
  max?: number;
  direction?: ScoreDirection;
  highScoreMeaning?: string;
  thresholds?: ScoreThreshold[];
}

export interface QuestionnaireScoringMetadata {
  questionnaireVersion: string;
  scoreVersion: string;
  min: number;
  max?: number;
  reverseItems: number[];
  direction: ScoreDirection;
  highScoreMeaning: string;
  thresholds: ScoreThreshold[];
  metrics?: MetricDefinition[];
  note?: string;
}

const severityThresholds: ScoreThreshold[] = [
  { min: 0, label: '极轻或无' },
  { min: 5, label: '轻度' },
  { min: 10, label: '中度' },
  { min: 15, label: '中重度' },
  { min: 20, label: '重度' },
];

const none: ScoreThreshold[] = [];

export const scoringMetadata: Record<string, QuestionnaireScoringMetadata> = {
  bigfive: {
    questionnaireVersion: 'ipip-big-five-50-zh-v1',
    scoreVersion: 'mindscope-bigfive-50-v1',
    min: 10,
    max: 50,
    reverseItems: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50],
    direction: 'trait',
    highScoreMeaning: '高分代表该人格特质更明显，不代表好坏。',
    thresholds: none,
  },
  'bigfive-120': {
    questionnaireVersion: 'ipip-neo-120-zh-v1',
    scoreVersion: 'mindscope-ipip-neo-120-v1',
    min: 24,
    max: 120,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表该人格领域或侧面更明显，不代表好坏。',
    thresholds: none,
  },
  'bigfive-300': {
    questionnaireVersion: 'ipip-neo-300-zh-v1',
    scoreVersion: 'mindscope-ipip-neo-300-v1',
    min: 60,
    max: 300,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表该人格领域或侧面更明显，不代表好坏。',
    thresholds: none,
  },
  'self-esteem': {
    questionnaireVersion: 'rosenberg-self-esteem-zh-v1',
    scoreVersion: 'mindscope-rse-v1',
    min: 0,
    max: 30,
    reverseItems: [2, 5, 6, 8, 9],
    direction: 'protective',
    highScoreMeaning: '高分通常代表自尊水平更高。',
    thresholds: [{ min: 0, label: '偏低' }, { min: 15, label: '中等' }, { min: 25, label: '较高' }],
  },
  grit: {
    questionnaireVersion: 'grit-s-zh-v1',
    scoreVersion: 'mindscope-grit-v1',
    min: 1,
    max: 5,
    reverseItems: [1, 3, 5, 6],
    direction: 'trait',
    highScoreMeaning: '高分代表坚持与兴趣稳定性更明显。',
    thresholds: none,
  },
  'self-control': {
    questionnaireVersion: 'brief-self-control-zh-v1',
    scoreVersion: 'mindscope-self-control-v1',
    min: 13,
    max: 65,
    reverseItems: [1, 2, 3, 4, 5, 7, 9, 10, 12, 13],
    direction: 'protective',
    highScoreMeaning: '高分通常代表自我控制能力更强。',
    thresholds: none,
  },
  'need-for-cognition': {
    questionnaireVersion: 'need-for-cognition-18-zh-v1',
    scoreVersion: 'mindscope-nfc-v1',
    min: 18,
    max: 90,
    reverseItems: [3, 4, 5, 7, 8, 9, 12, 16, 17],
    direction: 'trait',
    highScoreMeaning: '高分代表更倾向主动思考和享受认知活动。',
    thresholds: none,
  },
  maximizer: {
    questionnaireVersion: 'maximizer-13-zh-v1',
    scoreVersion: 'mindscope-maximizer-v1',
    min: 13,
    max: 65,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表决策中最大化倾向更明显。',
    thresholds: none,
  },
  attachment: {
    questionnaireVersion: 'ecr-rs-zh-v1',
    scoreVersion: 'mindscope-attachment-v1',
    min: 1,
    max: 7,
    reverseItems: [1, 2, 3, 4, 5, 6],
    direction: 'mixed',
    highScoreMeaning: '高分含义取决于维度，焦虑/回避高分通常代表不安全依恋更明显。',
    thresholds: none,
  },
  empathy: {
    questionnaireVersion: 'iri-28-zh-v1',
    scoreVersion: 'mindscope-iri-v1',
    min: 0,
    max: 28,
    reverseItems: [3, 4, 7, 12, 13, 14, 15, 18, 19],
    direction: 'mixed',
    highScoreMeaning: '高分含义取决于维度，共情维度较高通常代表相关倾向更明显。',
    thresholds: none,
  },
  'dark-triad': {
    questionnaireVersion: 'short-dark-triad-zh-v1',
    scoreVersion: 'mindscope-sd3-v1',
    min: 1,
    max: 5,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表黑暗三联相关人格倾向更明显。',
    thresholds: none,
  },
  hexaco: {
    questionnaireVersion: 'hexaco-60-zh-v1',
    scoreVersion: 'mindscope-hexaco-v1',
    min: 1,
    max: 5,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表该人格维度更明显，不代表好坏。',
    thresholds: none,
  },
  fisher: {
    questionnaireVersion: 'fisher-temperament-zh-v1',
    scoreVersion: 'mindscope-fisher-v1',
    min: 0,
    max: 40,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表对应气质倾向更明显。',
    thresholds: none,
  },
  schwartz: {
    questionnaireVersion: 'schwartz-values-zh-v1',
    scoreVersion: 'mindscope-schwartz-v1',
    min: 1,
    max: 5,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表对应价值观优先级更高。',
    thresholds: none,
  },
  via: {
    questionnaireVersion: 'via-48-zh-v1',
    scoreVersion: 'mindscope-via-v1',
    min: 1,
    max: 5,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表对应性格优势更明显。',
    thresholds: none,
  },
  oeps: {
    questionnaireVersion: 'open-enneagram-zh-v1',
    scoreVersion: 'mindscope-oeps-v1',
    min: 0,
    max: 100,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表对应九型人格倾向更明显。',
    thresholds: none,
  },
  crt: {
    questionnaireVersion: 'crt-7-zh-v1',
    scoreVersion: 'mindscope-crt-v1',
    min: 0,
    max: 7,
    reverseItems: [],
    direction: 'ability',
    highScoreMeaning: '高分代表答对题数更多，分析性反思表现更好。',
    thresholds: [{ min: 0, label: '较低' }, { min: 3, label: '中等' }, { min: 6, label: '较高' }],
  },
  riasec: {
    questionnaireVersion: 'riasec-zh-v1',
    scoreVersion: 'mindscope-riasec-v1',
    min: 0,
    max: 40,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表对应职业兴趣类型更明显。',
    thresholds: none,
  },
  'career-anchors': {
    questionnaireVersion: 'career-anchors-zh-v1',
    scoreVersion: 'mindscope-career-anchors-v1',
    min: 1,
    max: 6,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表对应职业锚更重要。',
    thresholds: none,
  },
  dass21: {
    questionnaireVersion: 'dass-21-zh-v1',
    scoreVersion: 'mindscope-dass21-v1',
    min: 0,
    max: 42,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表抑郁、焦虑或压力相关困扰更明显。',
    thresholds: [{ min: 0, label: '正常' }, { min: 10, label: '轻度' }, { min: 14, label: '中度' }, { min: 21, label: '重度' }, { min: 28, label: '极重度' }],
  },
  who5: {
    questionnaireVersion: 'who-5-zh-v1',
    scoreVersion: 'mindscope-who5-v1',
    min: 0,
    max: 100,
    reverseItems: [],
    direction: 'protective',
    highScoreMeaning: '高分代表主观幸福感更高。',
    thresholds: [{ min: 0, label: '需要关注' }, { min: 50, label: '一般或较好' }],
  },
  ocd: {
    questionnaireVersion: 'ybocs-self-report-zh-v1',
    scoreVersion: 'mindscope-ocd-v1',
    min: 0,
    max: 40,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表强迫相关困扰更明显。',
    thresholds: [{ min: 0, label: '亚临床' }, { min: 8, label: '轻度' }, { min: 16, label: '中度' }, { min: 24, label: '重度' }, { min: 32, label: '极重度' }],
  },
  scl90: {
    questionnaireVersion: 'scl-90-zh-v1',
    scoreVersion: 'mindscope-scl90-v1',
    min: 90,
    max: 450,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表近期心理症状负担更高。',
    thresholds: [{ min: 90, label: '较低' }, { min: 160, label: '需关注' }, { min: 200, label: '较高' }],
  },
  sds: {
    questionnaireVersion: 'zung-sds-zh-v1',
    scoreVersion: 'mindscope-sds-v1',
    min: 25,
    max: 100,
    reverseItems: [2, 5, 6, 11, 12, 14, 16, 17, 18, 20],
    direction: 'risk',
    highScoreMeaning: '高分代表抑郁相关症状更明显。',
    thresholds: [{ min: 0, label: '正常范围' }, { min: 53, label: '轻度' }, { min: 63, label: '中度' }, { min: 73, label: '重度' }],
  },
  gad7: {
    questionnaireVersion: 'gad-7-zh-v1',
    scoreVersion: 'mindscope-gad7-v1',
    min: 0,
    max: 21,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表焦虑相关症状更明显。',
    thresholds: [{ min: 0, label: '极轻或无' }, { min: 5, label: '轻度' }, { min: 10, label: '中度' }, { min: 15, label: '重度' }],
  },
  phq9: {
    questionnaireVersion: 'phq-9-zh-v1',
    scoreVersion: 'mindscope-phq9-v1',
    min: 0,
    max: 27,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表抑郁相关症状更明显。',
    thresholds: severityThresholds,
  },
  pss10: {
    questionnaireVersion: 'pss-10-zh-v1',
    scoreVersion: 'mindscope-pss10-v1',
    min: 0,
    max: 40,
    reverseItems: [4, 5, 7, 8],
    direction: 'risk',
    highScoreMeaning: '高分代表主观压力更高。',
    thresholds: [{ min: 0, label: '较低' }, { min: 14, label: '中等' }, { min: 27, label: '较高' }],
  },
  bdi2: {
    questionnaireVersion: 'bdi-ii-zh-v1',
    scoreVersion: 'mindscope-bdi2-v1',
    min: 0,
    max: 63,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表抑郁相关症状更明显。',
    thresholds: [{ min: 0, label: '极轻或无' }, { min: 14, label: '轻度' }, { min: 20, label: '中度' }, { min: 29, label: '重度' }],
  },
  isi: {
    questionnaireVersion: 'isi-zh-v1',
    scoreVersion: 'mindscope-isi-v1',
    min: 0,
    max: 28,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表失眠困扰更明显。',
    thresholds: [{ min: 0, label: '无明显失眠' }, { min: 8, label: '亚阈值' }, { min: 15, label: '中度' }, { min: 22, label: '重度' }],
  },
  adhd: {
    questionnaireVersion: 'asrs-v1-1-zh-v1',
    scoreVersion: 'mindscope-adhd-v1',
    min: 0,
    max: 72,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表注意力、冲动或执行功能相关困扰更明显。',
    thresholds: [{ min: 0, label: '较低' }, { min: 24, label: '需关注' }],
  },
  gd: {
    questionnaireVersion: 'gaming-disorder-questionnaire-zh-v1',
    scoreVersion: 'mindscope-gd-v1',
    min: 0,
    max: 36,
    reverseItems: [],
    direction: 'risk',
    highScoreMeaning: '高分代表游戏相关困扰更明显。',
    thresholds: [{ min: 0, label: '较低' }, { min: 20, label: '需关注' }],
  },
  npd: {
    questionnaireVersion: 'npi-16-zh-v1',
    scoreVersion: 'mindscope-npi16-v1',
    min: 0,
    max: 16,
    reverseItems: [],
    direction: 'trait',
    highScoreMeaning: '高分代表自恋相关人格倾向更明显。',
    thresholds: none,
  },
};

export function getScoringMetadata(questionnaireId: string) {
  return scoringMetadata[questionnaireId];
}
