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

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;

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.ManualForceRedactionEntity;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.ForceRedactionRepository;
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.ForceRedactionRequest;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class ForceRedactionPersistenceService {

    private final ForceRedactionRepository forceRedactionRepository;


    public void insert(String fileId, ForceRedactionRequest forceRedactionRequest) {

        ManualForceRedactionEntity manualForceRedaction = new ManualForceRedactionEntity();
        manualForceRedaction.setId(new AnnotationEntityId(forceRedactionRequest.getAnnotationId(), fileId));
        BeanUtils.copyProperties(forceRedactionRequest, manualForceRedaction);
        manualForceRedaction.setRequestDate(OffsetDateTime.now());
        forceRedactionRepository.saveAndFlush(manualForceRedaction);
    }


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

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


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

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


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

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


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

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


    public ManualForceRedactionEntity findForceRedaction(String fileId, String annotationId) {

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


    public Set<ManualForceRedactionEntity> findForceRedactions(String fileId, boolean includeDeletions) {

        return new HashSet<>(forceRedactionRepository.findByFileIdIncludeDeletions(fileId, includeDeletions));
    }


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

        forceRedactionRepository.markAsProcessed(new AnnotationEntityId(annotationId, fileId), OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
    }

}
