use crate::colors::get_colors;
use crate::config::get_config;
use crate::utils::{get_installed_packages, prepare_packages};

/// Lists installed `SBo` packages. If `filter` is provided, only packages
/// whose names contain any of the filter strings are shown.
pub fn list(filter: &[String]) {
    let filter = prepare_packages(filter);
    let installed = get_installed_packages();

    let cfg = get_config();
    let color = get_colors(cfg.colors);

    if filter.is_empty() {
        println!(
            "{}Installed SBo packages ({}{}{}):\n",
            color.bold,
            color.yellow,
            installed.len(),
            color.reset
        );

        for pkg in installed {
            println!("  {pkg}");
        }
    } else {
        let matches: Vec<&String> = installed
            .iter()
            .filter(|p| filter.iter().any(|f| p.contains(f)))
            .collect();

        println!(
            "Found installed SBo packages ({}{}{}):\n",
            color.yellow,
            matches.len(),
            color.reset
        );

        for pkg in &matches {
            println!("  {pkg}");
        }
    }
    println!();
}
