use thiserror::Error;

/// Error types for all closure operations.
#[derive(Error, Debug)]
pub enum ClosureError {
    /// The vault has not been decrypted yet — run `closure auth`.
    #[error("authentication required — run `closure auth`")]
    AuthRequired,

    /// The provided password does not match the vault key.
    #[error("invalid password")]
    InvalidPassword,

    /// A provider with the given name was not found in the store.
    #[error("provider not found: {0}")]
    ProviderNotFound(String),

    /// The target agent's config file could not be located.
    #[error("agent config not found: {0}")]
    AgentConfigNotFound(String),

    /// A file or configuration payload is structurally invalid.
    #[error("validation error: {0}")]
    Validation(String),

    /// Wraps standard I/O errors from file system operations.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),

    /// Wraps ZIP archive errors.
    #[error("zip error: {0}")]
    Zip(#[from] zip::result::ZipError),

    /// Wraps JSON serialization / deserialization errors.
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),

    /// Wraps TOML parsing errors when reading Codex config.
    #[error("toml error: {0}")]
    Toml(#[from] toml::de::Error),

    /// Wraps TOML serialization errors when writing Codex config.
    #[error("toml serialize error: {0}")]
    TomlSerialize(#[from] toml::ser::Error),

    /// Generic error for crypto, git, sync, and other runtime failures.
    #[error("crypto error: {0}")]
    Crypto(String),

    /// Bidirectional sync encountered a conflict that cannot be resolved automatically.
    #[error("sync conflict: {0}")]
    SyncConflict(String),

    /// Backup or restore operation failed.
    #[error("backup error: {0}")]
    Backup(String),
}

/// Shorthand for `std::result::Result<T, ClosureError>`.
pub type Result<T> = std::result::Result<T, ClosureError>;
