export type CoursePlanStatus = "planning" | "active" | "completed" | "paused"

export type CoursePlanSemester = "1" | "2"

export interface CoursePlan {
  id: string
  classId: string
  subjectId: string
  teacherId: string
  academicYearId: string | null
  semester: CoursePlanSemester
  totalHours: number
  completedHours: number
  weeklyHours: number
  startDate: string | null
  endDate: string | null
  syllabus: string | null
  objectives: string | null
  status: CoursePlanStatus
  createdBy: string
  createdAt: string
  updatedAt: string
}

export interface CoursePlanItem {
  id: string
  planId: string
  week: number
  topic: string
  content: string | null
  hours: number
  textbookChapter: string | null
  notes: string | null
  isCompleted: boolean
  completedAt: string | null
  createdAt: string
  updatedAt: string
}

export interface CoursePlanListItem extends CoursePlan {
  className: string | null
  subjectName: string | null
  teacherName: string | null
}

export interface CoursePlanWithItems extends CoursePlanListItem {
  items: CoursePlanItem[]
}

export interface GetCoursePlansParams {
  classId?: string
  teacherId?: string
  subjectId?: string
  status?: CoursePlanStatus
}

export interface ReorderCoursePlanItemInput {
  id: string
  week: number
}

/**
 * 数据范围上下文，用于在 data-access 层进行权限过滤。
 * 由调用方（Server Action / 页面）从 AuthContext.dataScope 解析后传入。
 */
export interface CoursePlanQueryScope {
  /** 当前用户 ID */
  userId: string
  /** 是否管理员（拥有全局视图） */
  isAdmin: boolean
  /** 可访问的班级 ID 列表（非 admin 时用于过滤） */
  classIds?: string[]
  /** 教师视角：仅查看自己负责的计划 */
  teacherId?: string
  /** 学生/家长视角：仅查看特定孩子的班级 */
  studentId?: string
}

/**
 * 年级仪表盘 - 维度4：年级下各班级/科目的课本进度。
 */
export interface GradeCoursePlanProgressItem {
  planId: string
  classId: string
  className: string | null
  subjectId: string
  subjectName: string | null
  teacherName: string | null
  semester: CoursePlanSemester
  totalHours: number
  completedHours: number
  /** 进度百分比 0-100 */
  progressRate: number
  status: CoursePlanStatus
  /** 周次条目总数 */
  itemCount: number
  /** 已完成条目数 */
  completedItemCount: number
}

export interface GradeCoursePlanProgressResult {
  gradeId: string
  /** 年级整体进度汇总 */
  overall: {
    totalPlans: number
    totalHours: number
    completedHours: number
    progressRate: number
    activePlans: number
    completedPlans: number
  }
  /** 按班级 + 科目拆分的进度列表 */
  items: GradeCoursePlanProgressItem[]
}

// ── 类型守卫 ──────────────────────────────────────────────

export const isCoursePlanStatus = (v: unknown): v is CoursePlanStatus =>
  v === "planning" || v === "active" || v === "completed" || v === "paused"

export const isCoursePlanSemester = (v: unknown): v is CoursePlanSemester =>
  v === "1" || v === "2"

// ── 角色配置驱动：决定各角色视图渲染哪些 Widget ────────────

export type CoursePlanWidgetId =
  | "list"
  | "progress"
  | "calendar"
  | "bulkActions"
  | "export"
  | "templates"

export interface RoleWidgetConfig {
  widgets: CoursePlanWidgetId[]
  canManage: boolean
  detailBaseHref: string
}

export const ROLE_WIDGET_CONFIG: Record<"admin" | "teacher" | "parent" | "student", RoleWidgetConfig> = {
  admin: {
    widgets: ["list", "progress", "calendar", "bulkActions", "export", "templates"],
    canManage: true,
    detailBaseHref: "/admin/course-plans",
  },
  teacher: {
    widgets: ["list", "progress", "calendar"],
    canManage: true,
    detailBaseHref: "/teacher/course-plans",
  },
  parent: {
    widgets: ["list", "progress"],
    canManage: false,
    detailBaseHref: "/parent/course-plans",
  },
  student: {
    widgets: ["list", "progress"],
    canManage: false,
    detailBaseHref: "/student/course-plans",
  },
}
