//! Error types for HeroScript parsing and execution.

use thiserror::Error;

/// Errors that can occur when parsing or accessing parameters.
#[derive(Error, Debug, Clone, PartialEq)]
pub enum ParamsError {
    /// A required key was not found in the parameters.
    #[error("Key not found: {0}")]
    KeyNotFound(String),

    /// A value could not be converted to the expected type.
    #[error("Invalid value for key '{key}': expected {expected}, got '{value}'")]
    InvalidValue {
        key: String,
        expected: String,
        value: String,
    },

    /// An error occurred during parsing.
    #[error("Parse error at position {position}: {message}")]
    ParseError { position: usize, message: String },

    /// An invalid character was found in a key name.
    #[error("Invalid character in key: '{0}'")]
    InvalidKeyChar(char),
}

/// Errors that can occur when working with HeroScript and PlayBooks.
#[derive(Error, Debug)]
pub enum HeroScriptError {
    /// A parameter-related error occurred.
    #[error("Params error: {0}")]
    Params(#[from] ParamsError),

    /// The requested action was not found.
    #[error("Action not found: {0}")]
    ActionNotFound(String),

    /// Multiple actions matched when only one was expected.
    #[error("Multiple actions found for filter: {0}")]
    MultipleActions(String),

    /// The action format is invalid.
    #[error("Invalid action format: {0}")]
    InvalidAction(String),

    /// The priority value is out of valid range.
    #[error("Priority must be 0-49, got: {0}")]
    InvalidPriority(i32),

    /// An I/O error occurred.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Some actions were not processed.
    #[error("Actions not processed: {0}")]
    UnprocessedActions(String),
}

/// A Result type alias using HeroScriptError.
pub type Result<T> = std::result::Result<T, HeroScriptError>;

/// A Result type alias using ParamsError.
pub type ParamsResult<T> = std::result::Result<T, ParamsError>;
