/**
 * P2-13: 配置驱动的角色渲染 ExamHomeworkRoleConfig
 *
 * 单一数据源：声明每个角色在考试/作业模块中可见的功能区与可执行的操作。
 * 配合 usePermission() 使用，作为 UI 渲染的防御性筛选层（权限系统仍为最终授权源）。
 *
 * 使用方式：
 *   const features = getRoleFeatures(roles)
 *   if (features.canCreateExam) { render <CreateExamButton /> }
 */

import type { Permission, Role } from "@/shared/types/permissions"
import { Permissions } from "@/shared/types/permissions"

/**
 * 考试/作业模块中按角色控制的功能区。
 * 字段语义：`can*` 表示是否可见/可执行某类操作。
 */
export interface ExamHomeworkRoleFeatures {
  /** 创建考试/作业（教师、管理员） */
  canCreate: boolean
  /** 查看考试/作业列表（所有有读权限的角色） */
  canRead: boolean
  /** 编辑/删除考试/作业（教师、管理员） */
  canManage: boolean
  /** 发布考试（教师、年级组长、教务主任） */
  canPublish: boolean
  /** 组卷/编辑作业题目（教师、管理员） */
  canBuild: boolean
  /** 批改作业/考试（教师、年级组长、教务主任） */
  canGrade: boolean
  /** 监考（教师、年级组长、教务主任） */
  canProctor: boolean
  /** 学生作答（学生） */
  canTake: boolean
  /** 查看成绩/反馈（学生、家长） */
  canViewResult: boolean
  /** AI 生成考试（教师、管理员） */
  canUseAi: boolean
  /** 查看统计/分析（教师、年级组长、教务主任、管理员） */
  canViewStats: boolean
}

/**
 * 角色到权限点的映射，作为渲染配置的依据。
 * 每个角色对应一组 Permission 常量，用于推导 features。
 */
export const EXAM_HOMEWORK_ROLE_PERMISSIONS: Record<Role, readonly Permission[]> = {
  admin: [
    Permissions.EXAM_CREATE,
    Permissions.EXAM_READ,
    Permissions.EXAM_UPDATE,
    Permissions.EXAM_DELETE,
    Permissions.EXAM_PUBLISH,
    Permissions.EXAM_AI_GENERATE,
    Permissions.EXAM_PROCTOR,
    Permissions.HOMEWORK_CREATE,
    Permissions.HOMEWORK_GRADE,
    Permissions.HOMEWORK_SUBMIT,
  ],
  teacher: [
    Permissions.EXAM_CREATE,
    Permissions.EXAM_READ,
    Permissions.EXAM_UPDATE,
    Permissions.EXAM_PUBLISH,
    Permissions.EXAM_AI_GENERATE,
    Permissions.EXAM_PROCTOR,
    Permissions.HOMEWORK_CREATE,
    Permissions.HOMEWORK_GRADE,
  ],
  grade_head: [
    Permissions.EXAM_READ,
    Permissions.EXAM_PUBLISH,
    Permissions.EXAM_PROCTOR,
    Permissions.HOMEWORK_GRADE,
  ],
  teaching_head: [
    Permissions.EXAM_READ,
    Permissions.EXAM_PUBLISH,
    Permissions.EXAM_PROCTOR,
    Permissions.HOMEWORK_GRADE,
  ],
  student: [
    Permissions.EXAM_READ,
    Permissions.EXAM_SUBMIT,
    Permissions.HOMEWORK_SUBMIT,
  ],
  parent: [],
} as const

/**
 * 角色到功能区配置的映射。
 * 这是渲染层的单一数据源：UI 根据 features 决定显示哪些入口/按钮。
 */
export const EXAM_HOMEWORK_ROLE_CONFIG: Record<Role, ExamHomeworkRoleFeatures> = {
  admin: {
    canCreate: true,
    canRead: true,
    canManage: true,
    canPublish: true,
    canBuild: true,
    canGrade: true,
    canProctor: true,
    canTake: false,
    canViewResult: true,
    canUseAi: true,
    canViewStats: true,
  },
  teacher: {
    canCreate: true,
    canRead: true,
    canManage: true,
    canPublish: true,
    canBuild: true,
    canGrade: true,
    canProctor: true,
    canTake: false,
    canViewResult: true,
    canUseAi: true,
    canViewStats: true,
  },
  grade_head: {
    canCreate: false,
    canRead: true,
    canManage: false,
    canPublish: true,
    canBuild: false,
    canGrade: true,
    canProctor: true,
    canTake: false,
    canViewResult: true,
    canUseAi: false,
    canViewStats: true,
  },
  teaching_head: {
    canCreate: false,
    canRead: true,
    canManage: false,
    canPublish: true,
    canBuild: false,
    canGrade: true,
    canProctor: true,
    canTake: false,
    canViewResult: true,
    canUseAi: false,
    canViewStats: true,
  },
  student: {
    canCreate: false,
    canRead: true,
    canManage: false,
    canPublish: false,
    canBuild: false,
    canGrade: false,
    canProctor: false,
    canTake: true,
    canViewResult: true,
    canUseAi: false,
    canViewStats: false,
  },
  parent: {
    canCreate: false,
    canRead: false,
    canManage: false,
    canPublish: false,
    canBuild: false,
    canGrade: false,
    canProctor: false,
    canTake: false,
    canViewResult: true,
    canUseAi: false,
    canViewStats: false,
  },
}

/**
 * 默认（无角色）特性集：所有功能关闭。
 * 用于未登录或角色未识别的兜底场景。
 */
export const DEFAULT_EXAM_HOMEWORK_FEATURES: ExamHomeworkRoleFeatures = {
  canCreate: false,
  canRead: false,
  canManage: false,
  canPublish: false,
  canBuild: false,
  canGrade: false,
  canProctor: false,
  canTake: false,
  canViewResult: false,
  canUseAi: false,
  canViewStats: false,
}

/**
 * 根据用户角色列表合并出最终的功能特性。
 *
 * 合并策略：任一角色拥有某功能即视为启用（并集）。
 * 例如：同时具有 teacher + grade_head 的用户，将获得两个角色的所有能力。
 *
 * @param roles 当前用户的角色列表
 * @returns 合并后的功能特性
 */
export function getExamHomeworkFeatures(roles: readonly Role[]): ExamHomeworkRoleFeatures {
  if (roles.length === 0) return DEFAULT_EXAM_HOMEWORK_FEATURES

  const merged: ExamHomeworkRoleFeatures = { ...DEFAULT_EXAM_HOMEWORK_FEATURES }
  for (const role of roles) {
    const cfg = EXAM_HOMEWORK_ROLE_CONFIG[role]
    if (!cfg) continue
    merged.canCreate = merged.canCreate || cfg.canCreate
    merged.canRead = merged.canRead || cfg.canRead
    merged.canManage = merged.canManage || cfg.canManage
    merged.canPublish = merged.canPublish || cfg.canPublish
    merged.canBuild = merged.canBuild || cfg.canBuild
    merged.canGrade = merged.canGrade || cfg.canGrade
    merged.canProctor = merged.canProctor || cfg.canProctor
    merged.canTake = merged.canTake || cfg.canTake
    merged.canViewResult = merged.canViewResult || cfg.canViewResult
    merged.canUseAi = merged.canUseAi || cfg.canUseAi
    merged.canViewStats = merged.canViewStats || cfg.canViewStats
  }
  return merged
}
