// Package ticdc implements the read-only TiCDC metadata boundary exposed by
// Clinic Data Proxy.
package ticdc

import "errors"

var (
	ErrInvalidArgument = errors.New("invalid TiCDC metadata request")
	ErrForbidden       = errors.New("TiCDC metadata request forbidden")
	ErrNotFound        = errors.New("TiCDC metadata not found")
	ErrUpstream        = errors.New("TiCDC metadata upstream failure")
	ErrUnavailable     = errors.New("TiCDC metadata upstream unavailable")
)

type Architecture string

const (
	ArchitectureDedicated Architecture = "dedicated"
	ArchitectureNextGen   Architecture = "nextgen"
	ArchitectureEssential Architecture = "essential"
)

type ListResponse struct {
	Changefeeds []*Changefeed `json:"changefeeds"`
}

type DetailResponse struct {
	Changefeed *Changefeed `json:"changefeed"`
}

type Changefeed struct {
	ID               string                 `json:"id"`
	DisplayID        string                 `json:"display_id"`
	ClusterType      string                 `json:"cluster_type"`
	Arch             string                 `json:"arch,omitempty"`
	DisplayName      string                 `json:"display_name"`
	DownstreamType   string                 `json:"downstream_type"`
	Status           string                 `json:"status"`
	CreatedAt        int64                  `json:"created_at"`
	TiCDCClusterName string                 `json:"ticdc_cluster_name,omitempty"`
	TiCDCCluster     *TiCDCCluster          `json:"ticdc_cluster"`
	NetworkInfo      *NetworkInfo           `json:"network_info,omitempty"`
	CheckpointTSO    string                 `json:"checkpoint_tso,omitempty"`
	CheckpointTS     int64                  `json:"checkpoint_ts,omitempty"`
	RCU              string                 `json:"rcu,omitempty"`
	TableConfig      *TableConfig           `json:"table_config,omitempty"`
	StartPosition    *StartPosition         `json:"start_position,omitempty"`
	DownstreamConfig map[string]interface{} `json:"downstream_config,omitempty"`
}

type NetworkInfo struct {
	NetworkType            string            `json:"network_type"`
	PrivateLinkServiceName string            `json:"private_link_service_name,omitempty"`
	KafkaID                string            `json:"kafka_id,omitempty"`
	Ports                  []uint32          `json:"ports,omitempty"`
	AdvertisedPatternGroup map[string]string `json:"advertised_pattern_group,omitempty"`
	SinkEndpointID         string            `json:"sink_endpoint_id,omitempty"`
}

type TableConfig struct {
	FilterRules   []string       `json:"filter_rules"`
	Mode          string         `json:"mode"`
	CaseSensitive bool           `json:"case_sensitive"`
	EventFilters  []*EventFilter `json:"event_filters"`
}

type EventFilter struct {
	TableMatchers                   []string `json:"table_matchers"`
	IgnoredEvents                   []string `json:"ignored_events"`
	IgnoredSQLStatements            []string `json:"ignored_sql_statements"`
	IgnoredInsertValueExpression    string   `json:"ignored_insert_value_expression,omitempty"`
	IgnoredUpdateNewValueExpression string   `json:"ignored_update_new_value_expression,omitempty"`
	IgnoredUpdateOldValueExpression string   `json:"ignored_update_old_value_expression,omitempty"`
	IgnoredDeleteValueExpression    string   `json:"ignored_delete_value_expression,omitempty"`
}

type StartPosition struct {
	Mode      string `json:"mode"`
	TSO       string `json:"tso,omitempty"`
	Timestamp string `json:"timestamp,omitempty"`
}

type TiCDCCluster struct {
	ID                string `json:"id"`
	Name              string `json:"name"`
	ClusterType       string `json:"cluster_type"`
	TiDBClusterID     string `json:"tidb_cluster_id"`
	Version           string `json:"version"`
	Region            string `json:"region"`
	ProviderType      string `json:"provider_type"`
	SpecificationCode string `json:"specification_code"`
	Status            string `json:"status"`
	CreatedAt         int64  `json:"created_at"`
	DeployType        string `json:"deploy_type"`
	ControlPlaneArch  string `json:"control_plane_arch"`
	OperatorVersion   string `json:"operator_version"`
}
