// RBAC 역할 위계: SUPERADMIN ⊃ EDITOR ⊃ AUTHOR ⊃ VIEWER
export const ROLE_RANK: Record<string, number> = {
  SUPERADMIN: 4,
  EDITOR: 3,
  AUTHOR: 2,
  VIEWER: 1,
}

export const CMS_ROLES = ['SUPERADMIN', 'EDITOR', 'AUTHOR', 'VIEWER']

export function currentRole(): string {
  return (localStorage.getItem('cms_role') || 'VIEWER').toUpperCase()
}

/** role 이 min 이상 권한인가 */
export function hasRole(min: string, role = currentRole()): boolean {
  return (ROLE_RANK[role] || 0) >= (ROLE_RANK[min.toUpperCase()] || 99)
}

export const canWrite = (role = currentRole()) => hasRole('AUTHOR', role)        // 작성/수정/삭제
export const canPublish = (role = currentRole()) => hasRole('EDITOR', role)       // 게시 승인/발행
export const isSuperAdmin = (role = currentRole()) => hasRole('SUPERADMIN', role) // 관리자 API
