use crate::invocation::{SkillContext, SkillInput};
use crate::outcome::SkillOutcome;
use crate::registry::SkillRegistry;
use anyhow::Result;

pub struct SkillCoordinator<'a> {
    registry: &'a SkillRegistry,
}

impl<'a> SkillCoordinator<'a> {
    pub fn new(registry: &'a SkillRegistry) -> Self {
        Self { registry }
    }

    pub fn execute_one(
        &self,
        skill_id: &str,
        input: SkillInput,
        ctx: &SkillContext,
    ) -> Result<SkillOutcome> {
        let skill = self
            .registry
            .get(skill_id)
            .ok_or_else(|| anyhow::anyhow!("skill not found: {skill_id}"))?;
        skill.execute(input, ctx)
    }

    pub fn execute_batch(
        &self,
        requests: Vec<(String, SkillInput)>,
        ctx: &SkillContext,
    ) -> Vec<Result<SkillOutcome>> {
        requests
            .into_iter()
            .map(|(id, input)| self.execute_one(&id, input, ctx))
            .collect()
    }
}
