use std::path::Path;

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

pub struct RubyPlugin;

impl Plugin for RubyPlugin {
    fn name(&self) -> &str {
        "ruby"
    }

    fn description(&self) -> &str {
        "Ruby bundler cache and gem artifacts"
    }

    fn scan_directory(&self, dir: &Path) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        let gemfile = dir.join("Gemfile");
        if gemfile.exists() {
            let bundle_dir = dir.join(".bundle");
            if bundle_dir.is_dir() {
                let size = utils::dir_size(&bundle_dir);
                targets.push(CleanTarget {
                    label: format!("{}", bundle_dir.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(bundle_dir),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Ruby gems, reinstall with `bundle install`".to_string(),
                });
            }
        }

        targets
    }

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

        let gem_cache = utils::home_dir().join(".gem");
        if gem_cache.is_dir() {
            let size = utils::dir_size(&gem_cache);
            targets.push(CleanTarget {
                label: format!("{}", gem_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(gem_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Ruby global gem cache, re-downloaded as needed".to_string(),
            });
        }

        targets
    }

    fn skip_subdirectories(&self) -> &'static [&'static str] {
        &[".bundle"]
    }

    fn markers(&self) -> &'static [&'static str] {
        &["Gemfile"]
    }
}
