package grpcclient

import (
	"time"

	"google.golang.org/grpc/keepalive"
)

// Option configures a Client.
type Option func(*Client)

// WithAPIKey sets the API key used for every StreamAlerts stream.
// The key is sent as "Bearer <key>" in the grpc-metadata
// "authorization" header on each stream.
//
// The format is "<company_id>:<source_id>:<secret>".
func WithAPIKey(k string) Option {
	return func(c *Client) { c.apiKey = k }
}

// WithMaxRetries sets the maximum number of retry attempts for transient
// errors (RATE_LIMITED, UNAVAILABLE).  Zero disables retries.
// The backoff between retries follows Error.retry_after_ms from the server
// when available, otherwise exponential backoff starting at 100ms.
func WithMaxRetries(n int) Option {
	return func(c *Client) { c.maxRetries = n }
}

// WithKeepalive configures the gRPC keepalive parameters on the underlying
// connection. Time is the interval between keepalive probes; Timeout is how
// long the peer has to respond before the connection is closed.
func WithKeepalive(time, timeout time.Duration) Option {
	return func(c *Client) {
		c.keepalive = keepalive.ClientParameters{
			Time:                time,
			Timeout:             timeout,
			PermitWithoutStream: false,
		}
	}
}

// WithInsecure disables transport security.  Appropriate for development
// only (e.g. connecting to ingestd on localhost).  In production, connections
// to ingestd should use mTLS or at minimum TLS, configured via a
// grpc.DialOption built with credentials.NewTLS.
func WithInsecure() Option {
	return func(c *Client) { c.insecure = true }
}
