package com.iqser.red.service.persistence.management.v1.processor.migration.migrations;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierTemplateEntity;
import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.migration.StorageToMongoCopyService;
import com.iqser.red.service.persistence.management.v1.processor.service.ComponentDefinitionService;
import com.iqser.red.service.persistence.management.v1.processor.service.CurrentApplicationTypeProvider;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierTemplatePersistenceService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.component.ComponentDefinitionAddRequest;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class V21ComponentOverridesMigration extends Migration {

    private static final String NAME = "Migrate component overrides to mongoDB and create component definitions";
    private static final long VERSION = 21;

    private final List<ComponentDefinitionAddRequest> componentDefinitions = List.of(new ComponentDefinitionAddRequest("study_title", "Study Title", "Study Title"),
                                                                                     new ComponentDefinitionAddRequest("performing_laboratory",
                                                                                                                       "Performing Laboratory",
                                                                                                                       "Performing Laboratory"),
                                                                                     new ComponentDefinitionAddRequest("report_number", "Report Number", "Report Number"),
                                                                                     new ComponentDefinitionAddRequest("glp_study", "GLP Study", "GLP Study"),
                                                                                     new ComponentDefinitionAddRequest("test_guidelines_1",
                                                                                                                       "Test Guidelines 1",
                                                                                                                       "Test Guidelines 1"),
                                                                                     new ComponentDefinitionAddRequest("test_guidelines_2",
                                                                                                                       "Test Guidelines 2",
                                                                                                                       "Test Guidelines 2"),
                                                                                     new ComponentDefinitionAddRequest("experimental_starting_date",
                                                                                                                       "Experimental Starting Date",
                                                                                                                       "Experimental Starting Date"),
                                                                                     new ComponentDefinitionAddRequest("experimental_completion_date",
                                                                                                                       "Experimental Completion Date",
                                                                                                                       "Experimental Completion Date"),
                                                                                     new ComponentDefinitionAddRequest("species", "Species", "Species"),
                                                                                     new ComponentDefinitionAddRequest("strain", "Strain", "Strain"),
                                                                                     new ComponentDefinitionAddRequest("was_the_definitive_study_conducted_with_positive_control",
                                                                                                                       "Was the definitive study conducted with positive control",
                                                                                                                       "Was the definitive study conducted with positive control"),
                                                                                     new ComponentDefinitionAddRequest("study_design_main_study",
                                                                                                                       "Study Design Main Study",
                                                                                                                       "Study Design Main Study"),
                                                                                     new ComponentDefinitionAddRequest("results_main_study",
                                                                                                                       "Results Main Study",
                                                                                                                       "Results Main Study"),
                                                                                     new ComponentDefinitionAddRequest("preliminary_test_results",
                                                                                                                       "Preliminary Test Results",
                                                                                                                       "Preliminary Test Results"),
                                                                                     new ComponentDefinitionAddRequest("what_was_the_approach_used",
                                                                                                                       "What was the approach used",
                                                                                                                       "What was the approach used"),
                                                                                     new ComponentDefinitionAddRequest("sex", "Sex", "Sex"),
                                                                                     new ComponentDefinitionAddRequest("number_of_animals",
                                                                                                                       "Number of Animals",
                                                                                                                       "Number of Animals"),
                                                                                     new ComponentDefinitionAddRequest("study_design", "Study Design", "Study Design"),
                                                                                     new ComponentDefinitionAddRequest("test_results", "Test Results", "Test Results"),
                                                                                     new ComponentDefinitionAddRequest("results_and_conclusions",
                                                                                                                       "Results and Conclusions",
                                                                                                                       "Results and Conclusions"),
                                                                                     new ComponentDefinitionAddRequest("conducted_with_4_hours_of_exposure",
                                                                                                                       "Conducted with 4 Hours of Exposure",
                                                                                                                       "Conducted with 4 Hours of Exposure"),
                                                                                     new ComponentDefinitionAddRequest("dosages", "Dosages", "Dosages"),
                                                                                     new ComponentDefinitionAddRequest("doses_mg_per_kg_bw",
                                                                                                                       "Doses mg per kg bw",
                                                                                                                       "Doses mg per kg bw"),
                                                                                     new ComponentDefinitionAddRequest("mortality", "Mortality", "Mortality"),
                                                                                     new ComponentDefinitionAddRequest("dose_mortality", "Dose Mortality", "Dose Mortality"),
                                                                                     new ComponentDefinitionAddRequest("mortality_statement",
                                                                                                                       "Mortality Statement",
                                                                                                                       "Mortality Statement"),
                                                                                     new ComponentDefinitionAddRequest("weight_behavior_changes",
                                                                                                                       "Weight Behavior Changes",
                                                                                                                       "Weight Behavior Changes"),
                                                                                     new ComponentDefinitionAddRequest("clinical_observations",
                                                                                                                       "Clinical Observations",
                                                                                                                       "Clinical Observations"),
                                                                                     new ComponentDefinitionAddRequest("clinical_signs", "Clinical Signs", "Clinical Signs"),
                                                                                     new ComponentDefinitionAddRequest("body_weight_changes",
                                                                                                                       "Body Weight Changes",
                                                                                                                       "Body Weight Changes"),
                                                                                     new ComponentDefinitionAddRequest("necropsy_findings",
                                                                                                                       "Necropsy Findings",
                                                                                                                       "Necropsy Findings"),
                                                                                     new ComponentDefinitionAddRequest("detailing_of_reported_changes",
                                                                                                                       "Detailing of Reported Changes",
                                                                                                                       "Detailing of Reported Changes"),
                                                                                     new ComponentDefinitionAddRequest("deviation_from_the_guideline",
                                                                                                                       "Deviation_from_the_Guideline",
                                                                                                                       "Deviation_from_the_Guideline"),
                                                                                     new ComponentDefinitionAddRequest("conclusion_ld50_greater_than",
                                                                                                                       "Conclusion LD50 Greater than",
                                                                                                                       "Conclusion LD50 Greater than"),
                                                                                     new ComponentDefinitionAddRequest("conclusion_ld50_mg_per_kg",
                                                                                                                       "Conclusion LD50 mg per kg",
                                                                                                                       "Conclusion LD50 mg per kg"),
                                                                                     new ComponentDefinitionAddRequest("conclusion_minimum_confidence",
                                                                                                                       "Conclusion Minimum Confidence",
                                                                                                                       "Conclusion Minimum Confidence"),
                                                                                     new ComponentDefinitionAddRequest("conclusion_maximum_confidence",
                                                                                                                       "Conclusion Maximum Confidence",
                                                                                                                       "Conclusion Maximum Confidence"),
                                                                                     new ComponentDefinitionAddRequest("study_conclusion", "Study Conclusion", "Study Conclusion"),
                                                                                     new ComponentDefinitionAddRequest("certificate_of_analysis_batch_identification",
                                                                                                                       "Certificate of analysis batch identification",
                                                                                                                       "Certificate of analysis batch identification"));

    @Autowired
    ComponentDefinitionService componentDefinitionService;
    @Autowired
    DossierTemplatePersistenceService dossierTemplatePersistenceService;
    @Autowired
    StorageToMongoCopyService storageToMongoCopyService;
    @Autowired
    private CurrentApplicationTypeProvider currentApplicationTypeProvider;


    public V21ComponentOverridesMigration() {

        super(NAME, VERSION);
    }


    @Override
    protected void migrate() {

        if (!currentApplicationTypeProvider.isDocuMine()) {
            log.info("Skipping component migration, due to application type not being equal to DOCUMINE!");
        }

        log.info("Running component log copy migration.");
        storageToMongoCopyService.copyComponentOverrides();

        log.info("Running component definition creation migration.");
        for (DossierTemplateEntity dossierTemplate : dossierTemplatePersistenceService.getAllDossierTemplates()) {
            componentDefinitionService.createComponents(dossierTemplate.getId(), componentDefinitions);
        }
    }

}
