//! Error types for the terminal balls application.
//!
//! This module defines the application's error types using `thiserror` for
//! ergonomic error handling. All errors implement `std::error::Error` and
//! can be easily converted to `anyhow::Error` for propagation.

use thiserror::Error;

/// Application-level errors for the physics simulation.
///
/// Each variant represents a distinct failure category, enabling precise
/// error handling and informative error messages.
#[derive(Error, Debug)]
pub enum AppError {
    /// Terminal initialization or operation failed.
    ///
    /// Wraps I/O errors from crossterm/terminal operations.
    #[error("Terminal error: {0}")]
    Terminal(#[from] std::io::Error),

    /// Physics simulation encountered an error.
    ///
    /// This can occur during world setup, stepping, or constraint solving.
    #[error("Physics error: {0}")]
    Physics(String),

    /// Rendering operation failed.
    ///
    /// Includes canvas allocation failures or invalid coordinate transforms.
    #[error("Render error: {0}")]
    Render(String),

    /// Configuration is invalid.
    ///
    /// Occurs when user provides out-of-range values or invalid combinations.
    #[error("Configuration error: {0}")]
    Config(String),

    /// Event handling failed.
    ///
    /// Mouse/keyboard event processing or dispatch errors.
    #[error("Event error: {0}")]
    Event(String),
}

/// Result type alias using `AppError` as the error type.
///
/// Use this throughout the application for consistent error handling.
pub type AppResult<T> = Result<T, AppError>;
