use std::path::Path;

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

pub struct ElixirPlugin;

impl Plugin for ElixirPlugin {
    fn name(&self) -> &str {
        "elixir"
    }

    fn description(&self) -> &str {
        "Elixir/Mix build artifacts and dependencies"
    }

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

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

            let deps = dir.join("deps");
            if deps.is_dir() {
                let size = utils::dir_size(&deps);
                targets.push(CleanTarget {
                    label: format!("{}", deps.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(deps),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Elixir dependencies, reinstall with `mix deps.get`".to_string(),
                });
            }
        }

        targets
    }

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