//! Error types for the consensus library.
//!
//! All fallible operations return [`ConsensusError`]. Variants are grouped into
//! configuration validation, vote/proposal validation, session state, and
//! consensus result categories.

use crate::signing::ConsensusSchemeError;

/// Enumerates everything that can go wrong during consensus operations.
#[derive(Debug, thiserror::Error)]
pub enum ConsensusError {
    // Configuration Validation Errors
    #[error("consensus_threshold must be between 0.0 and 1.0")]
    InvalidConsensusThreshold,
    #[error("timeout must be greater than 0")]
    InvalidTimeout,
    #[error("expected_voters_count must be greater than 0")]
    InvalidExpectedVotersCount,
    #[error("max_rounds must be greater than 0")]
    InvalidMaxRounds,

    // Vote and Proposal Validation Errors
    #[error("Invalid vote signature")]
    InvalidVoteSignature,
    #[error("Empty signature")]
    EmptySignature,
    #[error("Duplicate vote")]
    DuplicateVote,
    #[error("User already voted")]
    UserAlreadyVoted,
    #[error("Vote expired")]
    VoteExpired,
    #[error("Empty vote owner")]
    EmptyVoteOwner,
    #[error("Invalid vote hash")]
    InvalidVoteHash,
    #[error("Empty vote hash")]
    EmptyVoteHash,
    #[error("Proposal expired")]
    ProposalExpired,
    #[error("Vote proposal_id mismatch: vote belongs to different proposal")]
    VoteProposalIdMismatch,
    #[error("Received hash mismatch")]
    ReceivedHashMismatch,
    #[error("Parent hash mismatch")]
    ParentHashMismatch,
    #[error("Invalid vote timestamp")]
    InvalidVoteTimestamp,
    #[error("Vote timestamp is older than creation time")]
    TimestampOlderThanCreationTime,

    // Session/State Errors
    #[error("Session not active")]
    SessionNotActive,
    #[error("Session not found")]
    SessionNotFound,
    #[error("Proposal already exist in consensus service")]
    ProposalAlreadyExist,
    #[error("Scope not found")]
    ScopeNotFound,

    // Consensus Result Errors
    #[error("Insufficient votes at timeout")]
    InsufficientVotesAtTimeout,
    #[error("Consensus exceeded configured max rounds")]
    MaxRoundsExceeded,
    #[error("Consensus not reached")]
    ConsensusNotReached,
    #[error("Consensus failed")]
    ConsensusFailed,

    #[error("Signature scheme failure: {0}")]
    SignatureScheme(#[from] ConsensusSchemeError),
    #[error("Failed to get current time")]
    FailedToGetCurrentTime(#[from] std::time::SystemTimeError),
}
