package utils

import (
	"context"
	"net/http"

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

// auth_helper gives tool to our authorization services so that future middleware components and actual endpoints will have access
// to user data needed for specific granular control

func AUTHWriteProviderToRequest(provider string, r *http.Request) *http.Request {
	newCTX := AUTHWriteProviderToContext(provider, r.Context())

	r = r.Clone(newCTX)
	return r
}

func AUTHWriteProviderToContext(provider string, ctx context.Context) (newCTX context.Context) {
	newCTX = context.WithValue(ctx, c.ProviderKey, provider)
	return newCTX
}

// If provider is not in context, will return an empty string
func AUTHGetProviderFromRequest(r *http.Request) string {
	return AUTHGetProviderFromContext(r.Context())
}

// If provider is not in context, will return an empty string
func AUTHGetProviderFromContext(ctx context.Context) string {
	val, ok := ctx.Value(c.ProviderKey).(string)

	if !ok {
		return ""
	}
	return val
}
