use std::path::Path;

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

pub struct ZigPlugin;

impl Plugin for ZigPlugin {
    fn name(&self) -> &str {
        "zig"
    }

    fn description(&self) -> &str {
        "Zig build cache and output"
    }

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

        let build_zig = dir.join("build.zig");
        if build_zig.exists() {
            for (name, explanation) in [
                ("zig-cache", "Zig build cache, regenerated with `zig build`"),
                ("zig-out", "Zig build output, regenerated with `zig build`"),
                (
                    ".zig-cache",
                    "Zig build cache, regenerated with `zig build`",
                ),
            ] {
                let path = dir.join(name);
                if path.is_dir() {
                    let size = utils::dir_size(&path);
                    targets.push(CleanTarget {
                        label: format!("{}", path.display()),
                        size_bytes: size,
                        action: CleanAction::DeletePath(path),
                        plugin_name: self.name().to_string(),
                        safety: Safety::Recoverable,
                        explanation: explanation.to_string(),
                    });
                }
            }
        }

        targets
    }

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