/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>
*/

package engine

import (
	"errors"
	"reflect"

	"github.com/cgrates/birpc/context"
	"github.com/cgrates/cgrates/utils"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/bsoncodec"
	"go.mongodb.org/mongo-driver/bson/bsonrw"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

const (
	ColCfg = "config"
)

func (ms *MongoStorage) GetSection(ctx *context.Context, section string, val any) error {
	return ms.query(context.TODO(), func(sctx mongo.SessionContext) error {
		sr := ms.getCol(ColCfg).FindOne(sctx, bson.M{"section": section},
			options.FindOne().SetProjection(bson.M{"cfg": 1, "_id": 0 /*"section": 0, */}))
		tmp := map[string]bson.Raw{}
		decodeErr := sr.Decode(&tmp)
		if decodeErr != nil {
			if errors.Is(decodeErr, mongo.ErrNoDocuments) {
				return nil
			}
			return decodeErr
		}
		reg := bson.NewRegistry()
		decimalType := reflect.TypeOf(utils.Decimal{})
		reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(decimalEncoder))
		reg.RegisterTypeDecoder(decimalType, bsoncodec.ValueDecoderFunc(decimalDecoder))

		dec, err := bson.NewDecoder(bsonrw.NewBSONDocumentReader(tmp["cfg"]))
		if err != nil {
			return err
		}
		if err = dec.SetRegistry(reg); err != nil {
			return err
		}
		return dec.Decode(val)
	})
}

func (ms *MongoStorage) SetSection(ctx *context.Context, section string, jsn any) (err error) {
	return ms.query(ctx, func(sctx mongo.SessionContext) (err error) {
		_, err = ms.getCol(ColCfg).UpdateOne(sctx, bson.M{"section": section},
			bson.M{"$set": bson.M{
				"section": section,
				"cfg":     jsn}},
			options.Update().SetUpsert(true),
		)
		return err
	})
}

// Only intended for InternalDB
func (ms *MongoStorage) DumpConfigDB() (err error) {
	return utils.ErrNotImplemented
}

// Only intended for InternalDB
func (ms *MongoStorage) RewriteConfigDB() (err error) {
	return utils.ErrNotImplemented
}

// Only intended for InternalDB
func (ms *MongoStorage) BackupConfigDB(backupFolderPath string, zip bool) (err error) {
	return utils.ErrNotImplemented
}
