// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
	"fmt"

	tcapi "github.com/pingcap/private-tidb-operator/pkg/tidbcluster/api"
	tsapi "github.com/pingcap/private-tidb-operator/pkg/tidbset/api"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

var (
	schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)

	// AddToScheme applies all the stored functions to the scheme
	AddToScheme = schemeBuilder.AddToScheme

	// GroupName is the group name of CRD
	GroupName = "pingcap.com"

	// SchemeGroupVersion is the group version used to register objects.
	SchemeGroupVersion = schema.GroupVersion{
		Group:   GroupName,
		Version: "v1",
	}

	// TidbClusterCRDName is the name of TidbCluster CRD's name
	TidbClusterCRDName = fmt.Sprintf("%s.%s", tcapi.TidbClusterResourcePlural, GroupName)

	// TidbSetCRDName is the name of TidbSet CRD's name
	TidbSetCRDName = fmt.Sprintf("%s.%s", tsapi.TidbSetResourcePlural, GroupName)
)

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(s *runtime.Scheme) error {
	s.AddKnownTypes(
		SchemeGroupVersion,
		&tcapi.TidbCluster{},
		&tcapi.TidbClusterList{},
		&tsapi.TidbSet{},
		&tsapi.TidbSetList{},
	)

	metav1.AddToGroupVersion(s, SchemeGroupVersion)

	return nil
}
