package resource_model

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 ResourceModelMongoAccessor struct {
	utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}

/*
* Nothing special here, just the basic CRUD operations
 */

func New() *ResourceModelMongoAccessor {
	return &ResourceModelMongoAccessor{
		utils.AbstractAccessor{
			Type:   tools.RESOURCE_MODEL,
			Logger: logs.CreateLogger(tools.RESOURCE_MODEL.String()),
		},
	}
}

func (wfa *ResourceModelMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
	return utils.GenericDeleteOne(id, wfa)
}

func (wfa *ResourceModelMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
	return utils.GenericUpdateOne(set, id, wfa, &ResourceModel{})
}

func (wfa *ResourceModelMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
	return utils.GenericStoreOne(data, wfa)
}

func (wfa *ResourceModelMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
	return utils.GenericStoreOne(data, wfa)
}

func (a *ResourceModelMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
	return utils.GenericLoadOne[*ResourceModel](id, func(d utils.DBObject) (utils.DBObject, int, error) {
		return d, 200, nil
	}, a)
}

func (a *ResourceModelMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
	return utils.GenericLoadAll[*ResourceModel](func(d utils.DBObject) utils.ShallowDBObject {
		return d
	}, a)
}

func (a *ResourceModelMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericSearch[*ResourceModel](filters, search,
		&dbs.Filters{
			Or: map[string][]dbs.Filter{
				"resource_type": {{Operator: dbs.LIKE.String(), Value: search}},
			},
		}, func(d utils.DBObject) utils.ShallowDBObject { return d }, a)
}
