package com.betriebsratkanzlei.legalconsenthub_middleware.exception

import com.betriebsratkanzlei.legalconsenthub_middleware.smartcard.SmartCardNotFoundException
import com.betriebsratkanzlei.legalconsenthub_middleware.smartcard.SmartCardReadException
import com.betriebsratkanzlei.legalconsenthub_middleware.smartcard.SmartCardUnavailableException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import java.time.Instant

@RestControllerAdvice
class GlobalExceptionHandler {

    @ExceptionHandler(SmartCardNotFoundException::class)
    fun handleSmartCardNotFoundException(ex: SmartCardNotFoundException): ResponseEntity<Map<String, Any>> {
        val problem = mutableMapOf<String, Any>()
        problem["type"] = "https://problems-registry.smartbear.com/not-found"
        problem["title"] = "Smart Card Not Found"
        problem["status"] = HttpStatus.NOT_FOUND.value()
        problem["detail"] = ex.message ?: "Smart card not found"
        problem["instance"] = "/smart-card/info"
        problem["timestamp"] = Instant.now().toString()
        
        return ResponseEntity.status(HttpStatus.NOT_FOUND)
            .header("Content-Type", "application/problem+json")
            .body(problem)
    }

    @ExceptionHandler(SmartCardReadException::class)
    fun handleSmartCardReadException(ex: SmartCardReadException): ResponseEntity<Map<String, Any>> {
        val problem = mutableMapOf<String, Any>()
        problem["type"] = "https://problems-registry.smartbear.com/server-error"
        problem["title"] = "Smart Card Read Error"
        problem["status"] = HttpStatus.INTERNAL_SERVER_ERROR.value()
        problem["detail"] = ex.message ?: "Error reading smart card"
        problem["instance"] = "/smart-card/info"
        problem["timestamp"] = Instant.now().toString()
        
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .header("Content-Type", "application/problem+json")
            .body(problem)
    }

    @ExceptionHandler(SmartCardUnavailableException::class)
    fun handleSmartCardUnavailableException(ex: SmartCardUnavailableException): ResponseEntity<Map<String, Any>> {
        val problem = mutableMapOf<String, Any>()
        problem["type"] = "https://problems-registry.smartbear.com/service-unavailable"
        problem["title"] = "Smart Card Service Unavailable"
        problem["status"] = HttpStatus.SERVICE_UNAVAILABLE.value()
        problem["detail"] = ex.message ?: "Smart card service unavailable"
        problem["instance"] = "/smart-card/info"
        problem["timestamp"] = Instant.now().toString()
        
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
            .header("Content-Type", "application/problem+json")
            .body(problem)
    }

    @ExceptionHandler(Exception::class)
    fun handleGenericException(ex: Exception): ResponseEntity<Map<String, Any>> {
        val problem = mutableMapOf<String, Any>()
        problem["type"] = "about:blank"
        problem["title"] = "Internal Server Error"
        problem["status"] = HttpStatus.INTERNAL_SERVER_ERROR.value()
        problem["detail"] = "An unexpected error occurred: ${ex.message ?: "Unknown error"}"
        problem["timestamp"] = Instant.now().toString()
        
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .header("Content-Type", "application/problem+json")
            .body(problem)
    }
} 