package util

import (
	"fmt"

	"github.com/tidbcloud/metrics-server/central/db/entity/capacity"
	"github.com/tidbcloud/metrics-server/idl/pbgen/proto/core"
	"github.com/tidbcloud/metrics-server/pkg/tidb/component"
)

// TODO(chenfei): opt,move to better place

func CoreComponentType2componentName(tp core.ComponentType) component.Name {
	switch tp {
	case core.ComponentType_COMPONENT_TYPE_TIDB:
		return component.TiDB
	case core.ComponentType_COMPONENT_TYPE_PD:
		return component.PD
	case core.ComponentType_COMPONENT_TYPE_TIKV:
		return component.TiKV
	case core.ComponentType_COMPONENT_TYPE_TIFLASH:
		return component.TiFlash
	case core.ComponentType_COMPONENT_TYPE_TICDC:
		return component.TiCDC
	}
	panic(fmt.Sprintf("invalid tp: %s", tp))
}

func CoreClusterComponents2componentResources(components []*core.ClusterComponent) []component.Resources {
	var out []component.Resources
	for _, each := range components {
		machineType := capacity.MachineType(each.ComputeInstanceType)

		out = append(out, component.Resources{
			ComponentName: CoreComponentType2componentName(each.ComponentType),
			Image:         nil,
			Capacity: capacity.Capacity{
				MachineType: &machineType,
				Replicas:    uint32(each.Replicas),
				Compute: &capacity.Compute{
					MemoryMi: uint32(each.MemoryGi * 1024),
					CPU:      float32(each.CpuNum),
				},
				Disk: &capacity.Disk{
					SizeGi: uint32(each.StorageSizeGi),
					Type:   capacity.DiskType(each.StorageInstanceType),
				},
				Node: &capacity.Node{
					NodeProfileID: each.NodeProfileId,
					Name:          each.NodeProfileName,
				},
			},
		})
	}
	return out
}
