/**
 * exams 模块 Widget 配置（P2-5 骨架）
 *
 * 设计目标：
 * - 配置驱动决定每个角色在 exams 模块渲染哪些子模块/区块
 * - 新增角色或调整 Widget 只需修改此配置文件，不需动组件代码
 * - 与 dashboard/config/widget-configs.ts 模式对齐，保持一致性
 *
 * 当前阶段（P2-5）：
 * - 仅落地配置类型与默认配置骨架
 * - 不强制改造现有页面（teacher/exams/all 等仍使用既有布局）
 * - P2-7+ 落地多角色页（admin/student exams）时直接复用此配置
 *
 * 使用示例：
 * ```tsx
 * import { getExamWidgetConfig, ExamWidgetRenderer } from "@/modules/exams/config"
 * const config = getExamWidgetConfig("teacher")
 * <ExamWidgetRenderer config={config} data={data} />
 * ```
 */

/** exams 模块支持的角色（与 dashboard 对齐） */
export type ExamRole = "admin" | "teacher" | "student" | "parent"

/** exams 模块 Widget 标识 */
export type ExamWidgetId =
  | "exam-list"
  | "exam-create-entry"
  | "exam-analytics"
  | "exam-proctoring"
  | "exam-templates"
  | "exam-blueprint"
  | "exam-grade-entry"
  | "exam-history"

/** exams 模块 Widget 骨架屏变体（与 exam-boundaries 对齐） */
export type ExamWidgetSkeletonVariant = "list" | "card" | "detail" | "analytics" | "editor"

/** exams 模块渲染区域标识 */
export type ExamWidgetSlot =
  | "header"
  | "stats"
  | "main"
  | "sidebar"
  | "actions"
  | "table"

/**
 * 单个 Widget 配置项
 */
export interface ExamWidgetConfig {
  /** Widget 唯一标识（用于埋点与去重） */
  id: ExamWidgetId
  /** 渲染区域 */
  slot: ExamWidgetSlot
  /** 骨架屏变体 */
  skeletonVariant: ExamWidgetSkeletonVariant
  /** 响应式布局类名 */
  layoutClassName: string
  /** 是否默认显示 */
  defaultVisible: boolean
  /** 排序优先级（数字越小越靠前） */
  order: number
  /** 可选的 Widget 配置参数 */
  props?: Record<string, unknown>
}

/** 角色对应 exams 模块布局配置 */
export interface ExamWidgetLayoutConfig {
  role: ExamRole
  widgets: ExamWidgetConfig[]
}

/** Teacher 角色配置：完整组卷/预览/分析/监考能力 */
export const TEACHER_EXAM_WIDGET_CONFIG: ExamWidgetLayoutConfig = {
  role: "teacher",
  widgets: [
    {
      id: "exam-create-entry",
      slot: "actions",
      skeletonVariant: "card",
      layoutClassName: "grid gap-4 md:grid-cols-3",
      defaultVisible: true,
      order: 10,
      props: { entries: ["manual", "ai", "rich-editor"] },
    },
    {
      id: "exam-list",
      slot: "main",
      skeletonVariant: "list",
      layoutClassName: "",
      defaultVisible: true,
      order: 20,
    },
    {
      id: "exam-analytics",
      slot: "main",
      skeletonVariant: "analytics",
      layoutClassName: "",
      defaultVisible: false,
      order: 30,
    },
    {
      id: "exam-proctoring",
      slot: "main",
      skeletonVariant: "detail",
      layoutClassName: "",
      defaultVisible: false,
      order: 40,
    },
    {
      id: "exam-grade-entry",
      slot: "table",
      skeletonVariant: "list",
      layoutClassName: "",
      defaultVisible: false,
      order: 50,
    },
  ],
}

/** Admin 角色配置：聚合全校/年级视角 */
export const ADMIN_EXAM_WIDGET_CONFIG: ExamWidgetLayoutConfig = {
  role: "admin",
  widgets: [
    {
      id: "exam-list",
      slot: "main",
      skeletonVariant: "list",
      layoutClassName: "",
      defaultVisible: true,
      order: 10,
      props: { scope: "all-school" },
    },
    {
      id: "exam-analytics",
      slot: "main",
      skeletonVariant: "analytics",
      layoutClassName: "",
      defaultVisible: true,
      order: 20,
      props: { aggregation: "by-grade" },
    },
    {
      id: "exam-templates",
      slot: "sidebar",
      skeletonVariant: "card",
      layoutClassName: "",
      defaultVisible: false,
      order: 30,
    },
  ],
}

/** Student 角色配置：以作答与成绩回顾为主 */
export const STUDENT_EXAM_WIDGET_CONFIG: ExamWidgetLayoutConfig = {
  role: "student",
  widgets: [
    {
      id: "exam-list",
      slot: "main",
      skeletonVariant: "card",
      layoutClassName: "grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3",
      defaultVisible: true,
      order: 10,
      props: { filter: "published", showTake: true },
    },
    {
      id: "exam-history",
      slot: "main",
      skeletonVariant: "list",
      layoutClassName: "",
      defaultVisible: true,
      order: 20,
      props: { showScore: true, showAnswers: true },
    },
  ],
}

/** Parent 角色配置：以查看子女考试表现为主 */
export const PARENT_EXAM_WIDGET_CONFIG: ExamWidgetLayoutConfig = {
  role: "parent",
  widgets: [
    {
      id: "exam-list",
      slot: "main",
      skeletonVariant: "card",
      layoutClassName: "grid grid-cols-1 gap-4 md:grid-cols-2",
      defaultVisible: true,
      order: 10,
      props: { filter: "child", showScore: true },
    },
    {
      id: "exam-history",
      slot: "main",
      skeletonVariant: "list",
      layoutClassName: "",
      defaultVisible: true,
      order: 20,
      props: { trendChart: true },
    },
  ],
}

/** 按角色获取 exams 模块 Widget 配置 */
export function getExamWidgetConfig(role: ExamRole): ExamWidgetLayoutConfig {
  switch (role) {
    case "admin":
      return ADMIN_EXAM_WIDGET_CONFIG
    case "teacher":
      return TEACHER_EXAM_WIDGET_CONFIG
    case "student":
      return STUDENT_EXAM_WIDGET_CONFIG
    case "parent":
      return PARENT_EXAM_WIDGET_CONFIG
  }
}

/**
 * 从配置中筛选指定 slot 的 Widget（按 order 升序）。
 * 用于按区域分块渲染。
 */
export function getWidgetsBySlot(
  config: ExamWidgetLayoutConfig,
  slot: ExamWidgetSlot
): ExamWidgetConfig[] {
  return config.widgets
    .filter((w) => w.slot === slot && w.defaultVisible)
    .sort((a, b) => a.order - b.order)
}
