import { z } from "zod"

/** 错题来源类型 */
export const ErrorBookSourceTypeEnum = z.enum(["exam", "homework", "manual"])
export type ErrorBookSourceType = z.infer<typeof ErrorBookSourceTypeEnum>

/** 错题状态 */
export const ErrorBookStatusEnum = z.enum(["new", "learning", "mastered", "archived"])
export type ErrorBookStatus = z.infer<typeof ErrorBookStatusEnum>

/** 复习自评结果（SM-2 算法评级） */
export const ErrorBookReviewResultEnum = z.enum(["again", "hard", "good", "easy"])
export type ErrorBookReviewResult = z.infer<typeof ErrorBookReviewResultEnum>

/** 手动添加错题的输入 schema */
export const CreateErrorBookItemSchema = z.object({
  questionId: z.string().min(1, "请选择题目"),
  studentAnswer: z.unknown().optional(),
  correctAnswer: z.unknown().optional(),
  subjectId: z.string().optional(),
  knowledgePointIds: z.array(z.string()).optional(),
  note: z.string().max(2000, "笔记不能超过 2000 字").optional(),
  errorTags: z.array(z.string()).optional(),
})

export type CreateErrorBookItemInput = z.infer<typeof CreateErrorBookItemSchema>

/** 更新错题笔记的 schema */
export const UpdateErrorBookNoteSchema = z.object({
  itemId: z.string().min(1),
  note: z.string().max(2000, "笔记不能超过 2000 字").optional(),
  errorTags: z.array(z.string()).optional(),
})

export type UpdateErrorBookNoteInput = z.infer<typeof UpdateErrorBookNoteSchema>

/** 记录复习结果的 schema */
export const ReviewErrorBookItemSchema = z.object({
  itemId: z.string().min(1),
  result: ErrorBookReviewResultEnum,
})

export type ReviewErrorBookItemInput = z.infer<typeof ReviewErrorBookItemSchema>

/** 从考试/作业提交自动采集错题的 schema */
export const CollectFromSubmissionSchema = z.object({
  submissionId: z.string().min(1),
  sourceType: z.enum(["exam", "homework"]),
})

export type CollectFromSubmissionInput = z.infer<typeof CollectFromSubmissionSchema>
