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).max(1000),
  fullScore: z.coerce.number().min(1).max(1000).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).max(1000),
  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).max(1000).optional(),
  type: GradeRecordTypeEnum.optional(),
  semester: GradeRecordSemesterEnum.optional(),
  records: z.array(BatchGradeRecordItemSchema).max(500),
})

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).max(1000).optional(),
  fullScore: z.coerce.number().min(1).max(1000).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(),
  semester: GradeRecordSemesterEnum.optional(),
})

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

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

export const ExportGradesSchema = z.object({
  classId: z.string().min(1).optional(),
  studentId: z.string().min(1).optional(),
  subjectId: z.string().optional(),
  examId: z.string().optional(),
  semester: GradeRecordSemesterEnum.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(),
  studentId: z.string().optional(),
  examId: z.string().optional(),
  semester: GradeRecordSemesterEnum.optional(),
})

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

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

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

export const GradeDistributionByGradeQuerySchema = z.object({
  gradeId: z.string().min(1),
  subjectId: z.string().optional(),
  examId: z.string().optional(),
  semester: GradeRecordSemesterEnum.optional(),
})

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

/**
 * P3-4: 学生成长档案查询 Schema。
 * 仅需 studentId（学年/学期由 data-access 自动按学年聚合）。
 */
export const StudentGrowthArchiveQuerySchema = z.object({
  studentId: z.string().min(1),
})

/**
 * P3-10: Excel 批量导入成绩 Schema。
 *
 * Excel 模板列：
 * - 学生姓名 (studentName) - 必填
 * - 得分 (score) - 必填，0-1000
 * - 备注 (remark) - 选填
 */
export const ExcelImportRowSchema = z.object({
  studentName: z.string().min(1),
  score: z.coerce.number().min(0).max(1000),
  remark: z.string().optional(),
})

export const ExcelImportGradesSchema = z.object({
  classId: z.string().min(1),
  subjectId: z.string().min(1),
  title: z.string().min(1).max(255),
  examId: z.string().optional(),
  academicYearId: z.string().optional(),
  fullScore: z.coerce.number().min(1).max(1000).optional(),
  type: GradeRecordTypeEnum.optional(),
  semester: GradeRecordSemesterEnum.optional(),
  rows: z.array(ExcelImportRowSchema).min(1).max(500),
})

export type ExcelImportRowInput = z.infer<typeof ExcelImportRowSchema>
export type ExcelImportGradesInput = z.infer<typeof ExcelImportGradesSchema>

// --- 按试卷批量录入（每题得分）---

export const BatchGradeEntryByExamQuestionSchema = z.object({
  questionId: z.string().min(1),
  score: z.coerce.number().min(0).max(1000),
})

export const BatchGradeEntryByExamItemSchema = z.object({
  studentId: z.string().min(1),
  answers: z.array(BatchGradeEntryByExamQuestionSchema).min(1),
  remark: z.string().optional(),
})

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

export type BatchGradeEntryByExamInput = z.infer<typeof BatchGradeEntryByExamSchema>

// ============================================================================
// P3-7 成绩申诉 Schema
// ============================================================================

export const GradeAppealStatusEnum = z.enum([
  "pending",
  "approved",
  "rejected",
  "withdrawn",
])

/** 学生提交成绩申诉 */
export const CreateGradeAppealSchema = z.object({
  gradeRecordId: z.string().min(1),
  reason: z.string().min(10, "申诉理由至少 10 字").max(2000),
  expectedScore: z.coerce.number().min(0).max(1000).optional(),
})

export type CreateGradeAppealInput = z.infer<typeof CreateGradeAppealSchema>

/** 教师复核申诉 */
export const ReviewGradeAppealSchema = z.object({
  appealId: z.string().min(1),
  decision: z.enum(["approved", "rejected"]),
  reviewComment: z.string().min(1).max(2000),
  adjustedScore: z.coerce.number().min(0).max(1000).optional(),
})

export type ReviewGradeAppealInput = z.infer<typeof ReviewGradeAppealSchema>
