package live

import (
	"cloud.o-forge.io/core/oc-lib/dbs"
	"cloud.o-forge.io/core/oc-lib/logs"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

type liveMongoAccessor[T LiveInterface] struct {
	utils.AbstractAccessor[LiveInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller)
}

// New creates a new instance of the computeUnitsMongoAccessor
func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *liveMongoAccessor[T] {
	return &liveMongoAccessor[T]{
		AbstractAccessor: utils.AbstractAccessor[LiveInterface]{
			Logger:  logs.CreateLogger(t.String()), // Create a logger with the data type
			Request: request,
			Type:    t,
			New: func() LiveInterface {
				switch t {
				case tools.LIVE_DATACENTER:
					return &LiveDatacenter{}
				case tools.LIVE_STORAGE:
					return &LiveStorage{}
				case tools.LIVE_SERVICE:
					return &LiveService{}
				}
				return &LiveDatacenter{}
			},
			NotImplemented: []string{"CopyOne"},
		},
	}
}

func (wfa *liveMongoAccessor[T]) LoadAll(isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericLoadAll[T](wfa.GetExec(isDraft), isDraft, wfa, offset, limit)
}

func (wfa *liveMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
	if filters == nil && search == "*" {
		return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
			return d
		}, isDraft, wfa, offset, limit)
	}
	return utils.GenericSearch[T](filters, search, wfa.New().GetObjectFilters(search),
		func(d utils.DBObject) utils.ShallowDBObject {
			return d
		}, isDraft, wfa, offset, limit)
}

func (a *liveMongoAccessor[T]) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
	return func(d utils.DBObject) utils.ShallowDBObject {
		return d
	}
}
