// Define types based on Drizzle Schema
// In a real app, we would infer these from the schema, but since we might not have the full schema setup running locally with DB,
// we will define interfaces that match the schema description in ARCHITECTURE.md and schema.ts

// Note: Input types (CreateTextbookInput, UpdateTextbookInput, CreateChapterInput,
// UpdateChapterContentInput, CreateKnowledgePointInput, UpdateKnowledgePointInput)
// are defined in ./schema.ts alongside their Zod validation schemas.

export type Textbook = {
  id: string;
  title: string;
  subject: string;
  grade: string | null;
  publisher: string | null;
  createdAt: Date;
  updatedAt: Date;
  // Computed/Joined fields
  _count?: {
    chapters: number;
  };
};

export type Chapter = {
  id: string;
  textbookId: string;
  title: string;
  order: number | null;
  parentId: string | null;
  content?: string | null; // Added for content viewing
  createdAt: Date;
  updatedAt: Date;
  // Recursive structure for UI
  children?: Chapter[];
};

export type KnowledgePoint = {
  id: string;
  name: string;
  description?: string | null;
  anchorText?: string | null;
  parentId?: string | null;
  chapterId?: string; // Logic link for this module context
  level: number;
  order: number;
};

// ===== 知识图谱相关类型 =====

/** 图谱视图模式（数据视角：结构 / 学生掌握度 / 班级掌握度） */
export type GraphViewMode = "structure" | "student-mastery" | "class-mastery"

/** 图谱布局模式（渲染方式：分层有向图 / Obsidian 风格力导向图） */
export type GraphLayoutMode = "hierarchical" | "force"

/** 掌握度信息 */
export interface MasteryInfo {
  /** 掌握度等级 0-100 */
  masteryLevel: number
  /** 总题数 */
  totalQuestions: number
  /** 正确题数 */
  correctQuestions: number
  /** 最后测评时间 */
  lastAssessedAt: Date
}

/** 带关联关系的知识点（图谱数据） */
export interface KpWithRelations {
  id: string
  name: string
  description: string | null
  parentId: string | null
  chapterId: string | null
  level: number
  order: number
  /** 关联题目数 */
  questionCount: number
  /** 前置知识点 ID 列表 */
  prerequisiteIds: string[]
  /** 章节标题（用于节点 tooltip） */
  chapterTitle: string | null
}

/** 图谱节点数据（React Flow Node.data） */
export interface GraphNodeData {
  kp: KpWithRelations
  mastery: MasteryInfo | null
  viewMode: GraphViewMode
  isSelected: boolean
  isHighlighted: boolean
  chapterColor: string
}

/** 图谱边数据（React Flow Edge.data） */
export interface GraphEdgeData {
  edgeType: "parent" | "prerequisite"
  isHighlighted: boolean
}

/** 图谱完整数据（Server Action 返回） */
export interface KnowledgeGraphData {
  knowledgePoints: KpWithRelations[]
  /** mastery map，key = kpId（仅 mastery 模式下有值） */
  masteryMap: Record<string, MasteryInfo>
  viewMode: GraphViewMode
}

/** 掌握度色彩等级 */
export type MasteryLevel = "low" | "medium" | "high" | "unassessed"
