use serde::{Deserialize, Serialize};
use std::path::Path;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum TomlConfigError {
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    #[error("toml parse error: {0}")]
    Parse(#[from] toml::de::Error),
}

#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct OpRules {
    #[serde(default)]
    pub patterns: Vec<String>,
}

#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct BashRules {
    #[serde(default)]
    pub deny: Vec<String>,
    #[serde(default)]
    pub ask: Vec<String>,
    #[serde(default)]
    pub allow_prefixes: Vec<String>,
}

#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct BotignoreToml {
    pub read: Option<OpRules>,
    pub write: Option<OpRules>,
    pub bash: Option<BashRules>,
}

impl BotignoreToml {
    /// Load `<root>/botignore.toml` if present, else return an empty config.
    pub fn load(root: &Path) -> Result<Self, TomlConfigError> {
        let path = root.join("botignore.toml");
        if !path.exists() {
            return Ok(Self::default());
        }
        let text = std::fs::read_to_string(&path)?;
        let cfg = toml::from_str(&text)?;
        Ok(cfg)
    }
}
