/** Normalize role input to array for multi-role support */
export function normalizeRoles(role: string | string[]): string[] {
  return Array.isArray(role) ? role : [role];
}

/** Check if any of the user's roles is in the allowed list */
export function hasAnyRole(userRoles: string[], allowed: string[]): boolean {
  return userRoles.some((r) => allowed.includes(r));
}

/** Get the highest-privilege role from a list */
export function getPrimaryRole(roles: string[]): string {
  const priority = [
    'super_admin',
    'club_admin',
    'manager',
    'coordinator',
    'receptionist',
    'trainer',
  ];
  for (const p of priority) {
    if (roles.includes(p)) return p;
  }
  return roles[0] || '';
}

export abstract class EntityPermissions {
  abstract canCreate(userId: string, role: string | string[], clubId: string | null): boolean;

  abstract canRead(
    userId: string,
    role: string | string[],
    clubId: string | null,
    entityClubId: string | null,
  ): boolean;

  abstract canUpdate(
    userId: string,
    role: string | string[],
    clubId: string | null,
    entity: any,
  ): boolean;

  abstract canDelete(userId: string, role: string | string[], clubId: string | null): boolean;

  /** Return additional Prisma where-clause for row-level filtering */
  abstract filterByRole(
    userId: string,
    role: string | string[],
    clubId: string | null,
  ): Record<string, any>;
}
