/**
 * 课表纯函数工具集（P0-4 审计修复 + P2-4 可测试性抽取）。
 *
 * 修复点：
 * - 原 `getSubjectColor` 使用英文关键词（'math'/'english'）匹配科目，
 *   但 `DEFAULT_CLASS_SUBJECTS` 为中文（"数学"/"英语"），导致所有中文科目
 *   落到 default 分支，颜色视觉分组完全失效。
 * - 现支持中英文双语匹配，并导出为纯函数便于单测。
 */

/** 科目到颜色类的映射表（中英文关键词均匹配）。 */
const SUBJECT_COLOR_MAP: ReadonlyArray<{ keywords: readonly string[]; classes: string }> = [
  {
    keywords: ["math", "数学"],
    classes: "bg-blue-500/10 text-blue-700 border-blue-500/20 hover:bg-blue-500/20",
  },
  {
    keywords: ["physics", "物理", "science", "科学"],
    classes: "bg-purple-500/10 text-purple-700 border-purple-500/20 hover:bg-purple-500/20",
  },
  {
    keywords: ["english", "英语", "lit"],
    classes: "bg-amber-500/10 text-amber-700 border-amber-500/20 hover:bg-amber-500/20",
  },
  {
    keywords: ["history", "历史", "geo", "地理", "社会"],
    classes: "bg-orange-500/10 text-orange-700 border-orange-500/20 hover:bg-orange-500/20",
  },
  {
    keywords: ["art", "美术", "music", "音乐"],
    classes: "bg-pink-500/10 text-pink-700 border-pink-500/20 hover:bg-pink-500/20",
  },
  {
    keywords: ["sport", "pe", "体育"],
    classes: "bg-emerald-500/10 text-emerald-700 border-emerald-500/20 hover:bg-emerald-500/20",
  },
  {
    keywords: ["chinese", "语文", "language"],
    classes: "bg-rose-500/10 text-rose-700 border-rose-500/20 hover:bg-rose-500/20",
  },
] as const

const DEFAULT_SUBJECT_COLOR = "bg-primary/10 text-primary border-primary/20 hover:bg-primary/20"

/**
 * 根据科目名称返回对应的颜色类名（支持中英文）。
 * 匹配规则：科目名转为小写后，检查是否包含映射表中的任一关键词。
 */
export function getSubjectColor(subject: string): string {
  const s = subject.toLowerCase()
  for (const entry of SUBJECT_COLOR_MAP) {
    if (entry.keywords.some((kw) => s.includes(kw.toLowerCase()))) {
      return entry.classes
    }
  }
  return DEFAULT_SUBJECT_COLOR
}

/** 课表时间范围：8:00 - 18:00。 */
const MIN_TIME_MINUTES = 8 * 60
const MAX_TIME_MINUTES = 18 * 60
const TOTAL_DURATION_MINUTES = MAX_TIME_MINUTES - MIN_TIME_MINUTES

/**
 * 将 "HH:MM" 时间字符串转换为当天从 0 点起的分钟数。
 */
export function timeToMinutes(time: string): number {
  const parts = time.split(":").map(Number)
  const hours = parts[0] ?? 0
  const minutes = parts[1] ?? 0
  return hours * 60 + minutes
}

/**
 * 根据课程的开始/结束时间计算课表块在时间轴上的定位样式（top% / height%）。
 * 时间轴范围：8:00 - 18:00。
 */
export function getPositionStyle(startTime: string, endTime: string): { top: string; height: string } {
  const startMinutes = timeToMinutes(startTime)
  const endMinutes = timeToMinutes(endTime)

  const top = Math.max(0, ((startMinutes - MIN_TIME_MINUTES) / TOTAL_DURATION_MINUTES) * 100)
  const height = Math.min(100 - top, ((endMinutes - startMinutes) / TOTAL_DURATION_MINUTES) * 100)

  return {
    top: `${top}%`,
    height: `${height}%`,
  }
}

/** 课表周常量（周一~周日），label 为 i18n 翻译键。 */
export const SCHEDULE_WEEKDAYS: ReadonlyArray<{ key: 1 | 2 | 3 | 4 | 5 | 6 | 7; label: string }> = [
  { key: 1, label: "schedule.weekday.1" },
  { key: 2, label: "schedule.weekday.2" },
  { key: 3, label: "schedule.weekday.3" },
  { key: 4, label: "schedule.weekday.4" },
  { key: 5, label: "schedule.weekday.5" },
  { key: 6, label: "schedule.weekday.6" },
  { key: 7, label: "schedule.weekday.7" },
]
