package org.opencb.opencga.app.migrations.v2_4_3.catalog;

import com.mongodb.client.model.Projections;
import com.mongodb.client.model.UpdateOneModel;
import org.bson.Document;
import org.opencb.commons.datastore.mongodb.GenericDocumentComplexConverter;
import org.opencb.opencga.catalog.db.mongodb.MongoDBAdaptorFactory;
import org.opencb.opencga.catalog.migration.MigrationTool;

import java.util.Arrays;
import java.util.List;

import static com.mongodb.client.model.Filters.eq;

public class ShortenIndexedFieldVariables extends MigrationTool {

    @Override
    protected void run() throws Exception {

        migrateCollection(Arrays.asList(
                // Samples
                MongoDBAdaptorFactory.SAMPLE_COLLECTION, MongoDBAdaptorFactory.SAMPLE_ARCHIVE_COLLECTION,
                        MongoDBAdaptorFactory.DELETED_SAMPLE_COLLECTION,
                // Individuals
                MongoDBAdaptorFactory.INDIVIDUAL_COLLECTION, MongoDBAdaptorFactory.INDIVIDUAL_ARCHIVE_COLLECTION,
                MongoDBAdaptorFactory.DELETED_INDIVIDUAL_COLLECTION,
                // Files
                MongoDBAdaptorFactory.FILE_COLLECTION, MongoDBAdaptorFactory.DELETED_FILE_COLLECTION,
                // Families
                MongoDBAdaptorFactory.FAMILY_COLLECTION, MongoDBAdaptorFactory.FAMILY_ARCHIVE_COLLECTION,
                MongoDBAdaptorFactory.DELETED_FAMILY_COLLECTION,
                // Cohorts
                MongoDBAdaptorFactory.COHORT_COLLECTION, MongoDBAdaptorFactory.DELETED_COHORT_COLLECTION),
                new Document("$or", Arrays.asList(
                        "customAnnotationSets", new Document("$exists", true),
                        "customInternalAnnotationSets", new Document("$exists", true)
                )),
                Projections.include("customAnnotationSets", "customInternalAnnotationSets"),
                ((document, bulk) -> {
                    Document replacedDotsDoc = GenericDocumentComplexConverter.replaceDots(document);
                    Document set = new Document()
                            .append("_as", replacedDotsDoc.get("customAnnotationSets"))
                            .append("_ias", replacedDotsDoc.get("customInternalAnnotationSets"));
                    Document unset = new Document()
                            .append("customAnnotationSets", "")
                            .append("customInternalAnnotationSets", "");

                    bulk.add(new UpdateOneModel<>(
                                    eq("_id", document.get("_id")),
                                    new Document()
                                            .append("$set", set)
                                            .append("$unset", unset)
                            )
                    );
                })
        );

        List<String> indexedCollections = Arrays.asList(
                // Samples
                MongoDBAdaptorFactory.SAMPLE_COLLECTION, MongoDBAdaptorFactory.SAMPLE_ARCHIVE_COLLECTION,
                // Individuals
                MongoDBAdaptorFactory.INDIVIDUAL_COLLECTION, MongoDBAdaptorFactory.INDIVIDUAL_ARCHIVE_COLLECTION,
                // Files
                MongoDBAdaptorFactory.FILE_COLLECTION,
                // Families
                MongoDBAdaptorFactory.FAMILY_COLLECTION, MongoDBAdaptorFactory.FAMILY_ARCHIVE_COLLECTION,
                // Cohorts
                MongoDBAdaptorFactory.COHORT_COLLECTION
        );

        // Drop old indexes
        dropIndex(indexedCollections, new Document("customAnnotationSets.as", 1).append("studyUid", 1));
        dropIndex(indexedCollections, new Document("customAnnotationSets.vs", 1).append("studyUid", 1));
        dropIndex(indexedCollections, new Document("customAnnotationSets.id", 1).append("customAnnotationSets.value", 1).append("studyUid", 1));

        dropIndex(indexedCollections, new Document("customInternalAnnotationSets.as", 1).append("studyUid", 1));
        dropIndex(indexedCollections, new Document("customInternalAnnotationSets.vs", 1).append("studyUid", 1));
        dropIndex(indexedCollections, new Document("customInternalAnnotationSets.id", 1).append("customInternalAnnotationSets.value", 1).append("studyUid", 1));

        // Build new indexes
        catalogManager.installIndexes(token);
    }
}

@@ -82,176 +82,49 @@
 ent.
-model.Projections;%0Aimport com.mongodb.client.model.UpdateOneModel;%0Aimport org.bson.Document;%0Aimport org.opencb.commons.datastore.mongodb.GenericDocumentComplexConverter
+MongoCollection;%0Aimport org.bson.Document
 ;%0Aim
@@ -302,60 +302,8 @@
 t;%0A%0A
-import static com.mongodb.client.model.Filters.eq;%0A%0A
 publ
@@ -423,25 +423,24 @@
 ption %7B%0A
-%0A
         
 migrateC
@@ -435,16 +435,22 @@
     
-migrateC
+List%3CString%3E c
 olle
@@ -454,17 +454,20 @@
 llection
-(
+s = 
 Arrays.a
@@ -618,32 +618,24 @@
             
-        
 MongoDBAdapt
@@ -1343,17 +1343,17 @@
 LECTION)
-,
+;
 %0A       
@@ -1357,742 +1357,214 @@
     
-        new Document(%22$or%22, Arrays.asList(%0A                        %22customAnnotationSets%22, new Document(%22$exists%22, true),%0A                        %22customInternalAnnotationSets%22, new Document(%22$exists%22, true)%0A                )),%0A                Projections.include(%22customAnnotationSets%22, %22customInternalAnnotationSets%22),%0A                ((document, bulk) -%3E %7B%0A                    Document replacedDotsDoc = GenericDocumentComplexConverter.replaceDots(document);%0A                    Document set = new Document()%0A                            .append(%22_as%22, replacedDotsDoc.get(%22customAnnotationSets%22))%0A                            .append(%22_ias%22, replacedDotsDoc.get(%22customInternalAnnotationSets%22));%0A                    Document unset =
+for (String collection : collections) %7B%0A            MongoCollection%3CDocument%3E mongoCollection = getMongoCollection(collection);%0A            mongoCollection.updateMany(new Document(), new Document(%22$rename%22,
  new
@@ -1571,32 +1571,24 @@
  Document()%0A
-        
             
@@ -1632,17 +1632,14 @@
 %22, %22
+_as
 %22)%0A
-      
     
@@ -1646,34 +1646,32 @@
                 
-  
 .append(%22customI
@@ -1699,399 +1699,40 @@
 %22, %22
-%22);%0A%0A                    bulk.add(new UpdateOneModel%3C%3E(%0A                                    eq(%22_id%22, document.get(%22_id%22)),%0A                                    new Document()%0A                                            .append(%22$set%22, set)%0A                                            .append(%22$unset%22, unset)%0A                            )%0A                    );%0A                %7D)%0A        );
+_ias%22)%0A            ));%0A        %7D
 %0A%0A  
@@ -2372,17 +2372,16 @@
      );%0A
-%0A
         

use $rename operator, #TASK-1386