package utils

import (
	"errors"

	"cloud.o-forge.io/core/oc-lib/dbs"
	"cloud.o-forge.io/core/oc-lib/dbs/mongo"
	mgb "go.mongodb.org/mongo-driver/mongo"
)

type Owner struct {
	Name string `json:"name,omitempty" bson:"name,omitempty"`
	Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
}

func VerifyAccess(a Accessor, id string) error {
	data, _, err := a.LoadOne(id)
	if err != nil {
		return err
	}
	if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
		return errors.New("you are not allowed to access :" + a.GetType().String())
	}
	return nil
}

// GenericLoadOne loads one object from the database (generic)
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
	data.GenerateID()
	data.StoreDraftDefault()
	data.UpToDate(a.GetUser(), a.GetPeerID(), true)
	f := dbs.Filters{
		Or: map[string][]dbs.Filter{
			"abstractresource.abstractobject.name": {{
				Operator: dbs.LIKE.String(),
				Value:    data.GetName(),
			}},
			"abstractobject.name": {{
				Operator: dbs.LIKE.String(),
				Value:    data.GetName(),
			}},
		},
	}
	if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
		return nil, 403, errors.New("you are not allowed to access : " + a.GetType().String())
	}
	if cursor, _, _ := a.Search(&f, "", data.IsDrafted()); len(cursor) > 0 {
		return nil, 409, errors.New(a.GetType().String() + " with name " + data.GetName() + " already exists")
	}
	err := validate.Struct(data)
	if err != nil {
		return nil, 422, err
	}
	id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String())
	if err != nil {
		a.GetLogger().Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
		return nil, code, err
	}
	return a.LoadOne(id)
}

// GenericLoadOne loads one object from the database (generic)
func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
	res, code, err := a.LoadOne(id)
	if !res.CanDelete() {
		return nil, 403, errors.New("you are not allowed to delete :" + a.GetType().String())
	}
	if err != nil {
		a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
		return nil, code, err
	}
	if a.ShouldVerifyAuth() && !res.VerifyAuth(a.GetRequest()) {
		return nil, 403, errors.New("you are not allowed to access " + a.GetType().String())
	}
	_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
	if err != nil {
		a.GetLogger().Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
		return nil, code, err
	}
	return res, 200, nil
}

// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObject, int, error) {
	r, c, err := a.LoadOne(id)
	if err != nil {
		return nil, c, err
	}
	ok, newSet := r.CanUpdate(set)
	if !ok {
		return nil, 403, errors.New("you are not allowed to delete :" + a.GetType().String())
	}
	set = newSet
	r.UpToDate(a.GetUser(), a.GetPeerID(), false)
	if a.ShouldVerifyAuth() && !r.VerifyAuth(a.GetRequest()) {
		return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
	}
	change := set.Serialize(set) // get the changes
	loaded := r.Serialize(r)     // get the loaded object

	for k, v := range change { // apply the changes, with a flatten method
		loaded[k] = v
	}
	id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, a.GetType().String())
	if err != nil {
		a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
		return nil, code, err
	}
	return a.LoadOne(id)
}

func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), a Accessor) (DBObject, int, error) {
	var data T
	res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
	if err != nil {
		a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
		return nil, code, err
	}
	res_mongo.Decode(&data)
	if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
		return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
	}
	return f(data)
}

func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, onlyDraft bool, f func(DBObject) ShallowDBObject, a Accessor) ([]ShallowDBObject, int, error) {
	objs := []ShallowDBObject{}
	var results []T
	if err != nil {
		a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
		return nil, code, err
	}
	if err = res.All(mongo.MngoCtx, &results); err != nil {
		return nil, 404, err
	}
	for _, r := range results {
		if (a.ShouldVerifyAuth() && !r.VerifyAuth(a.GetRequest())) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
			continue
		}
		objs = append(objs, f(r))
	}
	return objs, 200, nil
}

func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
	res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
	return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
}

func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
	f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
	if filters == nil && search != "" {
		filters = defaultFilters
	}
	res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
	return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
}

// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, error) {
	id, code, err := mongo.MONGOService.UpdateOne(set, id, a.GetType().String())
	if err != nil {
		a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
		return nil, code, err
	}
	return a.LoadOne(id)
}
