import type { useBaseStore } from '#/store';

/** 与 docs/tms/字段注释与字典规范.md 对齐 */
export const TMS_DIC = {
  yesNo: 'tmsYesNo',
  enableStatus: 'tmsEnableStatus',
  signRule: 'tmsSignRule',
  questionType: 'tmsQuestionType',
  difficulty: 'tmsDifficulty',
  questionSource: 'tmsQuestionSource',
  openClosedInvalid: 'tmsOpenClosedInvalid',
  paperSortMode: 'tmsPaperSortMode',
  courseCategory: 'tmsCourseCategory',
  trainMode: 'tmsTrainMode',
  evalMode: 'tmsEvalMode',
  archiveStatus: 'tmsArchiveStatus',
  gradeStatus: 'tmsGradeStatus',
  examStatus: 'tmsExamStatus',
  passFlag: 'tmsPassFlag',
  taskPublishStatus: 'tmsTaskPublishStatus',
  taskCategory: 'tmsTaskCategory',
  taskKind: 'tmsTaskKind',
  taskSourceType: 'tmsTaskSourceType',
  personTaskStatus: 'tmsPersonTaskStatus',
} as const;

export type TmsDicCode = (typeof TMS_DIC)[keyof typeof TMS_DIC];

export type TmsDicOpt = {
  id: string;
  enCode?: string;
  fullName: string;
  [key: string]: any;
};

/** Select 存字典 enCode（与库字段编码一致） */
export const TMS_DIC_FIELD_NAMES = { value: 'enCode', label: 'fullName' } as const;

