//! ListTool — list directory contents.

use std::sync::Arc;

use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;
use sgr_agent_core::agent_tool::{Tool, ToolError, ToolOutput, parse_args};
use sgr_agent_core::context::AgentContext;
use sgr_agent_core::schema::json_schema_for;

use crate::backend::FileBackend;
use crate::helpers::backend_err;

pub struct ListTool<B: FileBackend>(pub Arc<B>);

#[derive(Deserialize, JsonSchema)]
struct ListArgs {
    /// Directory path
    path: String,
}

#[async_trait::async_trait]
impl<B: FileBackend> Tool for ListTool<B> {
    fn name(&self) -> &str { "list" }
    fn description(&self) -> &str { "List directory contents" }
    fn is_read_only(&self) -> bool { true }
    fn parameters_schema(&self) -> Value { json_schema_for::<ListArgs>() }
    async fn execute(&self, args: Value, _ctx: &mut AgentContext) -> Result<ToolOutput, ToolError> {
        let a: ListArgs = parse_args(&args)?;
        self.0.list(&a.path).await.map(ToolOutput::text).map_err(backend_err)
    }
    async fn execute_readonly(&self, args: Value, _ctx: &AgentContext) -> Result<ToolOutput, ToolError> {
        let a: ListArgs = parse_args(&args)?;
        self.0.list(&a.path).await.map(ToolOutput::text).map_err(backend_err)
    }
}
