import { z } from "zod"

export const GradeRecordTypeEnum = z.enum(["exam", "quiz", "homework", "other"])
export const GradeRecordSemesterEnum = z.enum(["1", "2"])

export const CreateGradeRecordSchema = z.object({
  studentId: z.string().min(1),
  classId: z.string().min(1),
  subjectId: z.string().min(1),
  examId: z.string().optional(),
  academicYearId: z.string().optional(),
  title: z.string().min(1).max(255),
  score: z.coerce.number().min(0),
  fullScore: z.coerce.number().min(1).optional(),
  type: GradeRecordTypeEnum.optional(),
  semester: GradeRecordSemesterEnum.optional(),
  remark: z.string().optional(),
})

export type CreateGradeRecordInput = z.infer<typeof CreateGradeRecordSchema>

export const BatchGradeRecordItemSchema = z.object({
  studentId: z.string().min(1),
  score: z.coerce.number().min(0),
  remark: z.string().optional(),
})

export const BatchCreateGradeRecordSchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().min(1),
  examId: z.string().optional(),
  academicYearId: z.string().optional(),
  title: z.string().min(1).max(255),
  fullScore: z.coerce.number().min(1).optional(),
  type: GradeRecordTypeEnum.optional(),
  semester: GradeRecordSemesterEnum.optional(),
  records: z.array(BatchGradeRecordItemSchema),
})

export type BatchCreateGradeRecordInput = z.infer<typeof BatchCreateGradeRecordSchema>

export const UpdateGradeRecordSchema = z.object({
  title: z.string().min(1).max(255).optional(),
  score: z.coerce.number().min(0).optional(),
  fullScore: z.coerce.number().min(1).optional(),
  type: GradeRecordTypeEnum.optional(),
  semester: GradeRecordSemesterEnum.optional(),
  remark: z.string().optional(),
  examId: z.string().optional(),
})

export type UpdateGradeRecordInput = z.infer<typeof UpdateGradeRecordSchema>

// --- 查询/分析相关 Schema（P1-3 新增：为缺失 Zod 校验的 Action 补齐） ---

export const DeleteGradeRecordSchema = z.object({
  id: z.string().min(1),
})

export const GetGradeRecordByIdSchema = z.object({
  id: z.string().min(1),
})

export const GradeQuerySchema = z.object({
  classId: z.string().optional(),
  subjectId: z.string().optional(),
  studentId: z.string().optional(),
  type: GradeRecordTypeEnum.optional(),
  semester: GradeRecordSemesterEnum.optional(),
  examId: z.string().optional(),
})

export type GradeQueryInput = z.infer<typeof GradeQuerySchema>

export const ClassGradeStatsQuerySchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().optional(),
  examId: z.string().optional(),
})

export const StudentGradeSummaryQuerySchema = z.object({
  studentId: z.string().min(1),
})

export const ClassRankingQuerySchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().optional(),
  examId: z.string().optional(),
})

export const ExportGradesSchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().optional(),
  examId: z.string().optional(),
  reportType: z.enum(["detail", "class"]).optional(),
})

export type ExportGradesInput = z.infer<typeof ExportGradesSchema>

export const GradeTrendQuerySchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().optional(),
})

export const ClassComparisonQuerySchema = z.object({
  gradeId: z.string().min(1),
  subjectId: z.string().min(1),
})

export const SubjectComparisonQuerySchema = z.object({
  classId: z.string().min(1),
})

export const GradeDistributionQuerySchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().optional(),
})

export const RankingTrendQuerySchema = z.object({
  studentId: z.string().min(1),
  subjectId: z.string().optional(),
  semester: GradeRecordSemesterEnum.optional(),
})
