package types

import (
	"regexp"

	"github.com/juju/errors"
	"github.com/pingcap/schrodinger/util"
)

// DefaultNodeLabel defins the default label for node
const DefaultNodeLabel = "general"

// GenCatProcess genernates cat process
func GenCatProcess(name string, namespace string, catConfig *Cat) *CatProcess {
	catConfig.SetDefaultConfig()
	cat := &CatProcess{
		Name:      name,
		Namespace: namespace,
		Creator:   catConfig.Creator,
		Meta:      &Meta{},
		Config: TiDBClusterConfig{
			PDConfig:   catConfig.PDConfig,
			TiDBConfig: catConfig.TiDBConfig,
			TiKVConfig: catConfig.TiKVConfig,
		},
		Stage:        CatStageNew,
		State:        CatStateNormal,
		Destroyed:    false,
		UseBoxLabel:  false,
		IgnoreLabels: make(map[string]string),
	}

	cat.Labels = util.GenLabels(catConfig.LabelStr)
	if node, ok := cat.Labels["node"]; !ok || util.IsEmpty(node) {
		cat.Labels["node"] = DefaultNodeLabel
	}
	return cat
}

func verifyK8sName(name string) error {
	pattern := "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
	re := regexp.MustCompile(pattern)
	if !re.MatchString(name) {
		return errors.NotValidf("name %s not match %s pattern", name, pattern)
	}

	return nil
}

// MatchInTagKinds returns true if the given kind value is in the supported kinds.
func MatchInTagKinds(kind TagKind) bool {
	for _, v := range TagKinds {
		if v == kind {
			return true
		}
	}
	return false
}
