use std::path::Path;

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

pub struct SbtPlugin;

impl Plugin for SbtPlugin {
    fn name(&self) -> &str {
        "sbt"
    }

    fn description(&self) -> &str {
        "Scala SBT build artifacts"
    }

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

        let build_sbt = dir.join("build.sbt");
        if build_sbt.exists() {
            let target = dir.join("target");
            if target.is_dir() {
                let size = utils::dir_size(&target);
                targets.push(CleanTarget {
                    label: format!("{}", target.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(target),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "SBT build output, regenerated with `sbt compile`".to_string(),
                });
            }

            let project_target = dir.join("project").join("target");
            if project_target.is_dir() {
                let size = utils::dir_size(&project_target);
                targets.push(CleanTarget {
                    label: format!("{}", project_target.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(project_target),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "SBT meta-build output, regenerated with `sbt compile`"
                        .to_string(),
                });
            }
        }

        targets
    }

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