use std::path::Path;

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

pub struct CmakePlugin;

impl Plugin for CmakePlugin {
    fn name(&self) -> &str {
        "cmake"
    }

    fn description(&self) -> &str {
        "CMake build directories"
    }

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

        let cmakelists = dir.join("CMakeLists.txt");
        if cmakelists.exists() {
            let build_dir = dir.join("build");
            if build_dir.is_dir() {
                let size = utils::dir_size(&build_dir);
                targets.push(CleanTarget {
                    label: format!("{}", build_dir.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(build_dir),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "CMake build artifacts, recreate with `cmake --build`".to_string(),
                });
            }
        }

        targets
    }

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

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