package order

import (
	"errors"

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

// New creates a new instance of the orderMongoAccessor
func NewAccessor(request *tools.APIRequest) *orderMongoAccessor {
	return &orderMongoAccessor{
		AbstractAccessor: utils.AbstractAccessor{
			Logger:  logs.CreateLogger(tools.ORDER.String()), // Create a logger with the data type
			Request: request,
			Type:    tools.ORDER,
		},
	}
}

/*
* Nothing special here, just the basic CRUD operations
 */
func (a *orderMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
	return utils.GenericDeleteOne(id, a)
}

func (a *orderMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
	return utils.GenericUpdateOne(set, id, a, &Order{})
}

func (a *orderMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
	return nil, 404, errors.New("Not implemented")
}

func (a *orderMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
	return nil, 404, errors.New("Not implemented")
}

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

func (a *orderMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericLoadAll[*Order](a.getExec(), isDraft, a)
}

func (a *orderMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericSearch[*Order](filters, search, (&Order{}).GetObjectFilters(search), a.getExec(), isDraft, a)
}

func (a *orderMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
	return func(d utils.DBObject) utils.ShallowDBObject {
		return d
	}
}
