import type { QuestionnaireAnswersV1_2 } from './types';

/**
 * Onboarding UI 的选项 ID → 标准问卷枚举（可跳过）
 *
 * 说明：
 * - UI 侧每题目前是单选，但数据结构是 string[]；这里取第 1 个作为答案
 * - 不存在错误处理：未知/非法值统一按“跳过”处理（返回 null）
 */
export type OnboardingSelections = Record<string, string[] | undefined>;

export function mapOnboardingSelectionsToQuestionnaireAnswers(
  selections: OnboardingSelections
): QuestionnaireAnswersV1_2 {
  return {
    mom_stage: mapMomStage(selections.status?.[0]),
    emotion: mapEmotion(selections.emotion?.[0]),
    context: mapContext(selections.influence?.[0]),
    need: mapNeed(selections.support?.[0]),
  };
}

function mapMomStage(raw: string | undefined): QuestionnaireAnswersV1_2['mom_stage'] {
  // 跳过：null（显式跳过）
  if (!raw) return null;
  // UI id → 标准枚举
  if (raw === 'pregnant') return 'expecting';
  if (raw === 'has_kids') return 'parenting';
  if (raw === 'no_fill') return 'unknown';
  // 其他非法值：按跳过处理
  return null;
}

function mapEmotion(raw: string | undefined): QuestionnaireAnswersV1_2['emotion'] {
  if (!raw) return null;
  // UI 当前选项：happy/calm/stressed/low
  if (raw === 'happy') return 'joyful';
  if (raw === 'calm') return 'calm';
  if (raw === 'stressed') return 'overwhelmed';
  if (raw === 'low') return 'low';
  return null;
}

function mapContext(raw: string | undefined): QuestionnaireAnswersV1_2['context'] {
  if (!raw) return null;
  // UI id 已与标准枚举一致：family/work/relationship/friends/health
  if (raw === 'family' || raw === 'work' || raw === 'relationship' || raw === 'friends' || raw === 'health') return raw;
  return null;
}

function mapNeed(raw: string | undefined): QuestionnaireAnswersV1_2['need'] {
  if (!raw) return null;
  // UI id → 标准枚举
  if (raw === 'emotional') return 'emotional_support';
  if (raw === 'parenting') return 'parenting_pressure';
  if (raw === 'self_worth') return 'self_worth';
  if (raw === 'anxiety') return 'anxiety_relief';
  if (raw === 'balance') return 'rest_balance';
  return null;
}

