package dataplane

import (
	"context"
	"fmt"

	apierrors "k8s.io/apimachinery/pkg/api/errors"
	"sigs.k8s.io/controller-runtime/pkg/client"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
	v1 "github.com/tidbcloud/infra-api/api/v1"
	apicommon "github.com/tidbcloud/infra-provider/api/tidbcloud/common"
)

func convertComponentTypeToApiCommon(componentType v1.ComponentType) apicommon.ComponentType {
	switch componentType {
	case v1.ComponentTiFlash:
		return apicommon.ComponentType_TIFLASH
	// ......
	default:
		return apicommon.ComponentType_COMPONENT_TYPE_UNSPECIFIED
	}
}

// nolint: unused
func buildDataplaneNs(clusterId string) string {
	return fmt.Sprintf("%s%s", "tidb", clusterId)
}

func getCluster(ctx context.Context, cli client.Client, key client.ObjectKey) (*cloudv1.Cluster, error) {
	cluster := &cloudv1.Cluster{}
	err := cli.Get(ctx, key, cluster)
	if err != nil {
		if apierrors.IsNotFound(err) {
			return nil, cloudv1.NewWFNonRetriableError(err)
		}
		return nil, fmt.Errorf("failed to get cluster %s: %s", key, err)
	}

	return cluster, nil
}
