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.mapper.ManualChangesExportMapper;
import com.iqser.red.service.persistence.management.v1.processor.model.ManualChangesQueryOptions;
import com.iqser.red.service.persistence.management.v1.processor.dataexchange.models.manualchanges.ManualChangesExportModel;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.AddRedactionPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.ForceRedactionPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.LegalBasisChangePersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.RecategorizationPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.RemoveRedactionPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.annotations.ResizeRedactionPersistenceService;

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 ManualChangesExportService {

    AddRedactionPersistenceService addRedactionPersistenceService;
    RemoveRedactionPersistenceService removeRedactionPersistenceService;
    ForceRedactionPersistenceService forceRedactionPersistenceService;
    RecategorizationPersistenceService recategorizationPersistenceService;
    LegalBasisChangePersistenceService legalBasisChangePersistenceService;
    ResizeRedactionPersistenceService resizeRedactionPersistenceService;

    ManualChangesExportMapper mapper = ManualChangesExportMapper.INSTANCE;


    @Transactional
    @Observed(name = "ManualChangesExportService", contextualName = "export-manual-changes")
    public ManualChangesExportModel export(String fileId) {

        ManualChangesExportModel exportModel = new ManualChangesExportModel();
        exportModel.setManualAddExportModels(addRedactionPersistenceService.findEntriesByFileIdAndOptions(fileId, ManualChangesQueryOptions.all())
                                               .stream()
                                               .map(mapper::toModel)
                                               .toList());
        exportModel.setManualRemoveExportModels(removeRedactionPersistenceService.findEntriesByFileIdAndOptions(fileId, ManualChangesQueryOptions.all())
                                                  .stream()
                                                  .map(mapper::toModel)
                                                  .toList());
        exportModel.setManualForceExportModels(forceRedactionPersistenceService.findEntriesByFileIdAndOptions(fileId, ManualChangesQueryOptions.all())
                                                 .stream()
                                                 .map(mapper::toModel)
                                                 .toList());
        exportModel.setManualLegalBasisChangeExportModels(legalBasisChangePersistenceService.findEntriesByFileIdAndOptions(fileId, ManualChangesQueryOptions.all())
                                                            .stream()
                                                            .map(mapper::toModel)
                                                            .toList());
        exportModel.setManualRecategorizationExportModels(recategorizationPersistenceService.findEntriesByFileIdAndOptions(fileId, ManualChangesQueryOptions.all())
                                                            .stream()
                                                            .map(mapper::toModel)
                                                            .toList());
        exportModel.setManualResizeExportModels(resizeRedactionPersistenceService.findEntriesByFileIdAndOptions(fileId, ManualChangesQueryOptions.all())
                                                  .stream()
                                                  .map(mapper::toModel)
                                                  .toList());
        return exportModel;

    }

}
