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

export const MES_ROLES = ['SUPERADMIN', 'MANAGER', 'WORKER', 'VIEWER']

export function currentRole(): string {
  return (localStorage.getItem('mes_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('WORKER', role)        // 실적/검사/입출고 입력
export const canManage = (role = currentRole()) => hasRole('MANAGER', role)       // 기준정보/마감/승인
export const isSuperAdmin = (role = currentRole()) => hasRole('SUPERADMIN', role) // 관리자 API
