import { knowledgeAreas } from '@/data'
import { normalizeAnswer } from '@/utils/practice'

export interface TailoringPracticeItem {
  id: string
  knowledgeAreaId: string
  knowledgeAreaName: string
  knowledgeAreaColor: string
  knowledgeAreaOrder: number
  factorIndex: number
  title: string
  description: string
  normalizedAnswer: string
}

/**
 * 生成敏捷裁剪因素练习序列
 * 顺序：按知识领域排序，再按该知识领域中的裁剪因素原始顺序展开
 */
export function generateTailoringPracticeItems(): TailoringPracticeItem[] {
  const sortedKnowledgeAreas = [...knowledgeAreas].sort((a, b) => a.order - b.order)
  const items: TailoringPracticeItem[] = []

  sortedKnowledgeAreas.forEach((knowledgeArea) => {
    knowledgeArea.tailoringFactors?.forEach((factor, index) => {
      items.push({
        id: `${knowledgeArea.id}-factor-${index + 1}`,
        knowledgeAreaId: knowledgeArea.id,
        knowledgeAreaName: knowledgeArea.name,
        knowledgeAreaColor: knowledgeArea.color,
        knowledgeAreaOrder: knowledgeArea.order,
        factorIndex: index,
        title: factor.title,
        description: factor.description,
        normalizedAnswer: normalizeAnswer(factor.title, false),
      })
    })
  })

  return items
}
