use async_trait::async_trait;

use super::{Tool, ToolResult};
use crate::gateway::reload::ReloadHandle;

pub struct ReloadConfigTool {
    reload: ReloadHandle,
}

impl ReloadConfigTool {
    pub fn new(reload: ReloadHandle) -> Self {
        Self { reload }
    }
}

#[async_trait]
impl Tool for ReloadConfigTool {
    fn name(&self) -> &str {
        "reload_config"
    }

    fn description(&self) -> &str {
        "重新读取并校验 PicoBot 配置，然后让 Gateway 优雅切换到新配置。仅在用户明确要求重新加载配置时调用。"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {},
            "additionalProperties": false
        })
    }

    async fn execute(&self, _args: serde_json::Value) -> anyhow::Result<ToolResult> {
        match self.reload.request().await {
            Ok(accepted) => Ok(ToolResult {
                success: true,
                output: accepted.message,
                error: None,
            }),
            Err(error) => Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some(error.to_string()),
            }),
        }
    }

    fn exclusive(&self) -> bool {
        true
    }
}
