package cluster

import (
	"fmt"
	"net/url"
	"strconv"
	"strings"
)

const (
	TiDBNameFormat = "%s-tidb"

	namespacePrefix = "tidb"
)

func ServiceURL(scheme, namespace, service, port string) *url.URL {
	// The schema is valid so url.Parse never returns an error.
	result, _ := url.Parse(fmt.Sprintf("%s://%s.%s.svc:%s", scheme, service, namespace, port))
	return result
}

func NamespaceById(clusterId int64) string {
	return fmt.Sprintf("%s%d", namespacePrefix, clusterId)
}

func GetIdByFirstPath(path string) (int64, error) {
	paths := strings.Split(path, "/")
	if len(paths) == 0 {
		return 0, fmt.Errorf("empty paths")
	}
	clusterId, err := strconv.ParseInt(paths[0], 10, 64)
	if err != nil {
		return clusterId, fmt.Errorf("error parsing cluster ID %s: %v", paths[0], err)
	}
	return clusterId, nil
}
