use crate::{
    invocation::{SkillContext, SkillInput},
    outcome::SkillOutcome,
    skill::{Skill, SkillId},
};
use serde_json::json;

pub struct AlchemistSkill;

impl Skill for AlchemistSkill {
    fn id(&self) -> SkillId {
        SkillId("alchemist")
    }
    fn display_name(&self) -> &str {
        "Alchemist"
    }

    fn input_schema(&self) -> serde_json::Value {
        json!({ "type": "object", "properties": { "content": { "type": "string" }, "domain_hint": { "type": "string" }, "extraction_mode": { "type": "string", "enum": ["pattern", "hybrid"] } }, "required": ["content"] })
    }

    fn output_schema(&self) -> serde_json::Value {
        json!({ "type": "object", "properties": { "extracted": { "type": "object" }, "confidence": { "type": "number" } } })
    }

    fn execute(&self, input: SkillInput, _ctx: &SkillContext) -> anyhow::Result<SkillOutcome> {
        let content = input.get_str("content").unwrap_or("");
        let domain = input.get_str("domain_hint").unwrap_or("freetext");

        // Minimal pattern extraction based on domain hint
        let extracted = match domain {
            "log" => {
                let errors: Vec<&str> = content
                    .lines()
                    .filter(|l| l.contains("ERROR") || l.contains("error:"))
                    .collect();
                json!({ "errors": errors, "error_count": errors.len() })
            }
            "json" => serde_json::from_str(content).unwrap_or(json!({ "raw": content })),
            _ => json!({ "text": content }),
        };

        Ok(SkillOutcome::success(
            "alchemist",
            json!({ "extracted": extracted, "confidence": 0.7, "extraction_method_used": "pattern" }),
        ))
    }
}
