/**
 * admin-portal 权限点常量
 *
 * 仲裁裁决（ISSUE-046 §5.3 / ISSUE-045 §5.2）：
 * - admin-portal 权限点统一加 ADMIN_ 前缀
 * - 跨服务资源保留原前缀（如 IAM_USER_READ）
 * - 错误码前缀 ADMIN_（合并 SCHOOL_ / ORG_ 入 ADMIN_）
 *
 * 来源：packages/contracts/src/permissions.ts（coord 维护）
 * admin-portal 引用，不硬编码
 */

/** admin 自身资源权限点（ADMIN_ 前缀） */
export const ADMIN_PERMISSIONS = {
  DASHBOARD_VIEW: "ADMIN_DASHBOARD_VIEW",
  SYSTEM_MANAGE: "ADMIN_SYSTEM_MANAGE",
  AUDIT_READ: "ADMIN_AUDIT_READ",
  CLASS_READ: "ADMIN_CLASS_READ",
  CLASS_MANAGE: "ADMIN_CLASS_MANAGE",
  TEACHER_READ: "ADMIN_TEACHER_READ",
  TEACHER_MANAGE: "ADMIN_TEACHER_MANAGE",
  STUDENT_READ: "ADMIN_STUDENT_READ",
  STUDENT_MANAGE: "ADMIN_STUDENT_MANAGE",
  ORG_MANAGE: "ADMIN_ORG_MANAGE",
  SCHOOL_READ: "ADMIN_SCHOOL_READ",
  SCHOOL_MANAGE: "ADMIN_SCHOOL_MANAGE",
  GRADE_READ: "ADMIN_GRADE_READ",
  GRADE_MANAGE: "ADMIN_GRADE_MANAGE",
  GRADE_INSIGHTS_VIEW: "ADMIN_GRADE_INSIGHTS_VIEW",
  DEPARTMENT_READ: "ADMIN_DEPARTMENT_READ",
  DEPARTMENT_MANAGE: "ADMIN_DEPARTMENT_MANAGE",
  ACADEMIC_YEAR_MANAGE: "ADMIN_ACADEMIC_YEAR_MANAGE",
  ANNOUNCEMENT_READ: "ADMIN_ANNOUNCEMENT_READ",
  ANNOUNCEMENT_MANAGE: "ADMIN_ANNOUNCEMENT_MANAGE",
  FILE_READ: "ADMIN_FILE_READ",
  FILE_MANAGE: "ADMIN_FILE_MANAGE",
  INVITATION_CODE_MANAGE: "ADMIN_INVITATION_CODE_MANAGE",
  AI_CONFIGURE: "ADMIN_AI_CONFIGURE",
  MONITORING_VIEW: "ADMIN_MONITORING_VIEW",
} as const;

/** 跨服务资源权限点（保留原前缀，iam 域） */
export const IAM_PERMISSIONS = {
  USER_READ: "IAM_USER_READ",
  USER_CREATE: "IAM_USER_CREATE",
  USER_UPDATE: "IAM_USER_UPDATE",
  USER_DELETE: "IAM_USER_DELETE",
  ROLE_READ: "IAM_ROLE_READ",
  ROLE_CREATE: "IAM_ROLE_CREATE",
  ROLE_UPDATE: "IAM_ROLE_UPDATE",
  ROLE_DELETE: "IAM_ROLE_DELETE",
  PERMISSION_READ: "IAM_PERMISSION_READ",
  VIEWPORT_READ: "IAM_VIEWPORT_READ",
  VIEWPORT_UPDATE: "IAM_VIEWPORT_UPDATE",
} as const;

/** 所有权限点合集 */
export const PERMISSIONS = {
  ...ADMIN_PERMISSIONS,
  ...IAM_PERMISSIONS,
} as const;

export type PermissionCode = (typeof PERMISSIONS)[keyof typeof PERMISSIONS];

/** admin-portal 视口路由权限映射（contract §1.2） */
export const ROUTE_PERMISSIONS: Record<string, string> = {
  "/admin/dashboard": ADMIN_PERMISSIONS.DASHBOARD_VIEW,
  "/admin/users": IAM_PERMISSIONS.USER_READ,
  "/admin/roles": IAM_PERMISSIONS.ROLE_READ,
  "/admin/permissions": IAM_PERMISSIONS.PERMISSION_READ,
  "/admin/viewports": IAM_PERMISSIONS.VIEWPORT_READ,
  "/admin/organization": ADMIN_PERMISSIONS.ORG_MANAGE,
  "/admin/system": ADMIN_PERMISSIONS.SYSTEM_MANAGE,
  "/admin/classes": ADMIN_PERMISSIONS.CLASS_READ,
  "/admin/teachers": ADMIN_PERMISSIONS.TEACHER_READ,
  "/admin/students": ADMIN_PERMISSIONS.STUDENT_READ,
  "/admin/audit-logs": ADMIN_PERMISSIONS.AUDIT_READ,
  "/admin/audit-logs/overview": ADMIN_PERMISSIONS.AUDIT_READ,
  "/admin/audit-logs/data-changes": ADMIN_PERMISSIONS.AUDIT_READ,
  "/admin/audit-logs/login-logs": ADMIN_PERMISSIONS.AUDIT_READ,
  "/admin/school": ADMIN_PERMISSIONS.SCHOOL_READ,
  "/admin/school/schools": ADMIN_PERMISSIONS.SCHOOL_READ,
  "/admin/school/grades": ADMIN_PERMISSIONS.GRADE_READ,
  "/admin/school/grades/insights": ADMIN_PERMISSIONS.GRADE_INSIGHTS_VIEW,
  "/admin/school/departments": ADMIN_PERMISSIONS.DEPARTMENT_READ,
  "/admin/school/academic-year": ADMIN_PERMISSIONS.ACADEMIC_YEAR_MANAGE,
  "/admin/announcements": ADMIN_PERMISSIONS.ANNOUNCEMENT_READ,
  "/admin/invitation-codes": ADMIN_PERMISSIONS.INVITATION_CODE_MANAGE,
  "/admin/files": ADMIN_PERMISSIONS.FILE_READ,
  "/admin/ai-settings": ADMIN_PERMISSIONS.AI_CONFIGURE,
};

/** admin-portal 错误码前缀（ISSUE-045 §5.2） */
export const ADMIN_ERROR_PREFIX = "ADMIN_";

/** 消费的错误码前缀（contract §2.5） */
export const CONSUMED_ERROR_PREFIXES = [
  "IAM_",
  "BFF_TEACHER_",
  "GW_",
  "NETWORK_",
  "ADMIN_",
] as const;
