import { z } from "zod"

export const CreateTextbookSchema = z.object({
  title: z.string().min(1).max(255),
  subject: z.string().min(1).max(100),
  grade: z.string().min(1).max(100),
  publisher: z.string().max(255).optional().default(""),
})

export type CreateTextbookInput = z.infer<typeof CreateTextbookSchema>

export const UpdateTextbookSchema = z.object({
  id: z.string().min(1),
  title: z.string().min(1).max(255),
  subject: z.string().min(1).max(100),
  grade: z.string().min(1).max(100),
  publisher: z.string().max(255).optional().default(""),
})

export type UpdateTextbookInput = z.infer<typeof UpdateTextbookSchema>

export const CreateChapterSchema = z.object({
  textbookId: z.string().min(1),
  title: z.string().min(1).max(255),
  parentId: z.string().optional(),
  order: z.coerce.number().int().min(0).optional(),
})

export type CreateChapterInput = z.infer<typeof CreateChapterSchema>

export const UpdateChapterContentSchema = z.object({
  chapterId: z.string().min(1),
  content: z.string(),
})

export type UpdateChapterContentInput = z.infer<typeof UpdateChapterContentSchema>

export const CreateKnowledgePointSchema = z.object({
  name: z.string().min(1).max(255),
  description: z.string().optional(),
  anchorText: z.string().optional(),
  chapterId: z.string().optional(),
  parentId: z.string().optional(),
})

export type CreateKnowledgePointInput = z.infer<typeof CreateKnowledgePointSchema>

export const UpdateKnowledgePointSchema = z.object({
  id: z.string().min(1),
  name: z.string().min(1).max(255),
  description: z.string().optional(),
  anchorText: z.string().optional(),
})

export type UpdateKnowledgePointInput = z.infer<typeof UpdateKnowledgePointSchema>

export const CreatePrerequisiteSchema = z.object({
  knowledgePointId: z.string().min(1),
  prerequisiteKpId: z.string().min(1),
}).refine((data) => data.knowledgePointId !== data.prerequisiteKpId, {
  // Zod message 仅为开发期提示，Action 层返回的 t("invalidInput") 是用户可见文案
  message: "A knowledge point cannot be a prerequisite of itself",
})

export type CreatePrerequisiteInput = z.infer<typeof CreatePrerequisiteSchema>

export const DeletePrerequisiteSchema = z.object({
  knowledgePointId: z.string().min(1),
  prerequisiteKpId: z.string().min(1),
})

export type DeletePrerequisiteInput = z.infer<typeof DeletePrerequisiteSchema>
