use std::fmt;

#[derive(Debug, Clone)]
pub enum ControlPlaneError {
    NotFound(String),
    Conflict(String),
    InvalidInput(String),
    Storage(String),
    Unauthorized(String),
    SessionExpired(String),
}

impl fmt::Display for ControlPlaneError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ControlPlaneError::NotFound(msg) => write!(f, "NotFound: {msg}"),
            ControlPlaneError::Conflict(msg) => write!(f, "Conflict: {msg}"),
            ControlPlaneError::InvalidInput(msg) => write!(f, "InvalidInput: {msg}"),
            ControlPlaneError::Storage(msg) => write!(f, "Storage: {msg}"),
            ControlPlaneError::Unauthorized(msg) => write!(f, "Unauthorized: {msg}"),
            ControlPlaneError::SessionExpired(msg) => write!(f, "SessionExpired: {msg}"),
        }
    }
}

impl std::error::Error for ControlPlaneError {}

impl From<rusqlite::Error> for ControlPlaneError {
    fn from(e: rusqlite::Error) -> Self {
        ControlPlaneError::Storage(e.to_string())
    }
}

impl From<serde_json::Error> for ControlPlaneError {
    fn from(e: serde_json::Error) -> Self {
        ControlPlaneError::Storage(format!("JSON error: {e}"))
    }
}
