use std::path::Path;

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

pub struct NxPlugin;

impl Plugin for NxPlugin {
    fn name(&self) -> &str {
        "nx"
    }

    fn description(&self) -> &str {
        "Nx monorepo cache"
    }

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

        let nx_json = dir.join("nx.json");
        if nx_json.exists() {
            let nx_cache = dir.join(".nx").join("cache");
            if nx_cache.is_dir() {
                let size = utils::dir_size(&nx_cache);
                targets.push(CleanTarget {
                    label: format!("{}", nx_cache.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(nx_cache),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Nx computation cache, regenerated on next run".to_string(),
                });
            }
        }

        targets
    }

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