package cluster

import (
	"fmt"

	coreapi "github.com/tidbcloud/core-svc/idl/pbgen/proto/core"
	"go.opentelemetry.io/otel"

	globaldedicatedv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/dedicated/v1"
)

var Tracer = otel.Tracer("core")

func cvtToCoreComponentType(t globaldedicatedv1.ComponentType) (ct coreapi.ComponentType, err error) {
	switch t {
	case globaldedicatedv1.ComponentType_TIDB:
		ct = coreapi.ComponentType_COMPONENT_TYPE_TIDB
	case globaldedicatedv1.ComponentType_TIKV:
		ct = coreapi.ComponentType_COMPONENT_TYPE_TIKV
	case globaldedicatedv1.ComponentType_TIFLASH:
		ct = coreapi.ComponentType_COMPONENT_TYPE_TIFLASH
	case globaldedicatedv1.ComponentType_PD:
		ct = coreapi.ComponentType_COMPONENT_TYPE_PD
	case globaldedicatedv1.ComponentType_TIPROXY:
		ct = coreapi.ComponentType_COMPONENT_TYPE_TIPROXY
	case globaldedicatedv1.ComponentType_COMPONENT_TYPE_UNSPECIFIED:
		ct = coreapi.ComponentType_COMPONENT_TYPE_UNSPECIFIED
	default:
		return coreapi.ComponentType_COMPONENT_TYPE_UNSPECIFIED, fmt.Errorf("unsupport component type %s", t)
	}

	return ct, nil
}

func cvtFromCoreComponentType(t coreapi.ComponentType) (ct globaldedicatedv1.ComponentType, err error) {
	switch t {
	case coreapi.ComponentType_COMPONENT_TYPE_TIDB:
		ct = globaldedicatedv1.ComponentType_TIDB
	case coreapi.ComponentType_COMPONENT_TYPE_TIKV:
		ct = globaldedicatedv1.ComponentType_TIKV
	case coreapi.ComponentType_COMPONENT_TYPE_TIFLASH:
		ct = globaldedicatedv1.ComponentType_TIFLASH
	case coreapi.ComponentType_COMPONENT_TYPE_PD:
		ct = globaldedicatedv1.ComponentType_PD
	case coreapi.ComponentType_COMPONENT_TYPE_TIPROXY:
		ct = globaldedicatedv1.ComponentType_TIPROXY
	case coreapi.ComponentType_COMPONENT_TYPE_UNSPECIFIED:
		ct = globaldedicatedv1.ComponentType_COMPONENT_TYPE_UNSPECIFIED
	default:
		return globaldedicatedv1.ComponentType_COMPONENT_TYPE_UNSPECIFIED, fmt.Errorf("unsupport component type %s", t)
	}

	return ct, nil
}
