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

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.transaction.Transactional;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.management.v1.processor.configuration.MessagingConfiguration;
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.DownloadJob;
import com.iqser.red.service.persistence.management.v1.processor.service.DossierService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DownloadStatusPersistenceService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.download.DownloadStatusValue;
import com.iqser.red.service.redaction.report.v1.api.model.ReportRequestMessage;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class DownloadProcessorService {

    private final DownloadStatusPersistenceService downloadStatusPersistenceService;
    private final DossierService dossierService;
    private final RabbitTemplate rabbitTemplate;
    private final ObjectMapper objectMapper;


    @Transactional
    public void processDownload(DownloadJob downloadJob) {

        DownloadStatusEntity downloadStatus = downloadStatusPersistenceService.getStatus(downloadJob.getStorageId());
        downloadStatusPersistenceService.updateStatus(downloadJob.getStorageId(), DownloadStatusValue.GENERATING);

        var dossier = dossierService.getDossierById(downloadStatus.getFiles().iterator().next().getDossierId());

        List<String> filenameSortedFileIds = downloadStatus.getFiles()
                .stream()
                .sorted(Comparator.comparing(FileEntity::getFilename))
                .map(FileEntity::getId)
                .collect(Collectors.toList());

        addReportQueue(ReportRequestMessage.builder()
                .userId(downloadJob.getUserId())
                .downloadId(downloadJob.getStorageId())
                .dossierId(dossier.getId())
                .dossierTemplateId(dossierService.getDossierById(dossier.getId()).getDossierTemplateId())
                .fileIds(filenameSortedFileIds)
                .templateIds(downloadStatus.getReports().stream().map(ReportTemplateEntity::getTemplateId).collect(Collectors.toSet()))
                .build(), 1);
    }


    private void addReportQueue(ReportRequestMessage reportRequestMessage, int priority) {

        rabbitTemplate.convertAndSend(MessagingConfiguration.REPORT_QUEUE, reportRequestMessage, message -> {
            message.getMessageProperties().setPriority(priority);
            return message;
        });
    }

}
