/**
 * User roles enum
 * Used across backend, API, and frontend
 * We need to use a plain enum here for typeORM which is why the enum may differ from the frontend implementation
 * However, we must ensure that the content of both enums is identical!
 */
export enum UserRole {
    USER = "user",
    ADMIN = "admin",
}

/**
 * Helper functions for UserRole
 */
export const UserRoleHelper = {
    /**
     * Get all role values as array
     */
    getAllRoles(): UserRole[] {
        return Object.values(UserRole);
    },

    /**
     * Check if a string is a valid role
     */
    isValidRole(value: string): value is UserRole {
        return Object.values(UserRole).includes(value as UserRole);
    },

    /**
     * Get role from string with validation
     */
    fromString(value: string): UserRole {
        if (this.isValidRole(value)) {
            return value as UserRole;
        }
        throw new Error(`Invalid role: ${value}`);
    },

    /**
     * Get display name for role (German)
     */
    getDisplayName(role: UserRole): string {
        const displayNames: Record<UserRole, string> = {
            [UserRole.USER]: "Benutzer",
            [UserRole.ADMIN]: "Administrator",
        };
        return displayNames[role];
    },

    /**
     * Get all roles with display names for dropdowns
     */
    getRoleOptions(): Array<{ value: UserRole; label: string }> {
        return this.getAllRoles().map((role) => ({
            value: role,
            label: this.getDisplayName(role),
        }));
    },
};