package cvt

import (
	"strings"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
	apiv1 "github.com/tidbcloud/infra-provider/api/tidbcloud/cluster-server/v1"
)

func Workflow2PB(wf *cloudv1.Workflow) *apiv1.Operation {
	op := &apiv1.Operation{
		IdempotencyKey: wf.Labels[cloudv1.IdempotencyKey],
		Type:           WFType2OperationType(wf.Spec.Type),
		Id:             wf.Name,
		ClusterId:      wf.Labels[cloudv1.ClusterTagKey],
		Phase:          WFPhase2OperationPhase(wf.Status.Phase),
		Reason:         wf.Status.Reason,
	}
	return op
}

func OperationType2WFType(tp apiv1.Operation_Type) cloudv1.WorkflowType {
	return cloudv1.WorkflowType(strings.ToLower(tp.String()))
}

func WFType2OperationType(tp cloudv1.WorkflowType) apiv1.Operation_Type {
	return apiv1.Operation_Type(apiv1.Operation_Type_value[strings.ToUpper(string(tp))])
}

func WFPhase2OperationPhase(tp cloudv1.WorkflowPhase) apiv1.Operation_Phase {
	return apiv1.Operation_Phase(apiv1.Operation_Phase_value[strings.ToUpper(string(tp))])
}
