use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use aws_local_core::errors::aws_error_response;

#[derive(Debug)]
pub enum RdsError {
    ResourceNotFound(String),
    AlreadyExists(String),
    InvalidInput(String),
    InternalError(String),
}

impl RdsError {
    fn type_and_status(&self) -> (&str, StatusCode) {
        match self {
            Self::ResourceNotFound(_) => ("DBInstanceNotFoundFault", StatusCode::NOT_FOUND),
            Self::AlreadyExists(_) => ("DBInstanceAlreadyExistsFault", StatusCode::CONFLICT),
            Self::InvalidInput(_) => ("InvalidParameterValue", StatusCode::BAD_REQUEST),
            Self::InternalError(_) => ("InternalFailure", StatusCode::INTERNAL_SERVER_ERROR),
        }
    }

    fn message(&self) -> &str {
        match self {
            Self::ResourceNotFound(m) | Self::AlreadyExists(m)
            | Self::InvalidInput(m) | Self::InternalError(m) => m,
        }
    }
}

impl IntoResponse for RdsError {
    fn into_response(self) -> Response {
        let (t, s) = self.type_and_status();
        aws_error_response(s, t, self.message())
    }
}
