import type { TeamMemberRole } from '@/components/teams/TeamRoster';

interface TeamMember {
  driver: {
    id: string;
    name: string;
    country: string;
  };
  role: TeamMemberRole;
  joinedAt: string;
  rating: number | null;
  overallRank: number | null;
}

export function sortMembers(members: TeamMember[], sortBy: string) {
  function getRoleOrder(role: TeamMemberRole): number {
    switch (role) {
      case 'owner':
        return 0;
      case 'manager':
        return 1;
      case 'member':
        return 2;
      default:
        return 3;
    }
  }

  const list = [...members];
  list.sort((a, b) => {
    switch (sortBy) {
      case 'rating': {
        const ratingA = a.rating ?? 0;
        const ratingB = b.rating ?? 0;
        return ratingB - ratingA;
      }
      case 'role': {
        return getRoleOrder(a.role) - getRoleOrder(b.role);
      }
      case 'name': {
        return a.driver.name.localeCompare(b.driver.name);
      }
      default:
        return 0;
    }
  });
  return list;
}
