package bootstrap

import (
	"context"
	"designs/config"
	"designs/global"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/readpref"
	"go.uber.org/zap"
)
import mongoOption "go.mongodb.org/mongo-driver/v2/mongo/options"

func InitializeMongo() *mongo.Client {

	var url string
	if config.Get("app.local") == "local" {
		url = "mongodb://localhost:27017"
	} else {
		url = "mongodb://admin:admin@localhost:27017"
	}

	//url = "mongodb://admin:admin@124.223.73.12:27017"

	client, _ := mongo.Connect(mongoOption.Client().ApplyURI(url))
	//client, _ := mongo.Connect(mongoOption.Client().ApplyURI("mongodb://localhost:27017"))
	err := client.Ping(context.Background(), readpref.Primary())
	if err != nil {
		global.App.Log.Error("mongoDB connect ping failed, err:", zap.Any("err", err))
		return nil
	}

	return client
}
