export type GradeRecordType = "exam" | "quiz" | "homework" | "other"
export type GradeRecordSemester = "1" | "2"

export interface GradeRecord {
  id: string
  studentId: string
  classId: string
  subjectId: string
  examId: string | null
  academicYearId: string | null
  title: string
  score: string
  fullScore: string
  type: GradeRecordType
  semester: GradeRecordSemester
  recordedBy: string
  remark: string | null
  createdAt: string
  updatedAt: string
}

export interface GradeRecordListItem {
  id: string
  studentId: string
  studentName: string
  classId: string
  className: string
  subjectId: string
  subjectName: string
  examId: string | null
  title: string
  score: number
  fullScore: number
  type: GradeRecordType
  semester: GradeRecordSemester
  recordedBy: string
  recorderName: string
  remark: string | null
  createdAt: string
}

export interface GradeStats {
  average: number
  median: number
  max: number
  min: number
  stdDev: number
  passRate: number
  excellentRate: number
  count: number
}

export interface ClassGradeStats {
  classId: string
  className: string
  stats: GradeStats
  studentCount: number
}

export interface StudentGradeSummary {
  studentId: string
  studentName: string
  records: GradeRecordListItem[]
  averageScore: number
  rank: number
}

export interface ClassRankingItem {
  studentId: string
  studentName: string
  averageScore: number
  rank: number
  recordCount: number
}

export interface GradeQueryParams {
  classId?: string
  subjectId?: string
  studentId?: string
  type?: GradeRecordType
  semester?: GradeRecordSemester
  examId?: string
}

// --- Analytics Types ---

export interface GradeTrendPoint {
  /** ISO date string of the grade record creation */
  date: string
  /** Title of the exam/assessment */
  title: string
  /** Raw score */
  score: number
  /** Full score for this record */
  fullScore: number
  /** Score normalized to 0-100 scale for cross-record comparison */
  normalizedScore: number
  /** Type of grade record */
  type: GradeRecordType
}

export interface GradeTrendResult {
  /** Label for the trend series (e.g. class name + subject) */
  label: string
  /** Sorted ascending by date */
  points: GradeTrendPoint[]
  /** Average of normalized scores */
  averageScore: number
}

export interface ClassComparisonItem {
  classId: string
  className: string
  /** Average score (normalized to 0-100) */
  averageScore: number
  /** Median score */
  medianScore: number
  /** Pass rate (score/fullScore >= 0.6) */
  passRate: number
  /** Excellent rate (score/fullScore >= 0.85) */
  excellentRate: number
  /** Number of grade records */
  count: number
  /** Number of unique students */
  studentCount: number
}

export interface SubjectComparisonItem {
  subjectId: string
  subjectName: string
  /** Average normalized score (0-100) */
  averageScore: number
  /** Median normalized score */
  medianScore: number
  /** Pass rate */
  passRate: number
  /** Excellent rate */
  excellentRate: number
  /** Number of records */
  count: number
}

export interface GradeDistributionBucket {
  /** Bucket label e.g. "90-100" */
  label: string
  /** Lower bound (inclusive) */
  min: number
  /** Upper bound (inclusive) */
  max: number
  /** Number of students in this bucket */
  count: number
}

export interface GradeDistributionResult {
  buckets: GradeDistributionBucket[]
  totalCount: number
}

export interface RankingTrendPoint {
  /** Title of the exam/assessment */
  title: string
  /** ISO date string */
  date: string
  /** Student's average score (normalized) */
  score: number
  /** Rank in class for this assessment (1-based) */
  rank: number
  /** Total students participating */
  totalStudents: number
}

export interface RankingTrendResult {
  studentId: string
  studentName: string
  points: RankingTrendPoint[]
}
