package virtualtables

import "context"

type clientInfoProviderKey struct{}

// ClientInfo describes an active client connection.
type ClientInfo struct {
	TenantID        string
	Address         string
	Port            int
	ProtocolVersion int
	Username        string
	Hostname        string
	DriverName      string
	DriverVersion   string
	RequestCount    int64
	SSLEnabled      bool
	SSLProtocol     string
	SSLCipherSuite  string
	Keyspace        string
	ClientOptions   map[string]string
}

// ClientInfoProvider returns active client connections.
type ClientInfoProvider func() []ClientInfo

// WithClientInfoProvider stores a client info provider in the context.
func WithClientInfoProvider(ctx context.Context, provider ClientInfoProvider) context.Context {
	if ctx == nil || provider == nil {
		return ctx
	}
	return context.WithValue(ctx, clientInfoProviderKey{}, provider)
}

func clientInfoProviderFromContext(ctx context.Context) (ClientInfoProvider, bool) {
	if ctx == nil {
		return nil, false
	}
	if value := ctx.Value(clientInfoProviderKey{}); value != nil {
		if provider, ok := value.(ClientInfoProvider); ok {
			return provider, true
		}
	}
	return nil, false
}
