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

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.management.v1.processor.dataexchange.FileExchangeNames;
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.TypeEntity;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusManagementService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DictionaryPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.utils.FileSystemBackedArchiver;
import com.iqser.red.service.persistence.service.v1.api.shared.model.FileExchangeExportRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileModel;

import io.micrometer.observation.annotation.Observed;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

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

    DictionaryPersistenceService dictionaryPersistenceService;
    FileExportService fileExportService;
    FileStatusManagementService fileStatusManagementService;
    EntityTypeExportService entityTypeExportService;
    ObjectMapper mapper;
    FileStatusService fileStatusService;


    @SneakyThrows
    @Observed(name = "FileExchangeExportService", contextualName = "export-dossier")
    public void addDossierToArchive(FileSystemBackedArchiver archiver, Path folder, FileExchangeExportRequest request, Dossier dossier) {

        List<String> fileIdsInDossier = fileStatusManagementService.getDossierStatusIds(dossier.getId(), false)
                .stream()
                .filter(fileId -> request.fileIds().isEmpty() || request.fileIds().contains(fileId))
                .toList();

        if (fileIdsInDossier.isEmpty()) {
            return;
        }

        Path dossierFolder = folder.resolve(dossier.getId());
        archiver.addEntry(new FileSystemBackedArchiver.ArchiveModel(dossierFolder, FileExchangeNames.DOSSIER, mapper.writeValueAsBytes(dossier)));

        for (String fileId : fileIdsInDossier) {

            FileModel file = fileStatusService.getStatus(fileId);

            fileExportService.addFileToArchive(archiver, dossierFolder, request, file);
        }

        List<TypeEntity> types = dictionaryPersistenceService.getAllTypesForDossier(dossier.getId(), false);

        String entitiesFolder = dossierFolder.resolve(FileExchangeNames.DOSSIER_ENTITY_FOLDER).toFile().toString();

        for (TypeEntity type : types) {
            entityTypeExportService.addEntityTypeToArchive(archiver, type, entitiesFolder);
        }
    }

}
