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 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.ManualImageRecategorizationEntity;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.ImageRecategorizationRepository;
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.ImageRecategorizationRequest;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class ImageRecategorizationPersistenceService {

    private final ImageRecategorizationRepository imageRecategorizationRepository;


    public void insert(String fileId, ImageRecategorizationRequest imageRecategorizationRequest) {

        ManualImageRecategorizationEntity manualImageRecategorization = new ManualImageRecategorizationEntity();
        manualImageRecategorization.setId(new AnnotationEntityId(imageRecategorizationRequest.getAnnotationId(), fileId));
        BeanUtils.copyProperties(imageRecategorizationRequest, manualImageRecategorization);
        manualImageRecategorization.setRequestDate(OffsetDateTime.now());
        imageRecategorizationRepository.saveAndFlush(manualImageRecategorization);

    }


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

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


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

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


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

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


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

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


    public ManualImageRecategorizationEntity findRecategorization(String fileId, String annotationId) {

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


    public List<ManualImageRecategorizationEntity> findRecategorizations(String fileId, boolean includeDeletions) {

        return imageRecategorizationRepository.findByFileIdIncludeDeletions(fileId, includeDeletions);

    }


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

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

}
