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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.pdftron.redaction.v1.api.model.RedactionMessage;
import com.iqser.red.service.pdftron.redaction.v1.api.model.RedactionResultDetail;
import com.iqser.red.service.pdftron.redaction.v1.api.model.RedactionResultMessage;
import com.iqser.red.service.pdftron.redaction.v1.api.model.RedactionType;
import com.iqser.red.service.persistence.management.v1.processor.configuration.MessagingConfiguration;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierTemplateEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.FileEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.ReportTemplateEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.download.DownloadStatusEntity;
import com.iqser.red.service.persistence.management.v1.processor.model.RedactionFileResult;
import com.iqser.red.service.persistence.management.v1.processor.service.ColorsService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileManagementStorageService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierTemplatePersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DownloadStatusPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.ReportTemplatePersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.settings.FileManagementServiceSettings;
import com.iqser.red.service.persistence.management.v1.processor.utils.FileSystemBackedArchiver;
import com.iqser.red.service.persistence.service.v1.api.shared.model.audit.AddNotificationRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.DownloadFileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.WorkflowStatus;
import com.iqser.red.service.persistence.service.v1.api.shared.model.download.DownloadStatusValue;
import com.iqser.red.service.redaction.report.v1.api.model.ReportResultMessage;
import com.iqser.red.service.redaction.report.v1.api.model.StoredFileInformation;
import com.knecon.fforesight.tenantcommons.TenantContext;

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

