// 课案状态
export type LessonPlanStatus = "draft" | "published" | "archived";

// Block 类型枚举
export type BlockType =
  | "objective"
  | "key_point"
  | "import"
  | "new_teaching"
  | "consolidation"
  | "summary"
  | "homework"
  | "blackboard"
  | "text_study"
  | "exercise"
  | "rich_text"
  | "reflection";

// 富文本类 block 的 data
export interface RichTextBlockData {
  html: string;
  knowledgePointIds: string[];
}

// 文本研习 block 的 data
export interface TextStudyAnnotation {
  id: string;
  anchor: { start: number; end: number };
  nodeType: string;
  title: string;
  note: string;
  color: "yellow" | "green";
}

export interface TextStudyBlockData {
  sourceText: string;
  annotations: TextStudyAnnotation[];
  knowledgePointIds: string[];
}

// 练习 block 的 data
export type ExercisePurpose = "class_practice" | "after_class_homework";

export interface InlineQuestionContent {
  content: unknown; // 与 questions.content 对齐
  type: string; // 与 questionTypeEnum 对齐
  difficulty: number;
  knowledgePointIds: string[];
}

export interface ExerciseItem {
  questionId: string; // bank=真实ID；inline=占位 inline_draft_xxx
  source: "bank" | "inline";
  score: number;
  order: number;
  inlineContent?: InlineQuestionContent; // 仅 inline
}

export interface ExerciseBlockData {
  items: ExerciseItem[];
  purpose: ExercisePurpose;
  knowledgePointIds: string[];
  publishedAssignmentId?: string;
  publishedExamId?: string;
  publishedAt?: string;
}

// Block 联合
export interface Block {
  id: string;
  type: BlockType;
  title: string;
  data: RichTextBlockData | TextStudyBlockData | ExerciseBlockData;
  order: number;
}

// 节点（Block + 画布坐标）
export interface LessonPlanNode extends Block {
  position: { x: number; y: number };
}

// 连线（节点间数据流/流程顺序）
export interface LessonPlanEdge {
  id: string;
  source: string; // 源节点 id
  target: string; // 目标节点 id
  sourceHandle?: string | null;
  targetHandle?: string | null;
}

// 文档 v1（旧格式，向后兼容读取）
export interface LessonPlanDocumentV1 {
  version: 1;
  blocks: Block[];
}

// 文档 v2（节点图格式）
export interface LessonPlanDocument {
  version: 2;
  nodes: LessonPlanNode[];
  edges: LessonPlanEdge[];
}

// 课案
export interface LessonPlan {
  id: string;
  title: string;
  textbookId: string | null;
  chapterId: string | null;
  coursePlanItemId: string | null;
  subjectId: string | null;
  gradeId: string | null;
  templateId: string | null;
  templateName: string | null;
  content: LessonPlanDocument;
  status: LessonPlanStatus;
  creatorId: string;
  lastSavedAt: string | null;
  createdAt: string;
  updatedAt: string;
}

// 版本
export interface LessonPlanVersion {
  id: string;
  planId: string;
  versionNo: number;
  label: string | null;
  content: LessonPlanDocument;
  isAuto: boolean;
  creatorId: string;
  createdAt: string;
}

// 模板
export type TemplateType = "system" | "personal";
export type TemplateScope =
  | "regular"
  | "review"
  | "experiment"
  | "inquiry"
  | "blank"
  | "custom";

export interface TemplateBlockSkeleton {
  type: BlockType;
  title: string;
  hint?: string;
}

export interface LessonPlanTemplate {
  id: string;
  name: string;
  type: TemplateType;
  scope: TemplateScope;
  blocks: TemplateBlockSkeleton[];
  creatorId: string | null;
  createdAt: string;
  updatedAt: string;
}

// 列表项（带教材/章节名）
export interface LessonPlanListItem extends LessonPlan {
  textbookTitle: string | null;
  chapterTitle: string | null;
  subjectName: string | null;
  gradeName: string | null;
  creatorName: string | null;
}

// ActionState（与项目现有约定一致）
export type ActionState<T = unknown> = {
  success: boolean;
  message?: string;
  errors?: Record<string, string[]>;
  data?: T;
};
