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

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;

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.core.type.TypeReference;
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.service.FileStatusProcessingUpdateService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileErrorInfo;

import io.micrometer.observation.ObservationRegistry;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class AzureNerMessageReceiver {

    public static final String AZURE_ENTITY_RESPONSE_LISTENER_ID = "azure-entity-response-listener";

    private final FileStatusService fileStatusService;
    private final ObjectMapper objectMapper;
    private final FileStatusProcessingUpdateService fileStatusProcessingUpdateService;


    @SneakyThrows
    @RabbitListener(id = AZURE_ENTITY_RESPONSE_LISTENER_ID)
    public void receive(Message message) {

        HashMap<String, Object> entityResponse = objectMapper.readValue(message.getBody(), new TypeReference<>() {
        });

        String dossierId = (String) entityResponse.get("dossierId");
        String fileId = (String) entityResponse.get("fileId");

        log.info("Received message {} for dossierId {} and fileId  {}", MessagingConfiguration.AZURE_ENTITY_RESPONSE_EXCHANGE, dossierId, fileId);
        fileStatusService.setStatusAnalyse(dossierId, fileId, false);
    }


    @RabbitListener(queues = MessagingConfiguration.AZURE_ENTITY_DLQ)
    public void handleDLQMessage(Message failedMessage) throws IOException {

        HashMap<String, Object> entityResponse = objectMapper.readValue(failedMessage.getBody(), new TypeReference<>() {
        });
        String dossierId = (String) entityResponse.get("dossierId");
        String fileId = (String) entityResponse.get("fileId");

        log.warn("Received message {} for dossierId {} and fileId {}", MessagingConfiguration.AZURE_ENTITY_DLQ, dossierId, fileId);
        fileStatusProcessingUpdateService.analysisFailed(dossierId,
                                                         fileId,
                                                         new FileErrorInfo("azure ner service failed",
                                                                           MessagingConfiguration.AZURE_ENTITY_DLQ,
                                                                           "azure-ner-service",
                                                                           OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)));
    }

}
