package machinetemplate

import (
	"context"
	"fmt"

	"github.com/pingcap/log"
	"github.com/pingcap/tep/pkg/apis/apiserver"
	store "github.com/pingcap/tep/pkg/registry/apiserver"
	"go.uber.org/zap"
	"k8s.io/apimachinery/pkg/api/errors"
	metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
	"k8s.io/apimachinery/pkg/runtime"
)

type Storage struct {
	store *store.Store
}

func NewStorage(store *store.Store) *Storage {
	return &Storage{
		store: store,
	}
}

func (s *Storage) Kind() string {
	return "MachineTemplate"
}

func (s *Storage) New() runtime.Object {
	return &apiserver.MachineTemplate{}
}

func (s *Storage) NewList() runtime.Object {
	return &apiserver.MachineTemplate{}
}

func (s *Storage) NamespaceScoped() bool {
	return false
}

func (s *Storage) NewExtraListOptions() runtime.Object {
	return &apiserver.ListOptions{}
}

func (s *Storage) List(ctx context.Context, options *metainternalversion.ListOptions, extraOptions runtime.Object) (runtime.Object, error) {
	stream, err := New(1024)
	if err != nil {
		msg := fmt.Errorf("failed to get machine_template. err: %v", err)
		log.Error("", zap.Error(msg))
		return nil, errors.NewInternalError(msg)
	}
	return stream, nil
}
