package com.betriebsratkanzlei.legalconsenthub.error

import com.betriebsratkanzlei.legalconsenthub_api.model.ProblemDetails
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.ResponseStatus
import java.net.URI

@ControllerAdvice
class ExceptionHandler {
    var logger = LoggerFactory.getLogger(ExceptionHandler::class.java)

    @ResponseBody
    @ExceptionHandler(ApplicationFormNotFoundException::class, UserNotFoundException::class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    fun handleNotFoundError(e: Exception): ResponseEntity<ProblemDetails> {
        logger.warn(e.message, e)
        return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .body(
                ProblemDetails(
                    title = "Not Found",
                    status = HttpStatus.NOT_FOUND.value(),
                    type = URI.create("about:blank"),
                    detail = e.message ?: "Something went wrong"
                )
            )
    }

    @ResponseBody
    @ExceptionHandler(ApplicationFormInvalidStateException::class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    fun handleInvalidStateError(e: ApplicationFormInvalidStateException): ResponseEntity<ProblemDetails> {
        logger.warn(e.message, e)
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .body(
                ProblemDetails(
                    title = "Invalid State",
                    status = HttpStatus.BAD_REQUEST.value(),
                    type = URI.create("about:blank"),
                    detail = e.message ?: "Operation not allowed in current state"
                )
            )
    }

    @ResponseBody
    @ExceptionHandler(UserAlreadyExistsException::class)
    @ResponseStatus(HttpStatus.CONFLICT)
    fun handleUserAlreadyExistsError(e: UserAlreadyExistsException): ResponseEntity<ProblemDetails> {
        logger.warn(e.message, e)
        return ResponseEntity.status(HttpStatus.CONFLICT).contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .body(
                ProblemDetails(
                    title = "Conflict",
                    status = HttpStatus.CONFLICT.value(),
                    type = URI.create("about:blank"),
                    detail = e.message ?: "Resource already exists"
                )
            )
    }

    @ResponseBody
    @ExceptionHandler(
        ApplicationFormNotCreatedException::class,
        ApplicationFormNotUpdatedException::class,
        ApplicationFormNotDeletedException::class,
    )
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    fun handleInternalServerError(e: Exception): ResponseEntity<ProblemDetails> {
        logger.warn(e.message, e)
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .body(
                ProblemDetails(
                    title = "Internal Server Error",
                    status = HttpStatus.INTERNAL_SERVER_ERROR.value(),
                    type = URI.create("about:blank"),
                    detail = e.message ?: "Something went wrong"
                )
            )
    }
}
