use std::fmt;

/// Error types for Topaz Video AI SDK
#[derive(Debug)]
pub enum TvaiError {
    /// IO error
    IoError(std::io::Error),
    /// JSON parsing error
    JsonParseError(serde_json::Error),
    /// JSON serialization error
    JsonSerializeError(serde_json::Error),
    /// Template validation error
    ValidationError(String),
    /// Template not found error
    TemplateNotFound(String),
    /// FFmpeg command generation error
    FfmpegError(String),
    /// Model mapping error
    ModelMappingError(String),
    /// Configuration error
    ConfigError(String),
}

impl fmt::Display for TvaiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TvaiError::IoError(err) => write!(f, "IO error: {}", err),
            TvaiError::JsonParseError(err) => write!(f, "JSON parsing error: {}", err),
            TvaiError::JsonSerializeError(err) => write!(f, "JSON serialization error: {}", err),
            TvaiError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
            TvaiError::TemplateNotFound(name) => write!(f, "Template not found: {}", name),
            TvaiError::FfmpegError(msg) => write!(f, "FFmpeg error: {}", msg),
            TvaiError::ModelMappingError(msg) => write!(f, "Model mapping error: {}", msg),
            TvaiError::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
        }
    }
}

impl std::error::Error for TvaiError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            TvaiError::IoError(err) => Some(err),
            TvaiError::JsonParseError(err) => Some(err),
            TvaiError::JsonSerializeError(err) => Some(err),
            _ => None,
        }
    }
}

impl From<std::io::Error> for TvaiError {
    fn from(err: std::io::Error) -> Self {
        TvaiError::IoError(err)
    }
}

impl From<serde_json::Error> for TvaiError {
    fn from(err: serde_json::Error) -> Self {
        TvaiError::JsonParseError(err)
    }
}

/// Result type for Topaz Video AI SDK operations
pub type TvaiResult<T> = Result<T, TvaiError>;
