package io.github.binout.soccer.infrastructure.persistence.mongo;

import com.github.fakemongo.Fongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import io.github.binout.soccer.infrastructure.persistence.TransactedScope;
import org.mongolink.MongoSession;
import org.mongolink.MongoSessionManager;
import org.mongolink.Settings;
import org.mongolink.domain.mapper.ContextBuilder;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

public class MongoConfiguration {

    @Produces
    @ApplicationScoped
    public MongoDatabase database() {
        String uri = System.getenv("MONGODB_URI");
        if (uri == null) {
            Fongo fongo = new Fongo("Dev Server");
            return fongo.getDatabase("dev");
        } else {
            MongoClient client = new MongoClient(new MongoClientURI(uri));
            return client.getDatabase("prod");
        }
    }

    @Produces
    @ApplicationScoped
    public MongoSessionManager sessionManager(MongoDatabase database) {
        ContextBuilder contextBuilder = new ContextBuilder(getClass().getPackage().getName());
        return MongoSessionManager.create(contextBuilder, Settings.defaultInstance().withDatabase(database));
    }

    @Produces
    @TransactedScope
    public MongoSession session(MongoSessionManager sessionManager) {
        return sessionManager.createSession();
    }
}

@@ -855,23 +855,34 @@
 goClient
- c
+URI mongoC
 lient
+URI
  = new M
@@ -895,32 +895,81 @@
 ient
-(new MongoClientURI(uri)
+URI(uri);%0A            MongoClient client = new MongoClient(mongoClientURI
 );%0A 
@@ -1009,14 +1009,36 @@
 ase(
-%22prod%22
+mongoClientURI.getDatabase()
 );%0A 

fix mongo database name