import { z } from "zod"

// ---------------------------------------------------------------------------
// 枚举验证
// ---------------------------------------------------------------------------

export const PracticeTypeSchema = z.enum([
  "error_variant",
  "knowledge_point",
  "weak_chapter",
  "ai_recommended",
])

export const PracticeAnswerStatusSchema = z.enum(["pending", "answered", "skipped"])

// ---------------------------------------------------------------------------
// 来源元数据验证（判别式联合类型，替代 z.record(z.string(), z.unknown())）
// ---------------------------------------------------------------------------

/**
 * 错题重做练习来源。
 *
 * practiceType="error_variant" 时使用。
 */
export const ErrorVariantSourceMetaSchema = z.object({
  errorBookItemIds: z.array(z.string().min(1)).min(1),
  sourceQuestionIds: z.array(z.string().min(1)).min(1),
})

/**
 * 知识点专项练习来源。
 *
 * practiceType="knowledge_point" 时使用。
 */
export const KnowledgePointSourceMetaSchema = z.object({
  knowledgePointIds: z.array(z.string().min(1)).min(1),
  difficulty: z.number().int().min(1).max(5).optional(),
})

/**
 * 薄弱章节练习来源。
 *
 * practiceType="weak_chapter" 时使用。
 * chapterId 可选——未传时跨所有章节自动识别薄弱知识点。
 * weakKnowledgePointIds 可选——未传时由后端自动识别薄弱知识点。
 */
export const WeakChapterSourceMetaSchema = z.object({
  chapterId: z.string().min(1).optional(),
  weakKnowledgePointIds: z.array(z.string().min(1)).min(1).optional(),
})

/**
 * AI 推荐练习来源。
 *
 * practiceType="ai_recommended" 时使用。
 * reason 为枚举字符串（业务数据，不应直接存翻译文本）。
 */
export const AiRecommendedReasonSchema = z.enum([
  "student_initiated",
  "teacher_assigned",
  "parent_suggested",
])

export const AiRecommendedSourceMetaSchema = z.object({
  recommendedKnowledgePointIds: z.array(z.string().min(1)).min(1),
  reason: AiRecommendedReasonSchema,
})

/**
 * 判别式联合类型 schema。
 *
 * 通过 practiceType 字段判别，确保 sourceMeta 结构与练习类型匹配。
 * 替代原先的 z.record(z.string(), z.unknown()) 弱类型校验。
 */
export const PracticeSourceMetaSchema = z.discriminatedUnion("practiceType", [
  z.object({
    practiceType: z.literal("error_variant"),
    sourceMeta: ErrorVariantSourceMetaSchema,
  }),
  z.object({
    practiceType: z.literal("knowledge_point"),
    sourceMeta: KnowledgePointSourceMetaSchema,
  }),
  z.object({
    practiceType: z.literal("weak_chapter"),
    sourceMeta: WeakChapterSourceMetaSchema,
  }),
  z.object({
    practiceType: z.literal("ai_recommended"),
    sourceMeta: AiRecommendedSourceMetaSchema,
  }),
])

// ---------------------------------------------------------------------------
// Action 输入验证
// ---------------------------------------------------------------------------

export const CreatePracticeSessionSchema = z.object({
  practiceType: PracticeTypeSchema,
  subjectId: z.string().min(1).optional(),
  sourceMeta: z.record(z.string(), z.unknown()),
  questionCount: z.number().int().min(1).max(50).default(10),
})

export const SubmitPracticeAnswerSchema = z.object({
  sessionId: z.string().min(1),
  answerId: z.string().min(1),
  answer: z.unknown(),
  /** 跳过此题 */
  skip: z.boolean().optional(),
})

export const CompletePracticeSessionSchema = z.object({
  sessionId: z.string().min(1),
})

export const AbandonPracticeSessionSchema = z.object({
  sessionId: z.string().min(1),
})

// ---------------------------------------------------------------------------
// 类型导出
// ---------------------------------------------------------------------------

export type CreatePracticeSessionInput = z.infer<typeof CreatePracticeSessionSchema>
export type SubmitPracticeAnswerInput = z.infer<typeof SubmitPracticeAnswerSchema>
export type CompletePracticeSessionInput = z.infer<typeof CompletePracticeSessionSchema>
export type AbandonPracticeSessionInput = z.infer<typeof AbandonPracticeSessionSchema>
export type AiRecommendedReason = z.infer<typeof AiRecommendedReasonSchema>
