/**
 * 班级模块类型定义。
 *
 * 说明（P1-4 审计决策）：
 * 下列 `ClassHomeworkInsights` / `GradeHomeworkInsights` / `ClassHomeworkAssignmentStats`
 * / `ScoreStats` / `AssignmentSummary` 等类型虽涉及 homework 概念，但它们是
 * **classes 模块对 homework 数据的视图**（按班级/年级聚合的作业统计），由
 * `data-access-stats.ts` 产出并被 classes 组件消费。homework 模块自身的
 * `types.ts` 定义的是作业实体类型（HomeworkAssignmentStatus 等），不包含这些聚合视图类型。
 * 因此将这些类型保留在 classes 模块，避免让 homework 模块承担 classes 视角的类型定义职责。
 */

export type TeacherClass = {
  id: string
  schoolName?: string | null
  name: string
  grade: string
  homeroom?: string | null
  room?: string | null
  invitationCode?: string | null
  studentCount: number
  recentAssignments?: AssignmentSummary[]
  schedule?: ClassScheduleItem[]
}

export interface AssignmentSummary {
  id: string
  title: string
  status: string
  subject?: string | null
  isActive: boolean
  isOverdue: boolean
  dueAt: Date | null
  submittedCount: number
  targetCount: number
  avgScore: number | null
  medianScore: number | null
}

export type TeacherOption = {
  id: string
  name: string
  email: string
}

export const DEFAULT_CLASS_SUBJECTS = ["语文", "数学", "英语", "美术", "体育", "科学", "社会", "音乐"] as const

export type ClassSubject = (typeof DEFAULT_CLASS_SUBJECTS)[number]

export type ClassSubjectTeacherAssignment = {
  subject: ClassSubject
  teacher: TeacherOption | null
}

export type AdminClassListItem = {
  id: string
  schoolName?: string | null
  schoolId?: string | null
  name: string
  grade: string
  gradeId?: string | null
  homeroom?: string | null
  room?: string | null
  invitationCode?: string | null
  teacher: TeacherOption
  subjectTeachers: ClassSubjectTeacherAssignment[]
  studentCount: number
  createdAt: string
  updatedAt: string
}

export type CreateTeacherClassInput = {
  schoolName?: string | null
  schoolId?: string | null
  name: string
  grade: string
  gradeId?: string | null
  homeroom?: string | null
  room?: string | null
}

export type UpdateTeacherClassInput = {
  schoolName?: string | null
  schoolId?: string | null
  name?: string
  grade?: string
  gradeId?: string | null
  homeroom?: string | null
  room?: string | null
}

export type ClassStudent = {
  id: string
  name: string
  email: string
  image?: string | null
  gender?: string | null
  classId: string
  className: string
  status: "active" | "inactive"
  joinedAt: Date
  subjectScores?: Record<string, number | null>
}

export type ClassScheduleItem = {
  id: string
  classId: string
  weekday: 1 | 2 | 3 | 4 | 5 | 6 | 7
  startTime: string
  endTime: string
  course: string
  location?: string | null
}

export type StudentEnrolledClass = {
  id: string
  schoolName?: string | null
  name: string
  grade: string
  homeroom?: string | null
  room?: string | null
  teacherName?: string | null
  teacherEmail?: string | null
}

export type StudentScheduleItem = {
  id: string
  classId: string
  className: string
  weekday: 1 | 2 | 3 | 4 | 5 | 6 | 7
  startTime: string
  endTime: string
  course: string
  location?: string | null
}

export type ScoreStats = {
  count: number
  avg: number | null
  median: number | null
  min: number | null
  max: number | null
}

export type ClassHomeworkAssignmentStats = {
  assignmentId: string
  title: string
  status: string
  subject?: string | null
  createdAt: string
  dueAt: string | null
  isActive: boolean
  isOverdue: boolean
  maxScore: number
  targetCount: number
  submittedCount: number
  gradedCount: number
  scoreStats: ScoreStats
}

export type ClassHomeworkInsights = {
  class: {
    id: string
    schoolName?: string | null
    schoolId?: string | null
    name: string
    grade: string
    homeroom?: string | null
    room?: string | null
    invitationCode?: string | null
  }
  studentCounts: { total: number; active: number; inactive: number }
  assignments: ClassHomeworkAssignmentStats[]
  latest: ClassHomeworkAssignmentStats | null
  overallScores: ScoreStats
}

export type GradeHomeworkClassSummary = {
  class: { id: string; name: string; grade: string; homeroom?: string | null; room?: string | null }
  studentCounts: { total: number; active: number; inactive: number }
  latestAvg: number | null
  prevAvg: number | null
  deltaAvg: number | null
  overallScores: ScoreStats
}

export type GradeHomeworkInsights = {
  grade: { id: string; name: string; school: { id: string; name: string } }
  classCount: number
  studentCounts: { total: number; active: number; inactive: number }
  assignments: ClassHomeworkAssignmentStats[]
  latest: ClassHomeworkAssignmentStats | null
  overallScores: ScoreStats
  classes: GradeHomeworkClassSummary[]
}
