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

import java.util.ArrayList;

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.service.FileManagementStorageService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.FileStatusPersistenceService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.RedactionLogEntry;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
@Service
public class DictionaryToEntityMigration2 extends Migration {

    private static final String NAME = "Migrate RedactionLogs remove false_positive and recommendation_ prefix";
    private static final long VERSION = 2;

    @Autowired
    private DossierPersistenceService dossierPersistenceService;

    @Autowired
    private FileStatusPersistenceService fileStatusPersistenceService;

    @Autowired
    private FileManagementStorageService fileManagementStorageService;

    @Autowired
    private ObjectMapper objectMapper;


    public DictionaryToEntityMigration2() {

        super(NAME, VERSION);
    }


    @Override
    protected void migrate() {

        var dossiers = dossierPersistenceService.findAllDossiers();
        dossiers.forEach(dossier -> {
            var files = fileStatusPersistenceService.getStatusesForDossier(dossier.getId());
            log.info("Start migration of dossier {}", dossier.getId());
            files.forEach(file -> {
                log.info("Start migration of file {}", file.getId());
                if (file.getHardDeletedTime() == null) {

                    try {
                        var newRedactionLogEntries = new ArrayList<RedactionLogEntry>();
                        var redactionLog = fileManagementStorageService.getRedactionLog(dossier.getId(), file.getId());
                        redactionLog.getRedactionLogEntry().forEach(entry -> {

                            if (entry.getType().equals("false_positive")) {
                                log.info("skipping false_positive for dossier {} and file {}", dossier.getId(), file.getId());
                                return;
                            }

                            if (entry.getType().startsWith("recommendation_")) {
                                entry.setType(entry.getType().substring(15));
                                entry.setRecommendation(true);
                                log.info("removed _recommendation for file {} and annotation {}", file.getId(), entry.getId());
                            }

                            newRedactionLogEntries.add(entry);
                        });

                        if (newRedactionLogEntries.size() != redactionLog.getRedactionLogEntry().size()) {
                            redactionLog.setRedactionLogEntry(newRedactionLogEntries);
                            fileManagementStorageService.storeJSONObject(dossier.getId(), file.getId(), FileType.REDACTION_LOG, redactionLog);
                            log.info("Stored redactionLog for dossierId: {} and fileId: {}", dossier.getId(), file.getId());
                        }
                    } catch (Exception e) {
                        log.info("redactionLog {} does not exsist", file.getId());
                    }
                }
            });
        });
    }

}
