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

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.IdRemovalEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.ManualRedactionEntryEntity;
import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.service.manualredactions.ManualRedactionProviderService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.AddRedactionPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.RemoveRedactionPersistenceService;

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

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

    private static final String NAME = "Delete removed ManualAddRedactions";
    private static final long VERSION = 7;

    @Autowired
    private AddRedactionPersistenceService addRedactionPersistenceService;

    @Autowired
    private RemoveRedactionPersistenceService removeRedactionPersistenceService;

    @Autowired
    private ManualRedactionProviderService manualRedactionProviderService;


    public DeleteRemovedManualAddRedactions7() {

        super(NAME, VERSION);
    }


    @Override
    protected void migrate() {

        List<ManualRedactionEntryEntity> addRedactions = addRedactionPersistenceService.findAllAddRedactions();

        for (IdRemovalEntity idRemovalEntity : removeRedactionPersistenceService.findAllRemoveRedactions()) {
            if (manualAddRedactionsContains(addRedactions, idRemovalEntity.getId().getAnnotationId())) {
                log.info("hard delete ManualRedactions for file {} and annotation {}", idRemovalEntity.getId().getFileId(), idRemovalEntity.getId().getAnnotationId());
                manualRedactionProviderService.hardDeleteManualRedactions(idRemovalEntity.getId().getFileId(), idRemovalEntity.getId().getAnnotationId());
            }
        }

    }


    private boolean manualAddRedactionsContains(List<ManualRedactionEntryEntity> addRedactions, String annotationId) {

        for (ManualRedactionEntryEntity manualRedactionEntryEntity : addRedactions) {
            if (StringUtils.equals(annotationId, manualRedactionEntryEntity.getId().getAnnotationId())) {
                return true;
            }
        }
        return false;
    }

}





