package initialize

import (
	"context"
	"go.mongodb.org/mongo-driver/mongo"
	mdb "gitea.pena/PenaSide/common/mongo"
	"time"
)

func MongoDB(ctx context.Context, cfg Config) (*mongo.Database, error) {
	dbConfig := &mdb.Configuration{
    URL: cfg.MongoURL,
    DatabaseName: cfg.DatabaseName,
	}

	newCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
	defer cancel()

	mongoDeps := &mdb.ConnectDeps{
		Configuration: dbConfig,
		Timeout:       10 * time.Second,
	}

	db, err := mdb.Connect(newCtx, mongoDeps)
	if err != nil {
		return nil, err
	}
	err = db.Client().Ping(newCtx, nil)
	if err != nil {
		return nil, err
	}

	return db, nil
}
