package httphandlers

import (
	"context"

	"github.com/fiskerinc/cloud-services/pkg/adminroles"
	"github.com/fiskerinc/cloud-services/pkg/common/authproviders"
	c "github.com/fiskerinc/cloud-services/pkg/common/context"
)

type AuthBase struct {
}

func (a AuthBase) hasRoles(requiredRoles map[string][]adminroles.RoleID) bool {
	if len(requiredRoles) == 0 {
		return false
	}

	for _, roles := range requiredRoles {
		if len(roles) > 0 {
			return true
		}
	}

	return false
}

func (a AuthBase) getRolesForProvider(provider string, required map[string][]adminroles.RoleID) (roles []adminroles.RoleID, ok bool) {
	if roles, ok = required[provider]; ok {
		return
	}

	roles, ok = required[authproviders.Default]
	return
}

func (a AuthBase) addContext(ctx context.Context, key c.ContextType, value string) context.Context {
	return context.WithValue(ctx, key, value)
}
