export const ORGANIZATION_PERMISSIONS = [
  'organization:manage',
  'organization:switch'
] as const;

export const PERMISSION_TEMPLATE_PERMISSIONS = [
  'permission_template:read',
  'permission_template:create',
  'permission_template:update',
  'permission_template:clone',
  'permission_template:delete',
  'permission_template:assign'
] as const;

export const USER_PERMISSION_PERMISSIONS = [
  'user_permission:read',
  'user_permission:update',
  'user_permission:override',
  'user_permission:remove_override'
] as const;

export const USER_MANAGEMENT_PERMISSIONS = [
  'users:read',
  'users:create',
  'users:update',
  'users:delete',
  'users:manage'
] as const;

export const EMPLOYEE_DIRECTORY_PERMISSIONS = [
  'employee_directory:read',
  'employee_directory:write',
  'employee_import:run',
  'employee_master_review:approve'
] as const;

export const COURSE_AND_MATRIX_PERMISSIONS = [
  'courses:read',
  'courses:write',
  'training_policy:read',
  'training_policy:write',
  'training_matrix:read',
  'training_matrix:write'
] as const;

export const TRAINING_RECORD_PERMISSIONS = [
  'training_record:self_read',
  'training_record:self_write',
  'training_record:read_all',
  'training_record:write_all',
  'training_record:submit',
  'training_record:review',
  'training_record:approve',
  'training_record:reject',
  'training_record:delete_draft',
  'training_record:manage_attachments'
] as const;

export const ANNOUNCEMENT_PERMISSIONS = [
  'announcement:read_public',
  'announcement:read_all',
  'announcement:create',
  'announcement:edit_draft',
  'announcement:delete_draft',
  'announcement:submit',
  'announcement:approve',
  'announcement:reject',
  'announcement:publish',
  'announcement:schedule',
  'announcement:unpublish',
  'announcement:archive',
  'announcement:restore',
  'announcement:revision',
  'announcement:preview',
  'announcement:view_history'
] as const;

export const ONLINE_LESSON_PERMISSIONS = [
  'online_lesson:read_public',
  'online_lesson:read_all',
  'online_lesson:create',
  'online_lesson:edit_draft',
  'online_lesson:delete_draft',
  'online_lesson:submit',
  'online_lesson:approve',
  'online_lesson:reject',
  'online_lesson:publish',
  'online_lesson:schedule',
  'online_lesson:unpublish',
  'online_lesson:archive',
  'online_lesson:restore',
  'online_lesson:revision',
  'online_lesson:preview',
  'online_lesson:view_history'
] as const;

export const REPORT_PERMISSIONS = [
  'reports:self_read',
  'reports:organization_read',
  'reports:export',
  'reports:export_excel',
  'reports:export_pdf'
] as const;

export const AUDIT_LOG_PERMISSIONS = ['audit_logs:read', 'audit_logs:export'] as const;

export const NOTIFICATION_PERMISSIONS = ['notifications:read', 'notifications:manage'] as const;

export const ALL_PERMISSIONS = [
  ...ORGANIZATION_PERMISSIONS,
  ...PERMISSION_TEMPLATE_PERMISSIONS,
  ...USER_PERMISSION_PERMISSIONS,
  ...USER_MANAGEMENT_PERMISSIONS,
  ...EMPLOYEE_DIRECTORY_PERMISSIONS,
  ...COURSE_AND_MATRIX_PERMISSIONS,
  ...TRAINING_RECORD_PERMISSIONS,
  ...ANNOUNCEMENT_PERMISSIONS,
  ...ONLINE_LESSON_PERMISSIONS,
  ...REPORT_PERMISSIONS,
  ...AUDIT_LOG_PERMISSIONS,
  ...NOTIFICATION_PERMISSIONS
] as const;

export type Permission = (typeof ALL_PERMISSIONS)[number];

export const DEFAULT_OWNER_PERMISSIONS: Permission[] = [
  ...ORGANIZATION_PERMISSIONS,
  ...USER_MANAGEMENT_PERMISSIONS,
  ...EMPLOYEE_DIRECTORY_PERMISSIONS,
  ...COURSE_AND_MATRIX_PERMISSIONS,
  ...TRAINING_RECORD_PERMISSIONS,
  ...ANNOUNCEMENT_PERMISSIONS,
  ...ONLINE_LESSON_PERMISSIONS,
  ...REPORT_PERMISSIONS,
  ...AUDIT_LOG_PERMISSIONS,
  ...NOTIFICATION_PERMISSIONS
];

export const DEFAULT_ADMIN_PERMISSIONS: Permission[] = [...DEFAULT_OWNER_PERMISSIONS];

export const DEFAULT_MEMBER_PERMISSIONS: Permission[] = [
  'training_record:self_read',
  'training_record:self_write',
  'training_record:submit',
  'announcement:read_public',
  'online_lesson:read_public',
  'reports:self_read',
  'notifications:read'
];

export function isPermission(value: string): value is Permission {
  return (ALL_PERMISSIONS as readonly string[]).includes(value);
}

export function normalizePermissions(values: readonly string[] | null | undefined): Permission[] {
  if (!values?.length) {
    return [];
  }

  return values.filter(isPermission);
}
