package org.rabix.engine.store.event.sourcing;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import org.apache.commons.configuration.Configuration;
import org.rabix.engine.store.memory.InMemoryRepositoryModule;
import org.rabix.engine.store.postgres.jdbi.JDBIRepositoryModule;
import org.rabix.engine.store.postgres.jdbi.JDBIRepositoryRegistry;
import org.rabix.engine.store.repository.*;
import org.skife.jdbi.v2.DBI;

public class EventSourcingModule extends AbstractModule {

    private final InMemoryRepositoryModule inMemoryRepositoryModule;
    private final JDBIRepositoryModule jdbiRepositoryModule;

    public EventSourcingModule (InMemoryRepositoryModule inMemoryRepositoryModule,
                                JDBIRepositoryModule jdbiRepositoryModule) {

        this.inMemoryRepositoryModule = inMemoryRepositoryModule;
        this.jdbiRepositoryModule = jdbiRepositoryModule;
    }

    @Singleton
    @Provides
    public JDBIRepositoryRegistry provideJDBIRepositoryRegistry(Configuration configuration) {
        DBI dbi = jdbiRepositoryModule.provideDBI(configuration);
        return dbi.onDemand(JDBIRepositoryRegistry.class);
    }

    @Provides
    @Singleton
    public EventRepository provideEventRepository(JDBIRepositoryRegistry jdbiRepositoryRegistry) {
        return jdbiRepositoryRegistry.eventRepository();
    }

    @Provides
    @Singleton
    public DAGRepository provideDAGRepository(JDBIRepositoryRegistry jdbiRepositoryRegistry) {
        return jdbiRepositoryRegistry.dagRepository();
    }

    @Provides
    @Singleton
    public AppRepository provideAppRepository(JDBIRepositoryRegistry jdbiRepositoryRegistry) {
        return jdbiRepositoryRegistry.applicationRepository();
    }

    @Provides
    @Singleton
    public ContextRecordRepository provideContextRecordRepository(JDBIRepositoryRegistry jdbiRepositoryRegistry) {
        return jdbiRepositoryRegistry.contextRecordRepository();
    }

    @Provides
    @Singleton
    public BackendRepository provideBackendRepository() {
        return inMemoryRepositoryModule.provideBackendRepository();
    }

    @Provides
    @Singleton
    public JobRepository provideJobRepository() {
        return inMemoryRepositoryModule.provideJobRepository();
    }

    @Provides
    @Singleton
    public JobRecordRepository provideJobRecordRepository() {
        return inMemoryRepositoryModule.provideJobRecordRepository();
    }

    @Provides
    @Singleton
    public LinkRecordRepository provideLinkRecordRepository() {
        return inMemoryRepositoryModule.provideLinkRecordRepository();
    }

    @Provides
    @Singleton
    public VariableRecordRepository provideVariableRecordRepository() {
        return inMemoryRepositoryModule.provideVariableRecordRepository();
    }

    @Provides
    @Singleton
    public JobStatsRecordRepository provideJobStatsRecordRepository() {
        return inMemoryRepositoryModule.provideJobStatsRecordRepository();
    }

    @Provides
    @Singleton
    public IntermediaryFilesRepository provideIntermediaryFilesRepository() {
        return inMemoryRepositoryModule.provideIntermediaryFilesRepository();
    }

    @Override
    protected void configure() {}
}

@@ -1210,32 +1210,54 @@
 .class);%0A    %7D%0A%0A
+    /* JDBI REPOS */%0A%0A
     @Provides%0A  
@@ -2103,32 +2103,77 @@
 ckendRepository(
+JDBIRepositoryRegistry jdbiRepositoryRegistry
 ) %7B%0A        retu
@@ -2171,32 +2171,28 @@
      return 
-inMemory
+jdbi
 RepositoryMo
@@ -2193,23 +2193,18 @@
 tory
-Module.provideB
+Registry.b
 acke
@@ -2218,32 +2218,59 @@
 itory();%0A    %7D%0A%0A
+    /* IN MEMORY REPOS */%0A%0A
     @Provides%0A  

use jdbi backend repository for event sourcing