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

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.management.v1.processor.model.AnalysisFlagCalculationMessage;
import com.iqser.red.service.persistence.management.v1.processor.service.AnalysisFlagsCalculationService;

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

    public static final String ANALYSIS_FLAG_CALCULATION_LISTENER_ID = "analysis-flag-calculation-listener";

    AnalysisFlagsCalculationService analysisFlagsCalculationService;
    ObjectMapper mapper;


    @SneakyThrows
    @RabbitHandler
    @RabbitListener(id = ANALYSIS_FLAG_CALCULATION_LISTENER_ID)
    public void receiveAnalysisFlagCalculationMessage(Message message) {

        var analysisFlagCalculationMessage = mapper.readValue(message.getBody(), AnalysisFlagCalculationMessage.class);
        log.debug("Calculating Flags for fileId {} and dossierId {}", analysisFlagCalculationMessage.getFileId(), analysisFlagCalculationMessage.getDossierId());
        analysisFlagsCalculationService.calculateFlags(analysisFlagCalculationMessage.getDossierId(), analysisFlagCalculationMessage.getFileId());
    }

}
