package database

import (
	"fmt"
	"kurumibot/laniakea"
	"log"
	"os"

	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

var MongoClient *mongo.Client

func ConnectMongo() {
	var err error
	opts := options.Client()
	host, exists := os.LookupEnv("MONGO_HOST")
	if !exists {
		host = "localhost"
	}
	opts = opts.ApplyURI(fmt.Sprintf("mongodb://%s:%s@%s:27017", os.Getenv("MONGO_USER"), os.Getenv("MONGO_PASS"), host))
	opts = opts.SetCompressors([]string{"snappy", "zlib", "zstd"})
	MongoClient, err = mongo.Connect(opts)
	if err != nil {
		log.Fatalln(err)
	}
}

func GetMongoCollection(db *laniakea.DatabaseContext, name string) *mongo.Collection {
	return db.MongoDB.Database(os.Getenv("MONGO_NAME")).Collection(name)
}
