use crate::colors::get_colors;
use crate::config::get_config;
use serde::Deserialize;
use std::fs;
use std::process::exit;
use toml::from_str;

#[derive(Deserialize, Debug)]
struct Blacklist {
    #[serde(default)]
    packages: Vec<String>,
}

/// Loads the blacklist from `/etc/sbpkg/blacklist.toml` and returns the package patterns.
///
/// Returns an empty `Vec` if the file does not exist.
///
/// # Exits
///
/// Exits with code 1 if the file exists but cannot be parsed.
pub fn get_blacklist() -> Vec<String> {
    let cfg = get_config();
    let color = get_colors(cfg.colors);

    let Ok(content) = fs::read_to_string(cfg.blacklist_file) else {
        return Vec::new();
    };

    let blk: Blacklist = from_str(&content).unwrap_or_else(|e| {
        eprintln!(
            "{}Error:{} Incorrect format in blacklist.toml: {}",
            color.red, color.reset, e
        );
        exit(1);
    });
    blk.packages
}
