package v1

import (
	acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// FabricEventStream defines FabricEventStream Custom Resource Definition Object.
// +k8s:deepcopy-gen=true
type FabricEventStream struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata"`

	Spec FabricEventStreamSpec `json:"spec"`
}

// FabricEventStreamSpec defines the specification for the FabricEventStream TPR.
// +k8s:deepcopy-gen=true
type FabricEventStreamSpec struct {
	ApplicationId string        `json:"applicationId"`
	EventStreams  []EventStream `json:"eventStreams"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// FabricEventStreamList defines a list of FabricEventStreams .
// +k8s:deepcopy-gen=true
type FabricEventStreamList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`

	Items []FabricEventStream `json:"items"`
}

// EventStream defines the source, flow and sink of the event stream
// +k8s:deepcopy-gen=true
type EventStream struct {
	EventStreamFlow     EventStreamFlow     `json:"flow"`
	EventStreamSink     EventStreamSink     `json:"sink"`
	EventStreamSource   EventStreamSource   `json:"source"`
	EventStreamRecovery EventStreamRecovery `json:"recovery"`
}

// EventStreamFlow defines the flow characteristics of the event stream
// +k8s:deepcopy-gen=true
type EventStreamFlow struct {
	Type          string  `json:"type"`
	PayloadColumn *string `json:"payloadColumn,omitempty"`
}

// EventStreamSink defines the target of the event stream
// +k8s:deepcopy-gen=true
type EventStreamSink struct {
	Type         string  `json:"type"`
	EventType    string  `json:"eventType,omitempty"`
	MaxBatchSize *uint32 `json:"maxBatchSize,omitempty"`
}

// EventStreamRecovery defines the target of dead letter queue
// +k8s:deepcopy-gen=true
type EventStreamRecovery struct {
	Type string           `json:"type"`
	Sink *EventStreamSink `json:"sink"`
}

// EventStreamSource defines the source of the event stream and connection for FES operator
// +k8s:deepcopy-gen=true
type EventStreamSource struct {
	Type             string           `json:"type"`
	Schema           string           `json:"schema,omitempty" defaults:"public"`
	EventStreamTable EventStreamTable `json:"table"`
	Filter           *string          `json:"filter,omitempty"`
	Connection       Connection       `json:"jdbcConnection"`
}

// EventStreamTable defines the name and ID column to be used for streaming
// +k8s:deepcopy-gen=true
type EventStreamTable struct {
	Name     string  `json:"name"`
	IDColumn *string `json:"idColumn,omitempty"`
}

// Connection to be used for allowing the FES operator to connect to a database
// +k8s:deepcopy-gen=true
type Connection struct {
	Url             string  `json:"jdbcUrl"`
	SlotName        string  `json:"slotName"`
	PluginType      string  `json:"pluginType,omitempty"`
	PublicationName *string `json:"publicationName,omitempty"`
	DBAuth          DBAuth  `json:"databaseAuthentication"`
}

// DBAuth specifies the credentials to be used for connecting with the database
// +k8s:deepcopy-gen=true
type DBAuth struct {
	Type        string `json:"type"`
	Name        string `json:"name,omitempty"`
	UserKey     string `json:"userKey,omitempty"`
	PasswordKey string `json:"passwordKey,omitempty"`
}

type Slot struct {
	Slot        map[string]string             `json:"slot"`
	Publication map[string]acidv1.StreamTable `json:"publication"`
}
