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

import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.FileEntity;
import com.iqser.red.service.persistence.management.v1.processor.mapper.ManualChangesExportMapper;
import com.iqser.red.service.persistence.management.v1.processor.dataexchange.models.manualchanges.ManualChangesExportModel;
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.ForceRedactionRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.LegalBasisChangeRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.ManualRedactionRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.RecategorizationRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.RemoveRedactionRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.ResizeRedactionRepository;

import io.micrometer.observation.annotation.Observed;
import jakarta.transaction.Transactional;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;

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

    ManualRedactionRepository manualRedactionRepository;
    ForceRedactionRepository forceRedactionRepository;
    RecategorizationRepository recategorizationRepository;
    RemoveRedactionRepository removeRedactionRepository;
    ResizeRedactionRepository resizeRedactionRepository;
    LegalBasisChangeRepository legalBasisChangeRepository;

    FileRepository fileRepository;

    ManualChangesExportMapper mapper = ManualChangesExportMapper.INSTANCE;


    @Transactional
    @Observed(name = "ManualChangesImportService", contextualName = "import-manual-changes")
    public void importManualChanges(ManualChangesExportModel exportModel, String fileId, String userId) {

        FileEntity fileEntity = fileRepository.findById(fileId)
                .orElseThrow();
        exportModel.getManualAddExportModels()
                .stream()
                .map(model -> mapper.toEntity(model, fileEntity, userId))
                .forEach(manualRedactionRepository::saveAndFlush);

        exportModel.getManualForceExportModels()
                .stream()
                .map(model -> mapper.toEntity(model, fileEntity, userId))
                .forEach(forceRedactionRepository::saveAndFlush);

        exportModel.getManualRecategorizationExportModels()
                .stream()
                .map(model -> mapper.toEntity(model, fileEntity, userId))
                .forEach(recategorizationRepository::saveAndFlush);

        exportModel.getManualRemoveExportModels()
                .stream()
                .map(model -> mapper.toEntity(model, fileEntity, userId))
                .forEach(removeRedactionRepository::saveAndFlush);

        exportModel.getManualResizeExportModels()
                .stream()
                .map(model -> mapper.toEntity(model, fileEntity, userId))
                .forEach(resizeRedactionRepository::saveAndFlush);

        exportModel.getManualLegalBasisChangeExportModels()
                .stream()
                .map(model -> mapper.toEntity(model, fileEntity, userId))
                .forEach(legalBasisChangeRepository::saveAndFlush);

    }

}
