package datacenter

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

type datacenterMongoAccessor struct {
	utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
}

// New creates a new instance of the datacenterMongoAccessor
func New() *datacenterMongoAccessor {
	return &datacenterMongoAccessor{}
}

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

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

func (dca *datacenterMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
	set.(*DatacenterResource).ResourceModel = nil
	return dca.GenericUpdateOne(set, id, dca, &DatacenterResource{})
}

func (dca *datacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
	data.(*DatacenterResource).ResourceModel = nil
	return dca.GenericStoreOne(data, dca)
}

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

func (dca *datacenterMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
	var datacenter DatacenterResource

	res_mongo, code, err := mongo.MONGOService.LoadOne(id, dca.GetType())
	if err != nil {
		dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
		return nil, code, err
	}

	res_mongo.Decode(&datacenter)
	accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
	resources, _, err := accessor.Search(nil, dca.GetType())
	if err == nil && len(resources) > 0 {
		datacenter.ResourceModel = resources[0].(*resource_model.ResourceModel)
	}
	return &datacenter, 200, nil
}

func (wfa datacenterMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
	objs := []utils.ShallowDBObject{}
	res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
	if err != nil {
		wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
		return nil, code, err
	}
	var results []DatacenterResource
	if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
		return nil, 404, err
	}
	accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
	resources, _, err := accessor.Search(nil, wfa.GetType())
	for _, r := range results {
		if err == nil && len(resources) > 0 {
			r.ResourceModel = resources[0].(*resource_model.ResourceModel)
		}
		objs = append(objs, &r.AbstractResource) // only get the abstract resource !
	}
	return objs, 200, nil
}

func (wfa *datacenterMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
	objs := []utils.ShallowDBObject{}
	if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
		filters = &dbs.Filters{
			Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
				"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
				"abstractresource.short_description":   {{Operator: dbs.LIKE.String(), Value: search}},
				"abstractresource.description":         {{Operator: dbs.LIKE.String(), Value: search}},
				"abstractresource.owner":               {{Operator: dbs.LIKE.String(), Value: search}},
				"abstractresource.source_url":          {{Operator: dbs.LIKE.String(), Value: search}},
			},
		}
	}
	res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
	if err != nil {
		wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
		return nil, code, err
	}
	var results []DatacenterResource
	if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
		return nil, 404, err
	}
	accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
	resources, _, err := accessor.Search(nil, wfa.GetType())
	for _, r := range results {
		if err == nil && len(resources) > 0 {
			r.ResourceModel = resources[0].(*resource_model.ResourceModel)
		}
		objs = append(objs, &r.AbstractResource) // only get the abstract resource !
	}
	return objs, 200, nil
}
