package dbbuild

import (
	"context"

	"git.imall.cloud/openim/open-im-server-deploy/pkg/common/config"
	"git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/cache/cachekey"
	"github.com/openimsdk/tools/db/mongoutil"
	"github.com/redis/go-redis/v9"
)

type Builder interface {
	Mongo(ctx context.Context) (*mongoutil.Client, error)
	Redis(ctx context.Context) (redis.UniversalClient, error)
}

func NewBuilder(mongoConf *config.Mongo, redisConf *config.Redis) Builder {
	if redisConf != nil {
		cachekey.SetOnlinePrefix(redisConf.OnlineKeyPrefix, redisConf.OnlineKeyPrefixHashTag, redisConf.RedisMode)
	}
	if config.Standalone() {
		globalStandalone.setConfig(mongoConf, redisConf)
		return globalStandalone
	}
	return &microservices{
		mongo: mongoConf,
		redis: redisConf,
	}
}
