/**
 * Domain Types/Utilities: LeagueRoles
 *
 * Utility functions for working with league membership roles.
 */

import type { MembershipRoleValue } from '../entities/MembershipRole';

/**
 * Role hierarchy (higher number = more authority)
 */
const ROLE_HIERARCHY: Record<MembershipRoleValue, number> = {
  member: 0,
  steward: 1,
  admin: 2,
  owner: 3,
};

/**
 * Check if a role is at least steward level
 */
export function isLeagueStewardOrHigherRole(role: MembershipRoleValue): boolean {
  return ROLE_HIERARCHY[role] >= ROLE_HIERARCHY.steward;
}

/**
 * Check if a role is at least admin level
 */
export function isLeagueAdminOrHigherRole(role: MembershipRoleValue): boolean {
  return ROLE_HIERARCHY[role] >= ROLE_HIERARCHY.admin;
}

/**
 * Check if a role is owner
 */
export function isLeagueOwnerRole(role: MembershipRoleValue): boolean {
  return role === 'owner';
}

/**
 * Compare two roles
 * Returns positive if role1 > role2, negative if role1 < role2, 0 if equal
 */
export function compareRoles(role1: MembershipRoleValue, role2: MembershipRoleValue): number {
  return ROLE_HIERARCHY[role1] - ROLE_HIERARCHY[role2];
}

/**
 * Get role display name
 */
export function getRoleDisplayName(role: MembershipRoleValue): string {
  const names: Record<MembershipRoleValue, string> = {
    member: 'Member',
    steward: 'Steward',
    admin: 'Admin',
    owner: 'Owner',
  };
  return names[role] || role;
}

/**
 * Get all roles in order of hierarchy
 */
export function getAllRolesOrdered(): MembershipRoleValue[] {
  return ['member', 'steward', 'admin', 'owner'];
}

/**
 * Get roles that can be assigned (excludes owner as it's transferred, not assigned)
 */
export function getAssignableRoles(): MembershipRoleValue[] {
  return ['member', 'steward', 'admin'];
}