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

import org.springframework.amqp.core.Message;
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.configuration.MessagingConfiguration;
import com.iqser.red.service.persistence.management.v1.processor.model.OCRStatusUpdateResponse;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusService;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class OCRProcessingMessageReceiver {

    private final ObjectMapper objectMapper;
    private final FileStatusService fileStatusService;


    @SneakyThrows
    @RabbitListener(queues = MessagingConfiguration.OCR_STATUS_UPDATE_RESPONSE_QUEUE)
    public void receive(OCRStatusUpdateResponse response) {

        fileStatusService.updateOCRStatus(response);

        log.info("Received message {} in {}", response, MessagingConfiguration.OCR_STATUS_UPDATE_RESPONSE_QUEUE);
    }


    @SneakyThrows
    @RabbitListener(queues = MessagingConfiguration.OCR_STATUS_UPDATE_RESPONSE_DQL)
    public void handleDLQMessage(Message failedMessage) {

        var response = objectMapper.readValue(failedMessage.getBody(), OCRStatusUpdateResponse.class);

        log.info("Received message {} in {}", response, MessagingConfiguration.OCR_STATUS_UPDATE_RESPONSE_DQL);
    }

}
