use std::path::Path;

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

pub struct OcamlPlugin;

impl Plugin for OcamlPlugin {
    fn name(&self) -> &str {
        "ocaml"
    }

    fn description(&self) -> &str {
        "OCaml/Dune build artifacts"
    }

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

        let dune_project = dir.join("dune-project");
        if dune_project.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: "Dune build artifacts, regenerated with `dune build`".to_string(),
                });
            }

            let opam = dir.join("_opam");
            if opam.is_dir() {
                let size = utils::dir_size(&opam);
                targets.push(CleanTarget {
                    label: format!("{}", opam.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(opam),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Local opam switch, recreate with `opam switch create .`"
                        .to_string(),
                });
            }
        }

        targets
    }

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