use crate::plugin::{CleanAction, CleanTarget, Plugin, Safety};
use crate::utils;
use std::path::Path;

pub struct RyePlugin;

impl Plugin for RyePlugin {
    fn name(&self) -> &str {
        "rye"
    }

    fn description(&self) -> &str {
        "Rye Python manager toolchains and cache"
    }

    fn scan_directory(&self, _dir: &Path) -> Vec<CleanTarget> {
        Vec::new()
    }

    fn global_targets(&self) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        let rye_home = utils::home_dir().join(".rye");
        if !rye_home.is_dir() {
            return targets;
        }

        // Python toolchain downloads
        let toolchains = rye_home.join("py");
        if toolchains.is_dir() {
            let size = utils::dir_size(&toolchains);
            targets.push(CleanTarget {
                label: format!("{}", toolchains.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(toolchains),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Rye Python toolchains, re-downloaded with `rye fetch`".to_string(),
            });
        }

        // Pip cache inside rye
        let cache = rye_home.join("cache");
        if cache.is_dir() {
            let size = utils::dir_size(&cache);
            targets.push(CleanTarget {
                label: format!("{}", cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Rye pip cache, re-downloaded as needed".to_string(),
            });
        }

        targets
    }
}