@Slf4j
@Service
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class DownloadPreparationService {
    static int MAX_RETRY = 2;
    DownloadStatusPersistenceService downloadStatusPersistenceService;
    FileManagementStorageService fileManagementStorageService;
    ReportTemplatePersistenceService reportTemplatePersistenceService;
    NotificationPersistenceService notificationPersistenceService;
    RabbitTemplate rabbitTemplate;
    ObjectMapper objectMapper;
    DownloadReportCleanupService downloadReportCleanupService;
    ColorsService colorsService;
    FileManagementServiceSettings settings;
    DossierTemplatePersistenceService dossierTemplatePersistenceService;
    Map<String, List<RedactionFileResult>> redactionFileResultsMap;


    @Transactional
    public void createDownload(ReportResultMessage reportResultMessage) {

        var downloadId = reportResultMessage.getDownloadId();
        var downloadStatus = downloadStatusPersistenceService.getStatus(downloadId);
        RedactionMessage.RedactionMessageBuilder messageBuilder = this.generateGeneralRedactionMessage(downloadId, downloadStatus);

        redactionFileResultsMap.put(downloadId, new ArrayList<>());

        downloadStatus.getFiles().forEach(fileEntity -> {
            RedactionMessage message = messageBuilder.fileId(fileEntity.getId()).unapprovedFile(fileEntity.getWorkflowStatus() != WorkflowStatus.APPROVED).build();
            redactionFileResultsMap.get(downloadId).add(RedactionFileResult.builder().fileId(fileEntity.getId()).processed(false).retryCounter(0).build());
            log.info("Sending redaction request for downloadId:{} fileId:{} to pdftron-redaction-queue", downloadId, fileEntity.getId());
            rabbitTemplate.convertAndSend(MessagingConfiguration.PDFTRON_QUEUE, message);
        });
    }

    private RedactionMessage.RedactionMessageBuilder generateGeneralRedactionMessage(String downloadId, DownloadStatusEntity downloadStatus) {

        var dossier = downloadStatus.getDossier();
        var storedPreviewColor = downloadStatus.getRedactionPreviewColor();
        var dossierTemplate = dossierTemplatePersistenceService.getDossierTemplate(dossier.getDossierTemplateId());

        final String previewColor;
        if (storedPreviewColor == null || storedPreviewColor.isBlank()) {
            var colors = colorsService.getColors(dossier.getDossierTemplateId());
            previewColor = colors.getPreviewColor();
        } else {
            previewColor = storedPreviewColor;
        }
        String appliedRedactionColor = colorsService.getColors(dossier.getDossierTemplateId()).getAppliedRedactionColor();
        return buildRedactionMessage(downloadId, downloadStatus, dossier, dossierTemplate, previewColor, appliedRedactionColor);
    }

    private RedactionMessage.RedactionMessageBuilder buildRedactionMessage(String downloadId,
                                                                           DownloadStatusEntity downloadStatus,
                                                                           DossierEntity dossier,
                                                                           DossierTemplateEntity dossierTemplate,
                                                                           String previewColor,
                                                                           String appliedRedactionColor) {

        return RedactionMessage.builder()
                .dossierId(dossier.getId())
                .downloadId(downloadId)
                .redactionTypes(toPdfTronRedactionTypes(downloadStatus.getDownloadFileTypes()))
                .redactionPreviewColor(previewColor)
                .keepImageMetaData(dossierTemplate.isKeepImageMetadata())
                .keepHiddenText(dossierTemplate.isKeepHiddenText())
                .keepOverlappingObjects(dossierTemplate.isKeepOverlappingObjects())
                .appliedRedactionColor(appliedRedactionColor);
    }


    private List<RedactionType> toPdfTronRedactionTypes(Collection<DownloadFileType> downloadFileTypes) {

        var result = new LinkedList<RedactionType>();
        for (var downloadFileType : downloadFileTypes) {
            switch (downloadFileType) {
                case REDACTED -> result.add(RedactionType.REDACTED);
                case PREVIEW -> result.add(RedactionType.PREVIEW);
                case DELTA_PREVIEW -> result.add(RedactionType.DELTA);
                default -> {
                }
                // Other types don't need to be passed to pdf-tron
            }
        }

        return result;
    }

    public void processingRedactionResultMessage(RedactionResultMessage redactionResultMessage) {

        String downloadId = redactionResultMessage.getDownloadId();
        List<RedactionFileResult> redactionFileResults = redactionFileResultsMap.get(downloadId);
        if (redactionFileResults == null) {
            log.info("The creation of download has finished for downloadId: {} ", downloadId);
            return;
        }
        long totalFiles = downloadStatusPersistenceService.getStatus(downloadId).getFiles().size();
        Optional<RedactionFileResult> resultOptional = redactionFileResults.stream().filter(r -> r.getFileId().equals(redactionResultMessage.getFileId())).findFirst();
        resultOptional.ifPresent(redactionFileResult -> {
            redactionFileResult.setRedactionResultDetailList(redactionResultMessage.getRedactionResultDetails());
            redactionFileResult.setProcessed(true);
        });

        var processedFilesCount = redactionFileResults.stream().filter(RedactionFileResult::isProcessed).count();
        log.info("Processed {} files out of total {} files for download {}", processedFilesCount, totalFiles, downloadId);

        if (processedFilesCount == totalFiles) {
            createDownload(redactionFileResults, downloadId);
        }
    }

    public void checkForRetryProcess(String downloadId, String fileId, boolean unapprovedFile) {

        List<RedactionFileResult> redactionFileResults = redactionFileResultsMap.get(downloadId);
        if (redactionFileResults == null) {
            log.info("The creation of download has finished for downloadId: {} ", downloadId);
            return;
        }
        Optional<RedactionFileResult> resultOptional = redactionFileResults.stream().filter(r -> r.getFileId().equals(fileId)).findFirst();
        if (resultOptional.isPresent()) {
            RedactionFileResult result = resultOptional.get();
            if (result.getRetryCounter() >= MAX_RETRY) { // update download status
                log.info("Failed download after max retries: {} for downloadId: {}, set the download status to FAILED", result.getRetryCounter(), downloadId);
                downloadStatusPersistenceService.updateStatus(downloadId, DownloadStatusValue.FAILED);
            } else { // retry to send it again
                result.increaseRetryCounter();
                var downloadStatus = downloadStatusPersistenceService.getStatus(downloadId);
                RedactionMessage.RedactionMessageBuilder messageBuilder = this.generateGeneralRedactionMessage(downloadId, downloadStatus);
                RedactionMessage message = messageBuilder.fileId(fileId).unapprovedFile(unapprovedFile).build();
                log.info("Resending redaction request for downloadId:{} fileId: {} to {}", downloadId, fileId, MessagingConfiguration.PDFTRON_QUEUE);
                rabbitTemplate.convertAndSend(MessagingConfiguration.PDFTRON_QUEUE, message);
            }
        }
    }

    public void createDownload(List<RedactionFileResult> redactionFileResults, String downloadId) {

        DownloadStatusEntity downloadStatus = downloadStatusPersistenceService.getStatus(downloadId);

        var storedFileInformations = getStoredFileInformation(downloadId);

        try (FileSystemBackedArchiver fileSystemBackedArchiver = new FileSystemBackedArchiver()) {

            generateAndAddFiles(downloadStatus, redactionFileResults, fileSystemBackedArchiver);
            addReports(downloadId, downloadStatus, storedFileInformations, fileSystemBackedArchiver);
            storeZipFile(downloadStatus, fileSystemBackedArchiver);

            updateStatusToReady(downloadStatus, fileSystemBackedArchiver);
            redactionFileResultsMap.remove(downloadId);

            notificationPersistenceService.insertNotification(AddNotificationRequest.builder()
                    .userId(downloadStatus.getUserId())
                    .issuerId(downloadStatus.getUserId())
                    .notificationType("DOWNLOAD_READY")
                    .target(Map.of("userId", downloadStatus.getUserId(), //
                            "dossierId", downloadStatus.getDossier().getId(), //
                            "downloadId", downloadStatus.getStatus()))
                    .build());
        }

        downloadReportCleanupService.deleteTmpReportFiles(storedFileInformations.stream().map(StoredFileInformation::getStorageId).collect(Collectors.toSet()));
        redactionFileResults.forEach(redactionFileResult -> downloadReportCleanupService.deleteTmpReportFiles(redactionFileResult.getRedactionResultDetailList()
                .stream()
                .map(RedactionResultDetail::getStorageId)
                .collect(Collectors.toSet())));

    }


    private void updateStatusToReady(DownloadStatusEntity downloadStatus, FileSystemBackedArchiver fileSystemBackedArchiver) {

        downloadStatusPersistenceService.updateStatus(downloadStatus, DownloadStatusValue.READY, fileSystemBackedArchiver.getContentLength());
    }


    private void generateAndAddFiles(DownloadStatusEntity downloadStatus, List<RedactionFileResult> redactionFileResults, FileSystemBackedArchiver fileSystemBackedArchiver) {

        int i = 1;
        long fileGenerationStart = System.currentTimeMillis();
        for (FileEntity file : downloadStatus.getFiles()) {

            long start = System.currentTimeMillis();
            byte[] original = fileManagementStorageService.getStoredObjectBytes(file.getDossierId(), file.getId(), FileType.ORIGIN);

            var isFileApproved = WorkflowStatus.APPROVED.equals(file.getWorkflowStatus());
            String filename = isFileApproved ? file.getFilename() : "UNAPPROVED_" + file.getFilename();
            for (DownloadFileType downloadFileType : downloadStatus.getDownloadFileTypes()) {
                Optional<RedactionFileResult> redactionFileResult = redactionFileResults.stream().filter(rfr -> rfr.getFileId().equals(file.getId())).findFirst();

                if (redactionFileResult.isEmpty()) {
                    throw new RuntimeException();
                }

                if (downloadFileType.name().equals(DownloadFileType.ORIGINAL.name())) {
                    fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Original", filename, original));
                }
                if (downloadFileType.name().equals(DownloadFileType.PREVIEW.name())) {
                    fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", addSuffix(filename, "highlighted"), //
                            getPreview(file.getId(), redactionFileResult.get().getRedactionResultDetailList())));
                }
                if (downloadFileType.name().equals(DownloadFileType.DELTA_PREVIEW.name())) {
                    fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Delta Preview", addSuffix(filename, "delta_highlighted"), //
                            getDeltaPreview(file.getId(), redactionFileResult.get().getRedactionResultDetailList())));
                }
                if (downloadFileType.name().equals(DownloadFileType.REDACTED.name()) && isFileApproved) {
                    fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Redacted", addSuffix(file.getFilename(), "redacted"), //
                            getRedacted(file.getId(), redactionFileResult.get().getRedactionResultDetailList())));
                }

            }
            log.info("Successfully added file {}/{} for downloadId {}, took {}", i, downloadStatus.getFiles().size(), downloadStatus.getStorageId(), System.currentTimeMillis() - start);
            i++;
        }
        log.info("Successfully added {} files for downloadId {}, took {}", downloadStatus.getFiles().stream().map(FileEntity::getId).toList(), downloadStatus.getStorageId(), System.currentTimeMillis() - fileGenerationStart);
    }


    private byte[] getRedacted(String fileId, List<RedactionResultDetail> redactionResultDetails) {

        return getStoredFileBytes(fileId, redactionResultDetails, RedactionType.REDACTED);
    }


    private byte[] getStoredFileBytes(String fileId, List<RedactionResultDetail> redactionResultDetails, RedactionType redactionType) {

        var redactionResultDetail = redactionResultDetails.stream().filter(rrd -> rrd.getRedactionType() == redactionType).findFirst();
        if (redactionResultDetail.isPresent()) {
            try (var inputStream = fileManagementStorageService.getObject(TenantContext.getTenantId(), redactionResultDetail.get().getStorageId())) {
                byte[] storedFileBytes = inputStream.readAllBytes();
                inputStream.close();
                return storedFileBytes;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        throw new RuntimeException(String.format("Result for file %s of type %s not found", fileId, redactionType));
    }


    private byte[] getDeltaPreview(String fileId, List<RedactionResultDetail> redactionResultDetails) {

        return getStoredFileBytes(fileId, redactionResultDetails, RedactionType.DELTA);
    }


    private byte[] getPreview(String fileId, List<RedactionResultDetail> redactionResultDetails) {

        return getStoredFileBytes(fileId, redactionResultDetails, RedactionType.PREVIEW);
    }


    private void addReports(String downloadId, DownloadStatusEntity downloadStatus, List<StoredFileInformation> storedFileInformations, FileSystemBackedArchiver fileSystemBackedArchiver) {

        long addReportsStart = System.currentTimeMillis();

        Map<String, FileEntity> fileById = downloadStatus.getFiles().stream().collect(Collectors.toMap(FileEntity::getId, a -> a));

        for (StoredFileInformation storedFileInformation : storedFileInformations) {

            FileEntity fileStatus = null;

            if (storedFileInformation.getFileId() != null) {
                fileStatus = fileById.get(storedFileInformation.getFileId());
            }

            ReportTemplateEntity reportTemplate = reportTemplatePersistenceService.find(storedFileInformation.getTemplateId());
            byte[] report = fileManagementStorageService.getStoredObjectBytes(storedFileInformation.getStorageId());
            fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel(removeExtension(reportTemplate.getFileName()) + (reportTemplate.isMultiFileReport() ? " (multifile)" : ""),
                    settings.isScmEnabled() ? createFileName(fileStatus, reportTemplate) : addSuffix(createFileName(fileStatus, reportTemplate),
                            "justification" + (storedFileInformation.getDocumentPartNr() != 0 ? "_" + storedFileInformation.getDocumentPartNr() : "")),
                    report));

        }
        log.info("Successfully added {} reports for downloadId {}, took {}", storedFileInformations.size(), downloadId, System.currentTimeMillis() - addReportsStart);
    }


    @SneakyThrows
    private List<StoredFileInformation> getStoredFileInformation(String downloadId) {

        var storageId = downloadId.substring(0, downloadId.length() - 3) + "/REPORT_INFO.json";

        return objectMapper.readValue(fileManagementStorageService.getStoredObjectBytes(storageId), new TypeReference<>() {
        });
    }


    private String createFileName(FileEntity fileStatus, ReportTemplateEntity reportTemplate) {

        if (fileStatus != null) {
            var isFileApproved = WorkflowStatus.APPROVED.equals(fileStatus.getWorkflowStatus());
            String filename = isFileApproved ? fileStatus.getFilename() : "UNAPPROVED_" + fileStatus.getFilename();
            return removeExtension(filename) + getExtension(reportTemplate.getFileName());
        } else {
            return reportTemplate.getFileName();
        }
    }


    private String getExtension(String fileName) {

        var index = fileName.lastIndexOf(".");
        if (index > 0) {
            return fileName.substring(index);
        } else {
            return "";
        }
    }


    private String removeExtension(String fileName) {

        var index = fileName.lastIndexOf(".");
        if (index > 0) {
            return fileName.substring(0, index);
        } else {
            return fileName;
        }
    }


    private String addSuffix(String filename, String suffix) {

        var oldExtension = getExtension(filename);
        return removeExtension(filename) + "_" + suffix + oldExtension;

    }


    private void storeZipFile(DownloadStatusEntity downloadStatus, FileSystemBackedArchiver fileSystemBackedArchiver) {

        long start = System.currentTimeMillis();
        fileManagementStorageService.storeObject(downloadStatus.getStorageId(), fileSystemBackedArchiver.toInputStream());
        log.info("Successfully stored zip for downloadId {}, took {}", downloadStatus.getStorageId(), System.currentTimeMillis() - start);
    }

}
