package entity

import (
	"fmt"

	apiv1 "github.com/tidbcloud/cluster-service-ng/api/cluster-service-ng/v1beta"
)

const (
	TiProxyGracefulShutdownDeleteDelaySecondsAnnotation = "core.pingcap.com/tiproxy-graceful-shutdown-delete-delay-seconds"
	DefaultTiProxyGracefulShutdownDeleteDelaySeconds    = int64(24 * 60 * 60)
	DefaultTiProxyGracefulRestartMaxSurge               = int32(1024)

	TiProxyHealthcheckPath             = "/api/debug/health"
	TiProxyHealthcheckPort             = "3080"
	TiProxyAWSHealthcheckProtocol      = "http"
	TiProxyAliCloudHealthcheckProtocol = "http"
	TiProxyAzureHealthcheckProtocol    = "http"

	AWSLBHealthcheckPathKey     = "service.beta.kubernetes.io/aws-load-balancer-healthcheck-path"
	AWSLBHealthcheckPortKey     = "service.beta.kubernetes.io/aws-load-balancer-healthcheck-port"
	AWSLBHealthcheckProtocolKey = "service.beta.kubernetes.io/aws-load-balancer-healthcheck-protocol"

	AliCloudLBHealthcheckFlagKey           = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-flag"
	AliCloudLBHealthcheckTypeKey           = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-type"
	AliCloudLBHealthcheckURIKey            = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-uri"
	AliCloudLBHealthcheckDomainKey         = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-domain"
	AliCloudLBHealthcheckConnectPortKey    = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-connect-port"
	AliCloudLBHealthcheckConnectTimeoutKey = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-connect-timeout"
	AliCloudLBHealthyThresholdKey          = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-healthy-threshold"
	AliCloudLBUnhealthyThresholdKey        = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-unhealthy-threshold"
	AliCloudLBHealthcheckIntervalKey       = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-interval"
	AliCloudLBHealthcheckMethodKey         = "service.beta.kubernetes.io/alibaba-cloud-loadbalancer-health-check-method"

	AzureLBHealthcheckProtocolKeyFormat    = "service.beta.kubernetes.io/port_%d_health-probe_protocol"
	AzureLBHealthcheckPortKeyFormat        = "service.beta.kubernetes.io/port_%d_health-probe_port"
	AzureLBHealthcheckRequestPathKeyFormat = "service.beta.kubernetes.io/port_%d_health-probe_request-path"
	AzureLBHealthcheckIntervalKeyFormat    = "service.beta.kubernetes.io/port_%d_health-probe_interval"
	AzureLBHealthcheckNumOfProbeKeyFormat  = "service.beta.kubernetes.io/port_%d_health-probe_num-of-probe"
)

func BuildTiProxyGracefulRestartLoadBalancerAnnotations(sharedPool *SharedPool, servicePorts ...int32) map[string]string {
	if sharedPool == nil {
		return nil
	}

	switch sharedPool.GetProvider().GetType() {
	case apiv1.ProviderType_AWS:
		return map[string]string{
			AWSLBHealthcheckPathKey:     TiProxyHealthcheckPath,
			AWSLBHealthcheckPortKey:     TiProxyHealthcheckPort,
			AWSLBHealthcheckProtocolKey: TiProxyAWSHealthcheckProtocol,
		}
	case apiv1.ProviderType_ALICLOUD:
		return map[string]string{
			AliCloudLBHealthcheckFlagKey:           "on",
			AliCloudLBHealthcheckTypeKey:           TiProxyAliCloudHealthcheckProtocol,
			AliCloudLBHealthcheckURIKey:            TiProxyHealthcheckPath,
			AliCloudLBHealthcheckDomainKey:         "$SERVER_IP",
			AliCloudLBHealthcheckConnectPortKey:    TiProxyHealthcheckPort,
			AliCloudLBHealthcheckConnectTimeoutKey: "5",
			AliCloudLBHealthyThresholdKey:          "2",
			AliCloudLBUnhealthyThresholdKey:        "2",
			AliCloudLBHealthcheckIntervalKey:       "5",
			AliCloudLBHealthcheckMethodKey:         "GET",
		}
	case apiv1.ProviderType_AZURE:
		annotations := map[string]string{}
		for _, port := range servicePorts {
			if port <= 0 {
				continue
			}
			annotations[fmt.Sprintf(AzureLBHealthcheckProtocolKeyFormat, port)] = TiProxyAzureHealthcheckProtocol
			annotations[fmt.Sprintf(AzureLBHealthcheckPortKeyFormat, port)] = TiProxyHealthcheckPort
			annotations[fmt.Sprintf(AzureLBHealthcheckRequestPathKeyFormat, port)] = TiProxyHealthcheckPath
			annotations[fmt.Sprintf(AzureLBHealthcheckIntervalKeyFormat, port)] = "5"
			annotations[fmt.Sprintf(AzureLBHealthcheckNumOfProbeKeyFormat, port)] = "2"
		}
		if len(annotations) == 0 {
			return nil
		}
		return annotations
	default:
		return nil
	}
}
