use std::path::PathBuf;

use async_trait::async_trait;
use serde_json::{json, Value};

use crate::skills;
use crate::traits::Tool;

pub struct UseSkillTool {
    skills_dir: PathBuf,
}

impl UseSkillTool {
    pub fn new(skills_dir: PathBuf) -> Self {
        Self { skills_dir }
    }
}

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

    fn description(&self) -> &str {
        "Activate a skill by name to get its instructions"
    }

    fn schema(&self) -> Value {
        json!({
            "name": "use_skill",
            "description": "Activate a skill by name to get its instructions. Use manage_skills list to see available skills.",
            "parameters": {
                "type": "object",
                "properties": {
                    "skill_name": {
                        "type": "string",
                        "description": "Name of the skill to activate"
                    }
                },
                "required": ["skill_name"]
            }
        })
    }

    async fn call(&self, arguments: &str) -> anyhow::Result<String> {
        let args: Value = serde_json::from_str(arguments).unwrap_or(json!({}));
        let skill_name = args["skill_name"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing required parameter: skill_name"))?;

        let all_skills = skills::load_skills(&self.skills_dir);
        if let Some(skill) = skills::find_skill_by_name(&all_skills, skill_name) {
            Ok(skill.body.clone())
        } else {
            let available: Vec<&str> = all_skills.iter().map(|s| s.name.as_str()).collect();
            Ok(format!(
                "Skill '{}' not found. Available skills: {}",
                skill_name,
                available.join(", ")
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::skills::Skill;

    fn make_skill(name: &str, body: &str) -> Skill {
        Skill {
            name: name.to_string(),
            description: format!("{} skill", name),
            triggers: vec![],
            body: body.to_string(),
            origin: None,
            source: None,
            source_url: None,
            dir_path: None,
            resources: vec![],
        }
    }

    fn write_skill(dir: &std::path::Path, skill: &Skill) {
        skills::write_skill_to_file(dir, skill).unwrap();
    }

    #[tokio::test]
    async fn found_skill_returns_body() {
        let dir = tempfile::TempDir::new().unwrap();
        write_skill(dir.path(), &make_skill("deploy", "Run deploy steps"));
        let tool = UseSkillTool::new(dir.path().to_path_buf());
        let result = tool.call(r#"{"skill_name": "deploy"}"#).await.unwrap();
        assert_eq!(result, "Run deploy steps");
    }

    #[tokio::test]
    async fn not_found_lists_available() {
        let dir = tempfile::TempDir::new().unwrap();
        write_skill(dir.path(), &make_skill("deploy", "body1"));
        write_skill(dir.path(), &make_skill("lint", "body2"));
        let tool = UseSkillTool::new(dir.path().to_path_buf());
        let result = tool.call(r#"{"skill_name": "missing"}"#).await.unwrap();
        assert!(result.contains("not found"));
        assert!(result.contains("deploy"));
        assert!(result.contains("lint"));
    }

    #[tokio::test]
    async fn empty_skills() {
        let dir = tempfile::TempDir::new().unwrap();
        let tool = UseSkillTool::new(dir.path().to_path_buf());
        let result = tool.call(r#"{"skill_name": "any"}"#).await.unwrap();
        assert!(result.contains("not found"));
    }
}
