blazy 0.2.4

Unified, AI-safe meta-package manager for 26 OS and language package managers
use crate::providers::exec::find_executable;
use serde::Serialize;

#[derive(Debug, Clone, Serialize)]
pub struct BootstrapPlan {
    pub manager: String,
    pub already_installed: bool,
    pub existing_path: Option<String>,
    pub command: String,
    pub args: Vec<String>,
    pub notes: Vec<String>,
}

impl BootstrapPlan {
    pub fn command_line(&self) -> String {
        if self.args.is_empty() {
            self.command.clone()
        } else {
            format!("{} {}", self.command, self.args.join(" "))
        }
    }
}

pub fn plan_bootstrap(manager: &str) -> Result<BootstrapPlan, String> {
    let key = manager.trim().to_ascii_lowercase();
    let bin = match key.as_str() {
        "rustup" | "cargo" => "rustup",
        "uv" => "uv",
        "bun" => "bun",
        "pnpm" => "pnpm",
        "scoop" => "scoop",
        "brew" | "homebrew" => "brew",
        other => {
            return Err(format!(
                "bootstrap not supported for '{other}' (supported: scoop, uv, bun, pnpm, rustup, brew)"
            ));
        }
    };

    if let Some(existing) = find_executable(&[bin]) {
        return Ok(BootstrapPlan {
            manager: key,
            already_installed: true,
            existing_path: Some(existing),
            command: String::new(),
            args: Vec::new(),
            notes: vec!["Already installed on PATH".to_string()],
        });
    }

    let (command, args, notes) = if cfg!(windows) {
        match key.as_str() {
            "scoop" => (
                "powershell.exe".to_string(),
                vec![
                    "-NoProfile".into(),
                    "-Command".into(),
                    "irm get.scoop.sh | iex".into(),
                ],
                vec!["Official Scoop installer (user-space, no admin required)".to_string()],
            ),
            "uv" => (
                "powershell.exe".to_string(),
                vec![
                    "-ExecutionPolicy".into(),
                    "ByPass".into(),
                    "-c".into(),
                    "irm https://astral.sh/uv/install.ps1 | iex".into(),
                ],
                vec!["Official Astral uv installer".to_string()],
            ),
            "bun" => (
                "powershell.exe".to_string(),
                vec!["-c".into(), "irm bun.sh/install.ps1 | iex".into()],
                vec!["Official Bun installer".to_string()],
            ),
            "pnpm" => (
                "powershell.exe".to_string(),
                vec![
                    "-c".into(),
                    "iwr https://get.pnpm.io/install.ps1 -useb | iex".into(),
                ],
                vec!["Official pnpm standalone installer".to_string()],
            ),
            "rustup" | "cargo" => (
                "winget".to_string(),
                vec![
                    "install".into(),
                    "--id".into(),
                    "Rustlang.Rustup".into(),
                    "--accept-package-agreements".into(),
                    "--accept-source-agreements".into(),
                ],
                vec!["Installs Rust & Cargo via winget".to_string()],
            ),
            "brew" | "homebrew" => {
                return Err("Homebrew is only supported on macOS and Linux".to_string());
            }
            _ => unreachable!(),
        }
    } else {
        match key.as_str() {
            "uv" => (
                "sh".to_string(),
                vec!["-c".into(), "curl -LsSf https://astral.sh/uv/install.sh | sh".into()],
                vec!["Official Astral uv installer".to_string()],
            ),
            "bun" => (
                "sh".to_string(),
                vec!["-c".into(), "curl -fsSL https://bun.sh/install | bash".into()],
                vec!["Official Bun installer".to_string()],
            ),
            "pnpm" => (
                "sh".to_string(),
                vec!["-c".into(), "curl -fsSL https://get.pnpm.io/install.sh | sh -".into()],
                vec!["Official pnpm installer".to_string()],
            ),
            "rustup" | "cargo" => (
                "sh".to_string(),
                vec![
                    "-c".into(),
                    "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y".into(),
                ],
                vec!["Official rustup installer".to_string()],
            ),
            "brew" | "homebrew" => (
                "/bin/bash".to_string(),
                vec![
                    "-c".into(),
                    "\"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"".into(),
                ],
                vec!["Official Homebrew installer".to_string()],
            ),
            "scoop" => return Err("Scoop is only supported on Windows".to_string()),
            _ => unreachable!(),
        }
    };

    Ok(BootstrapPlan {
        manager: key,
        already_installed: false,
        existing_path: None,
        command,
        args,
        notes,
    })
}

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

    #[test]
    fn plans_bootstrap_for_supported_managers() {
        let plan = plan_bootstrap("pnpm").unwrap();
        assert_eq!(plan.manager, "pnpm");
        assert!(plan.already_installed || !plan.command.is_empty());
        assert!(plan_bootstrap("unknown_pm").is_err());
    }
}