import type { StatusVariantMap, StatusLabelMap } from "@/shared/components/ui/status-badge"
import type { ErrorBookSourceType, ErrorBookStatus, ErrorBookReviewResult } from "./schema"

export type ErrorBookSourceTypeValue = ErrorBookSourceType
export type ErrorBookStatusValue = ErrorBookStatus
export type ErrorBookReviewResultValue = ErrorBookReviewResult

/** 错题来源 → Badge variant 映射 */
export const ERROR_BOOK_SOURCE_VARIANT: StatusVariantMap<ErrorBookSourceTypeValue> = {
  exam: "default",
  homework: "secondary",
  manual: "outline",
}

/** 错题来源 → 展示文本映射 */
export const ERROR_BOOK_SOURCE_LABEL: StatusLabelMap<ErrorBookSourceTypeValue> = {
  exam: "考试",
  homework: "作业",
  manual: "手动添加",
}

/** 错题状态 → Badge variant 映射 */
export const ERROR_BOOK_STATUS_VARIANT: StatusVariantMap<ErrorBookStatusValue> = {
  new: "secondary",
  learning: "default",
  mastered: "outline",
  archived: "outline",
}

/** 错题状态 → 展示文本映射 */
export const ERROR_BOOK_STATUS_LABEL: StatusLabelMap<ErrorBookStatusValue> = {
  new: "待学习",
  learning: "学习中",
  mastered: "已掌握",
  archived: "已归档",
}

/** 复习自评结果 → Badge variant 映射 */
export const REVIEW_RESULT_VARIANT: StatusVariantMap<ErrorBookReviewResultValue> = {
  again: "destructive",
  hard: "secondary",
  good: "default",
  easy: "outline",
}

/** 复习自评结果 → 展示文本映射 */
export const REVIEW_RESULT_LABEL: StatusLabelMap<ErrorBookReviewResultValue> = {
  again: "重来",
  hard: "困难",
  good: "良好",
  easy: "简单",
}

/** 常见错误原因标签 */
export const COMMON_ERROR_TAGS = [
  "概念不清",
  "计算错误",
  "粗心大意",
  "审题不清",
  "方法不当",
  "记忆错误",
  "时间不足",
] as const

/** 错题本列表条目 */
export interface ErrorBookItem {
  id: string
  studentId: string
  questionId: string
  sourceType: ErrorBookSourceTypeValue
  sourceId: string | null
  studentAnswer: unknown
  correctAnswer: unknown
  subjectId: string | null
  knowledgePointIds: string[] | null
  status: ErrorBookStatusValue
  masteryLevel: number
  nextReviewAt: Date | null
  reviewInterval: number
  reviewCount: number
  correctStreak: number
  note: string | null
  errorTags: string[] | null
  createdAt: Date
  updatedAt: Date
  /** 关联题目（联表查询） */
  question: {
    id: string
    content: unknown
    type: string
    difficulty: number | null
  } | null
  /** 关联学科名称 */
  subjectName: string | null
}

/** 错题本列表查询参数 */
export type GetErrorBookItemsParams = {
  studentId: string
  q?: string
  page?: number
  pageSize?: number
  status?: ErrorBookStatusValue
  sourceType?: ErrorBookSourceTypeValue
  subjectId?: string
  /** 仅查询到期需要复习的条目 */
  dueOnly?: boolean
}

/** 错题本列表结果 */
export type ErrorBookListResult = {
  data: ErrorBookItem[]
  meta: {
    page: number
    pageSize: number
    total: number
    totalPages: number
  }
}

/** 错题详情（含复习历史） */
export interface ErrorBookItemDetail extends ErrorBookItem {
  reviews: ErrorBookReviewRecord[]
}

/** 复习记录 */
export interface ErrorBookReviewRecord {
  id: string
  result: ErrorBookReviewResultValue
  reviewedAt: Date
  newInterval: number | null
  newMasteryLevel: number | null
}

/** 错题本统计概览 */
export interface ErrorBookStats {
  totalCount: number
  newCount: number
  learningCount: number
  masteredCount: number
  archivedCount: number
  dueReviewCount: number
  masteredRate: number
}

/** 知识点薄弱度统计 */
export interface KnowledgePointWeakness {
  knowledgePointId: string
  knowledgePointName: string
  errorCount: number
  masteredCount: number
  totalCount: number
  /** 掌握率 0-1 */
  masteryRate: number
  /** 所属章节 ID（增强：关联章节维度） */
  chapterId: string | null
  /** 所属章节标题 */
  chapterTitle: string | null
}

/** 学科错题分布 */
export interface SubjectErrorDistribution {
  subjectId: string | null
  subjectName: string
  errorCount: number
  masteredCount: number
  masteryRate: number
}

/** 班级学生错题统计（教师视图） */
export interface StudentErrorBookSummary {
  studentId: string
  totalCount: number
  newCount: number
  learningCount: number
  masteredCount: number
  dueReviewCount: number
  masteredRate: number
  lastActivityAt: Date | null
  /** 所属班级 ID（增强：支持按班级分组展示） */
  classId: string | null
  /** 所属班级名称 */
  className: string | null
}

/** 班级错题统计（教师视图） */
export interface ClassErrorBookStats {
  totalStudents: number
  studentsWithErrorBook: number
  totalErrorItems: number
  averageErrorPerStudent: number
  averageMasteryRate: number
  topWeakKnowledgePoints: KnowledgePointWeakness[]
  subjectDistribution: SubjectErrorDistribution[]
  topStudents: StudentErrorBookSummary[]
}

/** 章节薄弱度统计（哪些课在错） */
export interface ChapterWeakness {
  chapterId: string
  chapterTitle: string
  /** 该章节下的错题总数 */
  errorCount: number
  /** 该章节下已掌握的错题数 */
  masteredCount: number
  /** 该章节下涉及的知识点数 */
  knowledgePointCount: number
  /** 掌握率 0-1 */
  masteryRate: number
  /** 该章节下错得最多的知识点（前 3 个） */
  topKnowledgePoints: Array<{
    knowledgePointId: string
    knowledgePointName: string
    errorCount: number
  }>
}

/** 班级错题概览（按班级分组统计） */
export interface ClassErrorOverview {
  classId: string
  className: string
  studentCount: number
  totalErrorItems: number
  averageErrorPerStudent: number
  averageMasteryRate: number
  dueReviewCount: number
}

/** 学科错题概览（按学科分组统计，用于学科 Tab） */
export interface SubjectErrorOverview {
  subjectId: string
  subjectName: string
  totalErrorItems: number
  studentCount: number
  averageMasteryRate: number
  dueReviewCount: number
}

/** 错题趋势数据点 */
export interface ErrorBookTrendPoint {
  date: string
  addedCount: number
  masteredCount: number
  reviewedCount: number
}
