// composables/useRoles.ts
// Composable for role-based access control on client side

export const useRoles = () => {
    const { user } = useUserInfo();
    
    /**
     * Check if current user has a specific role
     */
    const hasRole = (role: string): boolean => {
        const currentUser = user.value;
        if (!currentUser || !currentUser.roles) {
            return false;
        }
        
        return currentUser.roles.includes(role);
    };
    
    /**
     * Check if current user has any of the specified roles
     */
    const hasAnyRole = (roles: string[]): boolean => {
        const currentUser = user.value;
        if (!currentUser || !currentUser.roles) {
            return false;
        }
        
        return roles.some(role => currentUser.roles?.includes(role));
    };
    
    /**
     * Check if current user has all of the specified roles
     */
    const hasAllRoles = (roles: string[]): boolean => {
        const currentUser = user.value;
        if (!currentUser || !currentUser.roles) {
            return false;
        }
        
        return roles.every(role => currentUser.roles?.includes(role));
    };
    
    /**
     * Get all roles of current user
     */
    const getRoles = (): string[] => {
        return user.value?.roles || [];
    };
    
    /**
     * Get realm roles only
     */
    const getRealmRoles = (): string[] => {
        return (user.value as any)?.realm_roles || [];
    };
    
    /**
     * Get client roles only
     */
    const getClientRoles = (): string[] => {
        return (user.value as any)?.client_roles || [];
    };
    
    return {
        hasRole,
        hasAnyRole,
        hasAllRoles,
        getRoles,
        getRealmRoles,
        getClientRoles
    };
};
