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

/**
 * Metadata for a single permission point, used by the permission catalog view
 * and the role-permission matrix to render grouped, labeled permissions.
 */
export interface PermissionMeta {
  /** The Permissions constant key, e.g. "EXAM_CREATE". */
  key: keyof typeof Permissions
  /** The permission string value, e.g. "exam:create". */
  value: Permission
  /** i18n key for the human-readable label, e.g. "rbac:permissions.exam.create". */
  labelKey: string
  /** i18n key for the description, e.g. "rbac:permissions.exam.createDesc". */
  descriptionKey: string
}

/**
 * A group of related permissions, used to render the catalog/matrix by module.
 */
export interface PermissionGroup {
  /** Group key, e.g. "exam". */
  group: string
  /** i18n key for the group label, e.g. "rbac:permissions.group.exam". */
  labelKey: string
  permissions: PermissionMeta[]
}

/**
 * The full permission catalog, grouped by module.
 *
 * This is the single source of truth for which permissions exist and how they
 * are grouped/labeled in the UI. When adding a new permission to
 * `shared/types/permissions.ts`, add a matching entry here so it shows up in
 * the admin permission management UI.
 */
export const PERMISSION_CATALOG: PermissionGroup[] = [
  {
    group: "exam",
    labelKey: "rbac:permissions.group.exam",
    permissions: [
      { key: "EXAM_CREATE", value: Permissions.EXAM_CREATE, labelKey: "rbac:permissions.exam.create", descriptionKey: "rbac:permissions.exam.createDesc" },
      { key: "EXAM_READ", value: Permissions.EXAM_READ, labelKey: "rbac:permissions.exam.read", descriptionKey: "rbac:permissions.exam.readDesc" },
      { key: "EXAM_UPDATE", value: Permissions.EXAM_UPDATE, labelKey: "rbac:permissions.exam.update", descriptionKey: "rbac:permissions.exam.updateDesc" },
      { key: "EXAM_DELETE", value: Permissions.EXAM_DELETE, labelKey: "rbac:permissions.exam.delete", descriptionKey: "rbac:permissions.exam.deleteDesc" },
      { key: "EXAM_DUPLICATE", value: Permissions.EXAM_DUPLICATE, labelKey: "rbac:permissions.exam.duplicate", descriptionKey: "rbac:permissions.exam.duplicateDesc" },
      { key: "EXAM_PUBLISH", value: Permissions.EXAM_PUBLISH, labelKey: "rbac:permissions.exam.publish", descriptionKey: "rbac:permissions.exam.publishDesc" },
      { key: "EXAM_AI_GENERATE", value: Permissions.EXAM_AI_GENERATE, labelKey: "rbac:permissions.exam.ai_generate", descriptionKey: "rbac:permissions.exam.ai_generateDesc" },
      { key: "EXAM_SUBMIT", value: Permissions.EXAM_SUBMIT, labelKey: "rbac:permissions.exam.submit", descriptionKey: "rbac:permissions.exam.submitDesc" },
      { key: "EXAM_PROCTOR", value: Permissions.EXAM_PROCTOR, labelKey: "rbac:permissions.exam.proctor", descriptionKey: "rbac:permissions.exam.proctorDesc" },
      { key: "EXAM_PROCTOR_READ", value: Permissions.EXAM_PROCTOR_READ, labelKey: "rbac:permissions.exam.proctor_read", descriptionKey: "rbac:permissions.exam.proctor_readDesc" },
    ],
  },
  {
    group: "homework",
    labelKey: "rbac:permissions.group.homework",
    permissions: [
      { key: "HOMEWORK_CREATE", value: Permissions.HOMEWORK_CREATE, labelKey: "rbac:permissions.homework.create", descriptionKey: "rbac:permissions.homework.createDesc" },
      { key: "HOMEWORK_GRADE", value: Permissions.HOMEWORK_GRADE, labelKey: "rbac:permissions.homework.grade", descriptionKey: "rbac:permissions.homework.gradeDesc" },
      { key: "HOMEWORK_SUBMIT", value: Permissions.HOMEWORK_SUBMIT, labelKey: "rbac:permissions.homework.submit", descriptionKey: "rbac:permissions.homework.submitDesc" },
    ],
  },
  {
    group: "question",
    labelKey: "rbac:permissions.group.question",
    permissions: [
      { key: "QUESTION_CREATE", value: Permissions.QUESTION_CREATE, labelKey: "rbac:permissions.question.create", descriptionKey: "rbac:permissions.question.createDesc" },
      { key: "QUESTION_READ", value: Permissions.QUESTION_READ, labelKey: "rbac:permissions.question.read", descriptionKey: "rbac:permissions.question.readDesc" },
      { key: "QUESTION_UPDATE", value: Permissions.QUESTION_UPDATE, labelKey: "rbac:permissions.question.update", descriptionKey: "rbac:permissions.question.updateDesc" },
      { key: "QUESTION_DELETE", value: Permissions.QUESTION_DELETE, labelKey: "rbac:permissions.question.delete", descriptionKey: "rbac:permissions.question.deleteDesc" },
    ],
  },
  {
    group: "textbook",
    labelKey: "rbac:permissions.group.textbook",
    permissions: [
      { key: "TEXTBOOK_CREATE", value: Permissions.TEXTBOOK_CREATE, labelKey: "rbac:permissions.textbook.create", descriptionKey: "rbac:permissions.textbook.createDesc" },
      { key: "TEXTBOOK_READ", value: Permissions.TEXTBOOK_READ, labelKey: "rbac:permissions.textbook.read", descriptionKey: "rbac:permissions.textbook.readDesc" },
      { key: "TEXTBOOK_UPDATE", value: Permissions.TEXTBOOK_UPDATE, labelKey: "rbac:permissions.textbook.update", descriptionKey: "rbac:permissions.textbook.updateDesc" },
      { key: "TEXTBOOK_DELETE", value: Permissions.TEXTBOOK_DELETE, labelKey: "rbac:permissions.textbook.delete", descriptionKey: "rbac:permissions.textbook.deleteDesc" },
    ],
  },
  {
    group: "class",
    labelKey: "rbac:permissions.group.class",
    permissions: [
      { key: "CLASS_CREATE", value: Permissions.CLASS_CREATE, labelKey: "rbac:permissions.class.create", descriptionKey: "rbac:permissions.class.createDesc" },
      { key: "CLASS_READ", value: Permissions.CLASS_READ, labelKey: "rbac:permissions.class.read", descriptionKey: "rbac:permissions.class.readDesc" },
      { key: "CLASS_UPDATE", value: Permissions.CLASS_UPDATE, labelKey: "rbac:permissions.class.update", descriptionKey: "rbac:permissions.class.updateDesc" },
      { key: "CLASS_DELETE", value: Permissions.CLASS_DELETE, labelKey: "rbac:permissions.class.delete", descriptionKey: "rbac:permissions.class.deleteDesc" },
      { key: "CLASS_ENROLL", value: Permissions.CLASS_ENROLL, labelKey: "rbac:permissions.class.enroll", descriptionKey: "rbac:permissions.class.enrollDesc" },
      { key: "CLASS_SCHEDULE", value: Permissions.CLASS_SCHEDULE, labelKey: "rbac:permissions.class.schedule", descriptionKey: "rbac:permissions.class.scheduleDesc" },
    ],
  },
  {
    group: "school",
    labelKey: "rbac:permissions.group.school",
    permissions: [
      { key: "SCHOOL_MANAGE", value: Permissions.SCHOOL_MANAGE, labelKey: "rbac:permissions.school.manage", descriptionKey: "rbac:permissions.school.manageDesc" },
      { key: "GRADE_MANAGE", value: Permissions.GRADE_MANAGE, labelKey: "rbac:permissions.school.grade_manage", descriptionKey: "rbac:permissions.school.grade_manageDesc" },
      { key: "USER_MANAGE", value: Permissions.USER_MANAGE, labelKey: "rbac:permissions.school.user_manage", descriptionKey: "rbac:permissions.school.user_manageDesc" },
    ],
  },
  {
    group: "user",
    labelKey: "rbac:permissions.group.user",
    permissions: [
      { key: "USER_PROFILE_UPDATE", value: Permissions.USER_PROFILE_UPDATE, labelKey: "rbac:permissions.user.profile_update", descriptionKey: "rbac:permissions.user.profile_updateDesc" },
    ],
  },
  {
    group: "ai",
    labelKey: "rbac:permissions.group.ai",
    permissions: [
      { key: "AI_CHAT", value: Permissions.AI_CHAT, labelKey: "rbac:permissions.ai.chat", descriptionKey: "rbac:permissions.ai.chatDesc" },
      { key: "AI_CONFIGURE", value: Permissions.AI_CONFIGURE, labelKey: "rbac:permissions.ai.configure", descriptionKey: "rbac:permissions.ai.configureDesc" },
    ],
  },
  {
    group: "settings",
    labelKey: "rbac:permissions.group.settings",
    permissions: [
      { key: "SETTINGS_ADMIN", value: Permissions.SETTINGS_ADMIN, labelKey: "rbac:permissions.settings.admin", descriptionKey: "rbac:permissions.settings.adminDesc" },
    ],
  },
  {
    group: "audit",
    labelKey: "rbac:permissions.group.audit",
    permissions: [
      { key: "AUDIT_LOG_READ", value: Permissions.AUDIT_LOG_READ, labelKey: "rbac:permissions.audit.read", descriptionKey: "rbac:permissions.audit.readDesc" },
    ],
  },
  {
    group: "announcement",
    labelKey: "rbac:permissions.group.announcement",
    permissions: [
      { key: "ANNOUNCEMENT_MANAGE", value: Permissions.ANNOUNCEMENT_MANAGE, labelKey: "rbac:permissions.announcement.manage", descriptionKey: "rbac:permissions.announcement.manageDesc" },
      { key: "ANNOUNCEMENT_READ", value: Permissions.ANNOUNCEMENT_READ, labelKey: "rbac:permissions.announcement.read", descriptionKey: "rbac:permissions.announcement.readDesc" },
    ],
  },
  {
    group: "grade_record",
    labelKey: "rbac:permissions.group.grade_record",
    permissions: [
      { key: "GRADE_RECORD_MANAGE", value: Permissions.GRADE_RECORD_MANAGE, labelKey: "rbac:permissions.grade_record.manage", descriptionKey: "rbac:permissions.grade_record.manageDesc" },
      { key: "GRADE_RECORD_READ", value: Permissions.GRADE_RECORD_READ, labelKey: "rbac:permissions.grade_record.read", descriptionKey: "rbac:permissions.grade_record.readDesc" },
    ],
  },
  {
    group: "file",
    labelKey: "rbac:permissions.group.file",
    permissions: [
      { key: "FILE_UPLOAD", value: Permissions.FILE_UPLOAD, labelKey: "rbac:permissions.file.upload", descriptionKey: "rbac:permissions.file.uploadDesc" },
      { key: "FILE_READ", value: Permissions.FILE_READ, labelKey: "rbac:permissions.file.read", descriptionKey: "rbac:permissions.file.readDesc" },
      { key: "FILE_DELETE", value: Permissions.FILE_DELETE, labelKey: "rbac:permissions.file.delete", descriptionKey: "rbac:permissions.file.deleteDesc" },
    ],
  },
  {
    group: "course_plan",
    labelKey: "rbac:permissions.group.course_plan",
    permissions: [
      { key: "COURSE_PLAN_MANAGE", value: Permissions.COURSE_PLAN_MANAGE, labelKey: "rbac:permissions.course_plan.manage", descriptionKey: "rbac:permissions.course_plan.manageDesc" },
      { key: "COURSE_PLAN_READ", value: Permissions.COURSE_PLAN_READ, labelKey: "rbac:permissions.course_plan.read", descriptionKey: "rbac:permissions.course_plan.readDesc" },
    ],
  },
  {
    group: "attendance",
    labelKey: "rbac:permissions.group.attendance",
    permissions: [
      { key: "ATTENDANCE_MANAGE", value: Permissions.ATTENDANCE_MANAGE, labelKey: "rbac:permissions.attendance.manage", descriptionKey: "rbac:permissions.attendance.manageDesc" },
      { key: "ATTENDANCE_READ", value: Permissions.ATTENDANCE_READ, labelKey: "rbac:permissions.attendance.read", descriptionKey: "rbac:permissions.attendance.readDesc" },
      { key: "LEAVE_REQUEST_CREATE", value: Permissions.LEAVE_REQUEST_CREATE, labelKey: "rbac:permissions.leave_request.create", descriptionKey: "rbac:permissions.leave_request.createDesc" },
      { key: "LEAVE_REQUEST_READ", value: Permissions.LEAVE_REQUEST_READ, labelKey: "rbac:permissions.leave_request.read", descriptionKey: "rbac:permissions.leave_request.readDesc" },
      { key: "LEAVE_REQUEST_REVIEW", value: Permissions.LEAVE_REQUEST_REVIEW, labelKey: "rbac:permissions.leave_request.review", descriptionKey: "rbac:permissions.leave_request.reviewDesc" },
    ],
  },
  {
    group: "message",
    labelKey: "rbac:permissions.group.message",
    permissions: [
      { key: "MESSAGE_SEND", value: Permissions.MESSAGE_SEND, labelKey: "rbac:permissions.message.send", descriptionKey: "rbac:permissions.message.sendDesc" },
      { key: "MESSAGE_READ", value: Permissions.MESSAGE_READ, labelKey: "rbac:permissions.message.read", descriptionKey: "rbac:permissions.message.readDesc" },
      { key: "MESSAGE_DELETE", value: Permissions.MESSAGE_DELETE, labelKey: "rbac:permissions.message.delete", descriptionKey: "rbac:permissions.message.deleteDesc" },
    ],
  },
  {
    group: "scheduling",
    labelKey: "rbac:permissions.group.scheduling",
    permissions: [
      { key: "SCHEDULE_AUTO", value: Permissions.SCHEDULE_AUTO, labelKey: "rbac:permissions.scheduling.auto", descriptionKey: "rbac:permissions.scheduling.autoDesc" },
      { key: "SCHEDULE_ADJUST", value: Permissions.SCHEDULE_ADJUST, labelKey: "rbac:permissions.scheduling.adjust", descriptionKey: "rbac:permissions.scheduling.adjustDesc" },
    ],
  },
  {
    group: "elective",
    labelKey: "rbac:permissions.group.elective",
    permissions: [
      { key: "ELECTIVE_MANAGE", value: Permissions.ELECTIVE_MANAGE, labelKey: "rbac:permissions.elective.manage", descriptionKey: "rbac:permissions.elective.manageDesc" },
      { key: "ELECTIVE_READ", value: Permissions.ELECTIVE_READ, labelKey: "rbac:permissions.elective.read", descriptionKey: "rbac:permissions.elective.readDesc" },
      { key: "ELECTIVE_SELECT", value: Permissions.ELECTIVE_SELECT, labelKey: "rbac:permissions.elective.select", descriptionKey: "rbac:permissions.elective.selectDesc" },
    ],
  },
  {
    group: "diagnostic",
    labelKey: "rbac:permissions.group.diagnostic",
    permissions: [
      { key: "DIAGNOSTIC_MANAGE", value: Permissions.DIAGNOSTIC_MANAGE, labelKey: "rbac:permissions.diagnostic.manage", descriptionKey: "rbac:permissions.diagnostic.manageDesc" },
      { key: "DIAGNOSTIC_READ", value: Permissions.DIAGNOSTIC_READ, labelKey: "rbac:permissions.diagnostic.read", descriptionKey: "rbac:permissions.diagnostic.readDesc" },
    ],
  },
  {
    group: "lesson_plan",
    labelKey: "rbac:permissions.group.lesson_plan",
    permissions: [
      { key: "LESSON_PLAN_CREATE", value: Permissions.LESSON_PLAN_CREATE, labelKey: "rbac:permissions.lesson_plan.create", descriptionKey: "rbac:permissions.lesson_plan.createDesc" },
      { key: "LESSON_PLAN_READ", value: Permissions.LESSON_PLAN_READ, labelKey: "rbac:permissions.lesson_plan.read", descriptionKey: "rbac:permissions.lesson_plan.readDesc" },
      { key: "LESSON_PLAN_UPDATE", value: Permissions.LESSON_PLAN_UPDATE, labelKey: "rbac:permissions.lesson_plan.update", descriptionKey: "rbac:permissions.lesson_plan.updateDesc" },
      { key: "LESSON_PLAN_DELETE", value: Permissions.LESSON_PLAN_DELETE, labelKey: "rbac:permissions.lesson_plan.delete", descriptionKey: "rbac:permissions.lesson_plan.deleteDesc" },
      { key: "LESSON_PLAN_PUBLISH", value: Permissions.LESSON_PLAN_PUBLISH, labelKey: "rbac:permissions.lesson_plan.publish", descriptionKey: "rbac:permissions.lesson_plan.publishDesc" },
    ],
  },
  {
    group: "dashboard",
    labelKey: "rbac:permissions.group.dashboard",
    permissions: [
      { key: "DASHBOARD_ADMIN_READ", value: Permissions.DASHBOARD_ADMIN_READ, labelKey: "rbac:permissions.dashboard.admin_read", descriptionKey: "rbac:permissions.dashboard.admin_readDesc" },
      { key: "DASHBOARD_TEACHER_READ", value: Permissions.DASHBOARD_TEACHER_READ, labelKey: "rbac:permissions.dashboard.teacher_read", descriptionKey: "rbac:permissions.dashboard.teacher_readDesc" },
      { key: "DASHBOARD_STUDENT_READ", value: Permissions.DASHBOARD_STUDENT_READ, labelKey: "rbac:permissions.dashboard.student_read", descriptionKey: "rbac:permissions.dashboard.student_readDesc" },
      { key: "DASHBOARD_PARENT_READ", value: Permissions.DASHBOARD_PARENT_READ, labelKey: "rbac:permissions.dashboard.parent_read", descriptionKey: "rbac:permissions.dashboard.parent_readDesc" },
    ],
  },
  {
    group: "error_book",
    labelKey: "rbac:permissions.group.error_book",
    permissions: [
      { key: "ERROR_BOOK_READ", value: Permissions.ERROR_BOOK_READ, labelKey: "rbac:permissions.error_book.read", descriptionKey: "rbac:permissions.error_book.readDesc" },
      { key: "ERROR_BOOK_MANAGE", value: Permissions.ERROR_BOOK_MANAGE, labelKey: "rbac:permissions.error_book.manage", descriptionKey: "rbac:permissions.error_book.manageDesc" },
      { key: "ERROR_BOOK_ANALYTICS_READ", value: Permissions.ERROR_BOOK_ANALYTICS_READ, labelKey: "rbac:permissions.error_book.analytics_read", descriptionKey: "rbac:permissions.error_book.analytics_readDesc" },
    ],
  },
  {
    group: "adaptive_practice",
    labelKey: "rbac:permissions.group.adaptive_practice",
    permissions: [
      { key: "ADAPTIVE_PRACTICE_READ", value: Permissions.ADAPTIVE_PRACTICE_READ, labelKey: "rbac:permissions.adaptive_practice.read", descriptionKey: "rbac:permissions.adaptive_practice.readDesc" },
      { key: "ADAPTIVE_PRACTICE_MANAGE", value: Permissions.ADAPTIVE_PRACTICE_MANAGE, labelKey: "rbac:permissions.adaptive_practice.manage", descriptionKey: "rbac:permissions.adaptive_practice.manageDesc" },
    ],
  },
  {
    group: "rbac",
    labelKey: "rbac:permissions.group.rbac",
    permissions: [
      { key: "ROLE_CREATE", value: Permissions.ROLE_CREATE, labelKey: "rbac:permissions.rbac.role_create", descriptionKey: "rbac:permissions.rbac.role_createDesc" },
      { key: "ROLE_READ", value: Permissions.ROLE_READ, labelKey: "rbac:permissions.rbac.role_read", descriptionKey: "rbac:permissions.rbac.role_readDesc" },
      { key: "ROLE_UPDATE", value: Permissions.ROLE_UPDATE, labelKey: "rbac:permissions.rbac.role_update", descriptionKey: "rbac:permissions.rbac.role_updateDesc" },
      { key: "ROLE_DELETE", value: Permissions.ROLE_DELETE, labelKey: "rbac:permissions.rbac.role_delete", descriptionKey: "rbac:permissions.rbac.role_deleteDesc" },
      { key: "ROLE_ASSIGN", value: Permissions.ROLE_ASSIGN, labelKey: "rbac:permissions.rbac.role_assign", descriptionKey: "rbac:permissions.rbac.role_assignDesc" },
      { key: "PERMISSION_READ", value: Permissions.PERMISSION_READ, labelKey: "rbac:permissions.rbac.permission_read", descriptionKey: "rbac:permissions.rbac.permission_readDesc" },
    ],
  },
]

/** Flatten the catalog into a single list of all permission metadata. */
export function getAllPermissionMetas(): PermissionMeta[] {
  return PERMISSION_CATALOG.flatMap((g) => g.permissions)
}

/** Look up the metadata for a single permission value. */
export function getPermissionMeta(value: Permission): PermissionMeta | undefined {
  return getAllPermissionMetas().find((m) => m.value === value)
}
