package dp.kotul.templategroupsbackend.exception

import dp.kotul.starter.ParentExceptionHandler
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.http.HttpStatus.CONFLICT
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.context.request.WebRequest

@ControllerAdvice
class ExceptionHandler : ParentExceptionHandler() {

    private val log = LoggerFactory.getLogger(ExceptionHandler::class.java)

    @ExceptionHandler(TransitiveTemplateVariableDependencyException::class)
    fun handleTransitiveTemplateVariableDependencyException(ex: Exception): ResponseEntity<Any> {
        log.warn("TransitiveTemplateVariableDependencyException: ", ex)

        return ResponseEntity(
            mapOf("status" to BAD_REQUEST.value(),
                "error" to BAD_REQUEST.reasonPhrase,
                "message" to ex.localizedMessage),
            BAD_REQUEST)
    }

    @ExceptionHandler(NotIncludedTemplateVariableException::class)
    fun handleNotIncludedTemplateVariableExceptionException(ex: Exception): ResponseEntity<Any> {
        log.warn("NotIncludedTemplateVariableException: ", ex)

        return ResponseEntity(
            mapOf("status" to BAD_REQUEST.value(),
                "error" to BAD_REQUEST.reasonPhrase,
                "message" to ex.localizedMessage),
            BAD_REQUEST)
    }

    @ExceptionHandler(WrongTemplateGroupSetupException::class)
    fun handleWrongTemplateGroupSetupException(ex: Exception): ResponseEntity<Any> {
        log.warn("WrongTemplateGroupSetupException: ", ex)

        return ResponseEntity(
            mapOf("status" to CONFLICT.value(),
                "error" to CONFLICT.reasonPhrase,
                "message" to ex.localizedMessage),
            CONFLICT)
    }
}