/** 字典不可用时的兜底，仅供 loadTmsDic 内部使用 */
const FALLBACK_BY_CODE: Record<string, TmsDicOpt[]> = {
  [TMS_DIC.yesNo]: [
    { id: '1', enCode: '1', fullName: '是' },
    { id: '0', enCode: '0', fullName: '否' },
  ],
  [TMS_DIC.enableStatus]: [
    { id: '1', enCode: '1', fullName: '启用' },
    { id: '0', enCode: '0', fullName: '停用' },
  ],
  [TMS_DIC.signRule]: [
    { id: 'scan', enCode: 'scan', fullName: '仅扫码' },
    { id: 'both', enCode: 'both', fullName: '扫码+在线' },
  ],
  [TMS_DIC.questionType]: [
    { id: 'single', enCode: 'single', fullName: '单选' },
    { id: 'multi', enCode: 'multi', fullName: '多选' },
    { id: 'judge', enCode: 'judge', fullName: '判断' },
    { id: 'blank', enCode: 'blank', fullName: '填空' },
    { id: 'essay', enCode: 'essay', fullName: '问答' },
  ],
  [TMS_DIC.difficulty]: [
    { id: 'easy', enCode: 'easy', fullName: '简单' },
    { id: 'normal', enCode: 'normal', fullName: '一般' },
    { id: 'hard', enCode: 'hard', fullName: '困难' },
  ],
  [TMS_DIC.questionSource]: [
    { id: 'self', enCode: 'self', fullName: '自命题' },
    { id: 'import', enCode: 'import', fullName: '导入' },
    { id: 'external', enCode: 'external', fullName: '外购' },
  ],
  [TMS_DIC.openClosedInvalid]: [
    { id: 'open', enCode: 'open', fullName: '开放' },
    { id: 'closed', enCode: 'closed', fullName: '不开放' },
    { id: 'invalid', enCode: 'invalid', fullName: '废弃' },
  ],
  [TMS_DIC.paperSortMode]: [
    { id: 'paper', enCode: 'paper', fullName: '试卷顺序' },
    { id: 'random', enCode: 'random', fullName: '随机' },
  ],
  [TMS_DIC.courseCategory]: [
    { id: 'gmp', enCode: 'gmp', fullName: 'GMP' },
    { id: 'sop', enCode: 'sop', fullName: 'SOP' },
    { id: 'safety', enCode: 'safety', fullName: '安全' },
    { id: 'skill', enCode: 'skill', fullName: '技能' },
    { id: 'other', enCode: 'other', fullName: '其他' },
  ],
  [TMS_DIC.trainMode]: [
    { id: 'onsite', enCode: 'onsite', fullName: '集中授课' },
    { id: 'practice', enCode: 'practice', fullName: '操作授课' },
    { id: 'online', enCode: 'online', fullName: '在线学习' },
  ],
  [TMS_DIC.evalMode]: [
    { id: 'quiz', enCode: 'quiz', fullName: '提问' },
    { id: 'practice', enCode: 'practice', fullName: '现场操作' },
    { id: 'exam', enCode: 'exam', fullName: '在线考试' },
    { id: 'none', enCode: 'none', fullName: '无需考核' },
  ],
  [TMS_DIC.archiveStatus]: [
    { id: 'pending', enCode: 'pending', fullName: '待归档' },
    { id: 'ready', enCode: 'ready', fullName: '可归档' },
    { id: 'archived', enCode: 'archived', fullName: '已归档' },
    { id: 'invalid', enCode: 'invalid', fullName: '无效' },
  ],
  [TMS_DIC.gradeStatus]: [
    { id: 'auto', enCode: 'auto', fullName: '无需阅卷' },
    { id: 'pending', enCode: 'pending', fullName: '待批改' },
    { id: 'graded', enCode: 'graded', fullName: '已批改' },
  ],
  [TMS_DIC.examStatus]: [
    { id: 'doing', enCode: 'doing', fullName: '考试中' },
    { id: 'submitted', enCode: 'submitted', fullName: '已交卷' },
    { id: 'cancelled', enCode: 'cancelled', fullName: '已取消' },
  ],
  [TMS_DIC.passFlag]: [
    { id: '1', enCode: '1', fullName: '合格' },
    { id: '0', enCode: '0', fullName: '不合格' },
  ],
  [TMS_DIC.taskPublishStatus]: [
    { id: 'draft', enCode: 'draft', fullName: '未发布' },
    { id: 'published', enCode: 'published', fullName: '已发布' },
    { id: 'cancelled', enCode: 'cancelled', fullName: '已取消' },
    { id: 'done', enCode: 'done', fullName: '已完成' },
  ],
  [TMS_DIC.taskCategory]: [
    { id: 'temp', enCode: 'temp', fullName: '临时培训' },
    { id: 'post_plan', enCode: 'post_plan', fullName: '岗位计划' },
    { id: 'annual_plan', enCode: 'annual_plan', fullName: '年度计划' },
    { id: 'file_effect', enCode: 'file_effect', fullName: '文件生效' },
    { id: 'out_train', enCode: 'out_train', fullName: '外派培训' },
    { id: 'retrain', enCode: 'retrain', fullName: '再培训' },
  ],
  [TMS_DIC.taskKind]: [
    { id: 'new', enCode: 'new', fullName: '新增' },
    { id: 'continue', enCode: 'continue', fullName: '继续' },
  ],
  [TMS_DIC.taskSourceType]: [
    { id: 'annual', enCode: 'annual', fullName: '年计划' },
    { id: 'post', enCode: 'post', fullName: '岗计划' },
    { id: 'temp', enCode: 'temp', fullName: '临时' },
    { id: 'file', enCode: 'file', fullName: '文件培训' },
  ],
  [TMS_DIC.personTaskStatus]: [
    { id: 'todo', enCode: 'todo', fullName: '未完成' },
    { id: 'signed', enCode: 'signed', fullName: '已签到' },
    { id: 'done', enCode: 'done', fullName: '已完成' },
    { id: 'expired', enCode: 'expired', fullName: '已过期' },
    { id: 'cancelled', enCode: 'cancelled', fullName: '已取消' },
  ],
};

export function labelOfDic(options: TmsDicOpt[], v?: string | null) {
  if (v == null || v === '') return '-';
  const hit = options.find((x) => x.enCode === v || x.id === v);
  return hit?.fullName || v;
}

type BaseStore = ReturnType<typeof useBaseStore>;

/** 统一字典入口：按编码拉取，失败时用内置兜底 */
export async function loadTmsDic(baseStore: BaseStore, code: string): Promise<TmsDicOpt[]> {
  const fallback = FALLBACK_BY_CODE[code] ?? [];
  try {
    const res = await baseStore.getDictionaryData(code);
    return Array.isArray(res) && res.length ? (res as TmsDicOpt[]) : fallback;
  } catch {
    return fallback;
  }
}
