package utils

import (
	"context"
	"fmt"
	"time"

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

var MongoDB *mongo.Client
var PreUserData *mongo.Collection
var User *mongo.Collection

func InitMongoDriver(mongoUri string) *mongo.Client {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoUri))

	if err != nil {
		panic(err)
	}

	err = client.Ping(ctx, readpref.Primary())

	if err != nil {
		panic(err)
	} else {
		fmt.Println("MongoDB is ready")
	}

	MongoDB = client
	PreUserData = MongoDB.Database("Totoday-shop").Collection("preusers")
	User = MongoDB.Database("Totoday-shop").Collection("users")

	return client
}
