//go:build control
// +build control

package utils

import (
	"context"

	"gopkg.in/yaml.v2"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type AgentConfig struct {
	Api       AgentApi             `yaml:"api,omitempty"`
	Sources   map[string]Source    `yaml:"sources,omitempty"`
	TransForm map[string]TransForm `yaml:"transforms,omitempty"`
	Sinks     map[string]Sink      `yaml:"sinks,omitempty"`
}

type AgentApi struct {
	Address    string `yaml:"address,omitempty"`
	Enabled    bool   `yaml:"enabled"`
	Playground bool   `yaml:"playground"`
}

type Source struct {
	Type     string `yaml:"type"`
	Format   string `yaml:"format"`
	Interval int    `yaml:"interval"`
}

type TransForm struct {
	Type    string   `yaml:"type"`
	Inputs  []string `yaml:"inputs"`
	Metrics []Metric `yaml:"metrics,omitempty"`
	Source  string   `yaml:"source,omitempty"`
}
type Metric struct {
	Type      string            `yaml:"type"`
	Field     string            `yaml:"field"`
	Name      string            `yaml:"name"`
	NameSpace string            `yaml:"namespace,omitempty"`
	Tags      map[string]string `yaml:"tags,omitempty"`
}

type Sink struct {
	Type             string   `yaml:"type"`
	Inputs           []string `yaml:"inputs"`
	Target           string   `yaml:"target,omitempty"`
	Encoding         Encode   `yaml:"encoding,omitempty"`
	EndPoint         string   `yaml:"endpoint,omitempty"`
	Healthcheck      bool     `yaml:"healthcheck"`
	Request          Reqs     `yaml:"request,omitempty"`
	Acknowledgements string   `yaml:"acknowledgements,omitempty"`
	Region           string   `yaml:"region,omitempty"`
	Bucket           string   `yaml:"bucket,omitempty"`
	KeyPrefix        string   `yaml:"key_prefix,omitempty"`
	Compression      string   `yaml:"compression,omitempty"`
	Auth             Role     `yaml:"auth,omitempty"`
	// todo
	Batch  Batch  `yaml:"batch,omitempty"`
	Buffer Buffer `yaml:"buffer,omitempty"`
}

type Role struct {
	AssumeRole string `yaml:"assume_role,omitempty"`
}

type Reqs struct {
	RetryAttempts int `yaml:"retry_attempts,omitempty"`
}

type Encode struct {
	Codec string `yaml:"codec,omitempty"`
}

type Batch struct {
	MaxBytes    int64 `yaml:"max_bytes"`
	TimeoutSecs int64 `yaml:"timeout_secs"`
}

type Buffer struct {
	Type    string `yaml:"type"`
	MaxSize int64  `yaml:"max_size"`
}

type StartWriteParam struct {
	ClusterID  string
	NameSpace  string //agent namespace
	DNS        string
	Name       string //agent name
	TenantID   string
	AssumeRole string
}

const (
	sinkName     = "my_sink_id"
	configMapKey = "agent.yaml"
)

func StopAgentWrite(ns, name string) {
	configmap, err := kubeClient.CoreV1().ConfigMaps(ns).Get(context.TODO(), name, metav1.GetOptions{})
	if err != nil {
		panic(err)
	}
	//var out vector.
	var vectorConfig AgentConfig
	err = yaml.Unmarshal([]byte(configmap.Data["agent.yaml"]), &vectorConfig)
	if err != nil {
		panic(err)
	}
	writeSink := make(map[string]Sink)
	writeSink[sinkName] = Sink{
		Type: "console",
		Inputs: []string{
			"my_transform",
		},
		Target: "stdout",
		Encoding: Encode{
			Codec: "json",
		},
	}

	vectorConfig.Sinks = writeSink
	res, err := yaml.Marshal(vectorConfig)
	if err != nil {
		panic(err)
	}
	configmap.Data[configMapKey] = string(res)
	kubeClient.CoreV1().ConfigMaps(ns).Update(context.TODO(), configmap, metav1.UpdateOptions{})
}
