package systemlog

import (
	"context"
	"github.com/pingcap/tep/pkg/apis/apiserver"
	store "github.com/pingcap/tep/pkg/registry/apiserver"
	"github.com/pingcap/tep/pkg/utils"
	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 "SystemLog"
}

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

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

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

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

func (s *Storage) List(ctx context.Context, options *metainternalversion.ListOptions, extraOptions runtime.Object) (runtime.Object, error) {
	listOptions := extraOptions.(*apiserver.SystemLogListOptions)
	return s.SystemLogs(Context{
		From:           utils.GetStartIndex(listOptions.Page, listOptions.ItemsPerPage),
		Size:           utils.GetSize(listOptions.ItemsPerPage),
		StartTimeInSec: listOptions.StartTimeInSec,
		EndTimeInSec:   listOptions.EndTimeInSec,
		Host:           listOptions.Host,
	})
}
