package oclib

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

func New() *workflowResourceMongoAccessor {
	return &workflowResourceMongoAccessor{}
}

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

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

func (wfa *workflowResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
	data.(*WorkflowResource).ResourceModel = nil
	return wfa.GenericStoreOne(data, wfa)
}

func (wfa *workflowResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
	res, _, _ := wfa.LoadOne(data.GetID())
	data.(*WorkflowResource).WorkflowID = data.GetID()
	if res == nil {
		return wfa.GenericStoreOne(data, wfa)
	} else {
		data.(*WorkflowResource).UUID = res.GetID()
		return wfa.GenericUpdateOne(data, res.GetID(), wfa, &WorkflowResource{})
	}
}

func (wfa *workflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
	var workflow WorkflowResource
	res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
	if err != nil {
		wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
		return nil, code, err
	}
	res_mongo.Decode(&workflow)
	accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
	resources, _, err := accessor.Search(nil, wfa.GetType())
	if err == nil && len(resources) > 0 {
		workflow.ResourceModel = resources[0].(*resource_model.ResourceModel)
	}
	return &workflow, 200, nil
}

func (wfa workflowResourceMongoAccessor) 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 []WorkflowResource
	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)
	}
	return objs, 200, nil
}

// Search searches for workflow resources in the database, given some filters OR a search string
func (wfa *workflowResourceMongoAccessor) 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 []WorkflowResource
	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)
	}
	return objs, 200, nil
}
