//! Error types for the CLI.
//!
//! This module defines the unified error type [`CliError`] used throughout the CLI binary.
//! It consolidates errors from downstream crates (driver, proposer, rpc)
//! and whitelist-preconfirmation-driver, as well as CLI-specific errors like URL parsing, runtime
//! initialization, and metrics setup.

use thiserror::Error;

/// Errors that can occur during CLI execution.
///
/// This enum covers all error cases in the CLI binary, including:
/// - Errors propagated from downstream crates (driver, proposer, rpc,
///   whitelist-preconfirmation-driver)
/// - Configuration errors (URL parsing, socket address parsing)
/// - Runtime errors (tokio runtime initialization, I/O)
/// - Metrics initialization errors
#[derive(Debug, Error)]
pub enum CliError {
    /// Error from the driver crate.
    ///
    /// Wraps [`driver::DriverError`] for errors occurring during driver operations
    /// such as event syncing, block derivation, and execution engine communication.
    #[error(transparent)]
    Driver(#[from] driver::DriverError),

    /// Error from the driver sync module.
    ///
    /// Wraps [`driver::sync::SyncError`] for errors occurring during event syncer
    /// initialization and synchronization operations.
    #[error(transparent)]
    Sync(#[from] driver::sync::SyncError),

    /// Error from the proposer crate.
    ///
    /// Wraps [`proposer::error::ProposerError`] for errors occurring during block proposal
    /// operations such as transaction building and L1 submission.
    #[error(transparent)]
    Proposer(#[from] proposer::error::ProposerError),

    /// Error from the RPC client crate.
    ///
    /// Wraps [`rpc::RpcClientError`] for errors occurring during RPC client
    /// initialization and provider communication.
    #[error(transparent)]
    Rpc(#[from] rpc::RpcClientError),

    /// Error from the whitelist preconfirmation driver.
    ///
    /// Wraps [`whitelist_preconfirmation_driver::WhitelistPreconfirmationDriverError`] for
    /// whitelist preconfirmation message validation and insertion failures.
    #[error(transparent)]
    WhitelistPreconfirmation(
        #[from] whitelist_preconfirmation_driver::WhitelistPreconfirmationDriverError,
    ),

    /// Failed to parse a URL.
    ///
    /// Occurs when parsing endpoint URLs from command-line arguments fails.
    /// Common causes include malformed URLs or unsupported schemes.
    #[error("failed to parse URL: {0}")]
    UrlParse(#[from] url::ParseError),

    /// Runtime initialization or I/O error.
    ///
    /// Wraps [`std::io::Error`] for errors occurring during tokio runtime
    /// initialization or general I/O operations.
    #[error("runtime error: {0}")]
    Runtime(#[from] std::io::Error),

    /// Failed to parse a socket address.
    ///
    /// Occurs when parsing metrics server addresses from command-line arguments fails.
    #[error("invalid socket address: {0}")]
    AddrParse(#[from] std::net::AddrParseError),

    /// Invalid L1 transport configuration.
    ///
    /// Occurs when CLI arguments or programmatic construction provide either zero or multiple
    /// L1 endpoints.
    #[error("configure exactly one of --l1.http / L1_HTTP or --l1.ws / L1_WS")]
    InvalidL1EndpointConfig,
}

/// Result alias for CLI operations.
///
/// This type alias simplifies function signatures throughout the CLI binary
/// by using [`CliError`] as the default error type.
pub type Result<T> = std::result::Result<T, CliError>;
