package org.luxons.sevenwonders.errors;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;

@ControllerAdvice
public class ExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    private static final String ERROR_CODE_VALIDATION = "VALIDATION_ERROR";

    private static final String ERROR_MSG_VALIDATION = "Input invalid";

    private final MessageSource messageSource;

    @Autowired
    public ExceptionHandler(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleValidationError(MethodArgumentNotValidException exception) {
        logger.error("Invalid input", exception);
        List<ObjectError> errors = exception.getBindingResult().getAllErrors();
        UIError uiError = new UIError(ERROR_CODE_VALIDATION, ERROR_MSG_VALIDATION, ErrorType.VALIDATION);
        uiError.setValidationErrors(errors);
        return uiError;
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleGenericUserError(UserInputException exception) {
        logger.error("Incorrect user input: " + exception.getMessage());
        String messageKey = exception.getMessageResourceKey();
        String message = messageSource.getMessage(messageKey, exception.getParams(), messageKey, Locale.US);
        return new UIError(messageKey, message, ErrorType.USER);
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleApiError(ApiMisuseException exception) {
        logger.error("Invalid API input", exception);
        return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL);
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleUnexpectedInternalError(Throwable exception) {
        logger.error("Uncaught exception thrown during message handling", exception);
        return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL);
    }
}

@@ -253,16 +253,91 @@
 Source;%0A
+import org.springframework.messaging.converter.MessageConversionException;%0A
 import o
@@ -405,24 +405,24 @@
 ionHandler;%0A
-
 import org.s
@@ -949,39 +949,205 @@
 ROR_
-MSG_VALIDATION = %22Input invalid
+CODE_CONVERSION = %22MESSAGE_FORMAT_ERROR%22;%0A%0A    private static final String ERROR_MSG_VALIDATION = %22Invalid input data%22;%0A%0A    private static final String ERROR_MSG_CONVERSION = %22Invalid input format
 %22;%0A%0A
@@ -1749,16 +1749,16 @@
 rrors);%0A
-
         
@@ -1772,32 +1772,345 @@
 uiError;%0A    %7D%0A%0A
+    @MessageExceptionHandler%0A    @SendToUser(%22/queue/errors%22)%0A    private UIError handleConversionError(MessageConversionException exception) %7B%0A        logger.error(%22Error interpreting the message%22, exception);%0A        return new UIError(ERROR_CODE_CONVERSION, ERROR_MSG_CONVERSION, ErrorType.VALIDATION);%0A    %7D%0A%0A
     @MessageExce

Add conversion exception handling for bad input JSON format