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

import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.*;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.CommentRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.FileRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.*;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;

import jakarta.transaction.Transactional;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class SaasAnnotationIdMigrationService {

    ManualRedactionRepository manualRedactionRepository;
    RemoveRedactionRepository removeRedactionRepository;
    ForceRedactionRepository forceRedactionRepository;
    ResizeRedactionRepository resizeRedactionRepository;
    RecategorizationRepository recategorizationRepository;
    LegalBasisChangeRepository legalBasisChangeRepository;
    CommentRepository commentRepository;
    FileRepository fileRepository;


    public int updateManualAddRedaction(AnnotationEntityId oldAnnotationEntityId, AnnotationEntityId newAnnotationEntityId) {

        if (oldAnnotationEntityId.equals(newAnnotationEntityId)) {
            return 0;
        }
        var oldEntry = manualRedactionRepository.findById(oldAnnotationEntityId);
        if (oldEntry.isPresent()) {

            var newEntry = MagicConverter.convert(oldEntry.get(), ManualRedactionEntryEntity.class);
            newEntry.setPositions(MagicConverter.convert(oldEntry.get().getPositions(), RectangleEntity.class));
            newEntry.setFileStatus(fileRepository.findById(oldAnnotationEntityId.getFileId())
                                           .get());
            newEntry.setId(newAnnotationEntityId);

            manualRedactionRepository.deleteById(oldAnnotationEntityId);
            manualRedactionRepository.save(newEntry);
            return 1;
        }
        return 0;
    }


    public int updateRemoveRedaction(AnnotationEntityId oldAnnotationEntityId, AnnotationEntityId newAnnotationEntityId) {

        if (oldAnnotationEntityId.equals(newAnnotationEntityId)) {
            return 0;
        }
        var oldEntry = removeRedactionRepository.findById(oldAnnotationEntityId);
        if (oldEntry.isPresent()) {

            var newEntry = MagicConverter.convert(oldEntry.get(), IdRemovalEntity.class);
            newEntry.setFileStatus(fileRepository.findById(oldAnnotationEntityId.getFileId())
                                           .get());
            newEntry.setId(newAnnotationEntityId);

            removeRedactionRepository.deleteById(oldAnnotationEntityId);
            removeRedactionRepository.save(newEntry);
            return 1;
        }
        return 0;
    }


    public int updateForceRedaction(AnnotationEntityId oldAnnotationEntityId, AnnotationEntityId newAnnotationEntityId) {

        if (oldAnnotationEntityId.equals(newAnnotationEntityId)) {
            return 0;
        }
        var oldEntry = forceRedactionRepository.findById(oldAnnotationEntityId);
        if (oldEntry.isPresent()) {

            var newEntry = MagicConverter.convert(oldEntry.get(), ManualForceRedactionEntity.class);
            newEntry.setFileStatus(fileRepository.findById(oldAnnotationEntityId.getFileId())
                                           .get());
            newEntry.setId(newAnnotationEntityId);

            forceRedactionRepository.deleteById(oldAnnotationEntityId);
            forceRedactionRepository.save(newEntry);
            return 1;
        }
        return 0;
    }


    public int updateResizeRedaction(AnnotationEntityId oldAnnotationEntityId, AnnotationEntityId newAnnotationEntityId) {

        if (oldAnnotationEntityId.equals(newAnnotationEntityId)) {
            return 0;
        }
        var oldEntry = resizeRedactionRepository.findById(oldAnnotationEntityId);
        if (oldEntry.isPresent()) {

            var newEntry = MagicConverter.convert(oldEntry.get(), ManualResizeRedactionEntity.class);
            newEntry.setId(newAnnotationEntityId);
            newEntry.setPositions(MagicConverter.convert(oldEntry.get().getPositions(), RectangleEntity.class));
            newEntry.setFileStatus(fileRepository.findById(oldAnnotationEntityId.getFileId())
                                           .get());

            resizeRedactionRepository.deleteById(oldAnnotationEntityId);
            resizeRedactionRepository.save(newEntry);
            return 1;
        }
        return 0;
    }


    public int updateRecategorizationRedaction(AnnotationEntityId oldAnnotationEntityId, AnnotationEntityId newAnnotationEntityId) {

        if (oldAnnotationEntityId.equals(newAnnotationEntityId)) {
            return 0;
        }
        var oldEntry = recategorizationRepository.findById(oldAnnotationEntityId);
        if (oldEntry.isPresent()) {

            var newEntry = MagicConverter.convert(oldEntry.get(), ManualRecategorizationEntity.class);
            newEntry.setId(newAnnotationEntityId);
            newEntry.setFileStatus(fileRepository.findById(oldAnnotationEntityId.getFileId())
                                           .get());

            recategorizationRepository.deleteById(oldAnnotationEntityId);
            recategorizationRepository.save(newEntry);
            return 1;
        }
        return 0;
    }


    public int updateLegalBasisChangeRedaction(AnnotationEntityId oldAnnotationEntityId, AnnotationEntityId newAnnotationEntityId) {

        if (oldAnnotationEntityId.equals(newAnnotationEntityId)) {
            return 0;
        }
        var oldEntry = legalBasisChangeRepository.findById(oldAnnotationEntityId);
        if (oldEntry.isPresent()) {

            var newEntry = MagicConverter.convert(oldEntry.get(), ManualLegalBasisChangeEntity.class);
            newEntry.setId(newAnnotationEntityId);
            newEntry.setFileStatus(fileRepository.findById(oldAnnotationEntityId.getFileId())
                                           .get());

            legalBasisChangeRepository.deleteById(oldAnnotationEntityId);
            legalBasisChangeRepository.save(newEntry);
            return 1;
        }
        return 0;
    }


    public int updateCommentIds(String fileId, String key, String value) {

        if (key.equals(value)) {
            return 0;
        }
        return commentRepository.saasMigrationUpdateAnnotationIds(fileId, key, value);
    }

}
