use crate::storage::StorageError;

/// 任务错误类型
#[derive(Debug, thiserror::Error)]
pub enum TaskError {
    #[error("Task session not found: {0}")]
    SessionNotFound(String),

    #[error("Invalid parent session")]
    InvalidParentSession,

    #[error("Failed to create subagent: {0}")]
    AgentCreationFailed(String),

    #[error("Execution failed: {0}")]
    ExecutionFailed(String),

    #[error("Task execution timed out")]
    Timeout,

    #[error("Repository error: {0}")]
    RepositoryError(#[from] StorageError),

    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),

    #[error("Missing required context: {0}")]
    MissingContext(String),

    #[error("Invalid arguments: {0}")]
    InvalidArguments(String),
}

impl TaskError {
    pub fn as_status(&self) -> &'static str {
        match self {
            Self::Timeout => "timeout",
            Self::SessionNotFound(_) => "failed",
            Self::InvalidParentSession => "failed",
            Self::AgentCreationFailed(_) => "failed",
            Self::ExecutionFailed(_) => "failed",
            Self::RepositoryError(_) => "failed",
            Self::SerializationError(_) => "failed",
            Self::MissingContext(_) => "failed",
            Self::InvalidArguments(_) => "failed",
        }
    }
}