package v1alpha1

import (
	corev1 "k8s.io/api/core/v1"
)

const DefaultMaxWebhookPayloadSize int64 = 1048576 // 1MB

// WebhookContext holds a general purpose REST API context
type WebhookContext struct {
	// REST API endpoint
	Endpoint string `json:"endpoint" protobuf:"bytes,1,opt,name=endpoint"`
	// Method is HTTP request method that indicates the desired action to be performed for a given resource.
	// See RFC7231 Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
	Method string `json:"method" protobuf:"bytes,2,opt,name=method"`
	// Port on which HTTP server is listening for incoming events.
	Port string `json:"port" protobuf:"bytes,3,opt,name=port"`
	// URL is the url of the server.
	URL string `json:"url" protobuf:"bytes,4,opt,name=url"`
	// ServerCertPath refers the file that contains the cert.
	ServerCertSecret *corev1.SecretKeySelector `json:"serverCertSecret,omitempty" protobuf:"bytes,5,opt,name=serverCertSecret"`
	// ServerKeyPath refers the file that contains private key
	ServerKeySecret *corev1.SecretKeySelector `json:"serverKeySecret,omitempty" protobuf:"bytes,6,opt,name=serverKeySecret"`
	// Metadata holds the user defined metadata which will passed along the event payload.
	// +optional
	Metadata map[string]string `json:"metadata,omitempty" protobuf:"bytes,7,rep,name=metadata"`
	// AuthSecret holds a secret selector that contains a bearer token for authentication
	// +optional
	AuthSecret *corev1.SecretKeySelector `json:"authSecret,omitempty" protobuf:"bytes,8,opt,name=authSecret"`
	// MaxPayloadSize is the maximum webhook payload size that the server will accept.
	// Requests exceeding that limit will be rejected with "request too large" response.
	// Default value: 1048576 (1MB).
	// +optional
	MaxPayloadSize *int64 `json:"maxPayloadSize,omitempty" protobuf:"bytes,9,opt,name=maxPayloadSize"`
}

func (wc *WebhookContext) GetMaxPayloadSize() int64 {
	maxPayloadSize := DefaultMaxWebhookPayloadSize
	if wc != nil && wc.MaxPayloadSize != nil {
		maxPayloadSize = *wc.MaxPayloadSize
	}

	return maxPayloadSize
}
