package com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.AnnotationEntityId;
import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.IdRemovalEntity;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.RemoveRedactionRepository;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.AnnotationStatus;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.RemoveRedactionRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.IdRemoval;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class RemoveRedactionPersistenceService {

    private final RemoveRedactionRepository removeRedactionRepository;


    @Transactional
    public IdRemovalEntity insert(String fileId, RemoveRedactionRequest removeRedactionRequest) {

        IdRemovalEntity idRemoval = new IdRemovalEntity();
        idRemoval.setId(new AnnotationEntityId(removeRedactionRequest.getAnnotationId(), fileId));
        BeanUtils.copyProperties(removeRedactionRequest, idRemoval);
        idRemoval.setRequestDate(OffsetDateTime.now());

        return removeRedactionRepository.saveAndFlush(idRemoval);
    }


    public IdRemovalEntity findRemoveRedaction(String fileId, String annotationId) {

        return removeRedactionRepository.findByIdAndNotSoftDeleted(new AnnotationEntityId(annotationId, fileId))
                .orElseThrow(() -> new NotFoundException("Unknown file/annotation combination: " + fileId + "/" + annotationId));
    }


    public Set<IdRemovalEntity> findRemoveRedactions(String fileId, boolean includeDeletions) {

        return removeRedactionRepository.findByFileIdIncludeDeletions(fileId, includeDeletions).stream().collect(Collectors.toSet());
    }


    public List<IdRemovalEntity> findAllRemoveRedactions() {

        return removeRedactionRepository.findAll();
    }


    @Transactional
    public void hardDelete(String fileId, String annotationId) {

        removeRedactionRepository.deleteByIdIs(new AnnotationEntityId(annotationId, fileId));
    }


    @Transactional
    public void softDelete(String fileId, String annotationId, OffsetDateTime softDeleteTime) {

        removeRedactionRepository.updateSoftDelete(new AnnotationEntityId(annotationId, fileId), softDeleteTime);
    }


    @Transactional
    public void undelete(String fileId, String annotationId) {

        removeRedactionRepository.updateSoftDelete(new AnnotationEntityId(annotationId, fileId), null);
    }


    @Transactional
    public void updateStatus(String fileId, String annotationId, AnnotationStatus annotationStatus) {

        removeRedactionRepository.updateStatus(new AnnotationEntityId(annotationId, fileId), annotationStatus);
    }


    @Transactional
    public void updateStatus(String fileId,
                             String annotationId,
                             AnnotationStatus annotationStatus,
                             boolean isAddOrRemoveFromDictionary,
                             Set<String> typeIdsOfModifiedDictionaries) {

        var idRemoval = removeRedactionRepository.findByIdAndNotSoftDeleted(new AnnotationEntityId(annotationId, fileId))
                .orElseThrow(() -> new NotFoundException("Unknown file/annotation combination: " + fileId + "/" + annotationId));

        idRemoval.setStatus(annotationStatus);
        idRemoval.setRemoveFromDictionary(isAddOrRemoveFromDictionary);
        idRemoval.setTypeIdsOfModifiedDictionaries(typeIdsOfModifiedDictionaries);

        removeRedactionRepository.saveAndFlush(idRemoval);
    }


    @Transactional
    public void markAsProcessed(IdRemoval e) {

        removeRedactionRepository.markAsProcessed(new AnnotationEntityId(e.getAnnotationId(), e.getFileId()), OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
    }

}
