use std::path::Path;

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

pub struct VagrantPlugin;

impl Plugin for VagrantPlugin {
    fn name(&self) -> &str {
        "vagrant"
    }

    fn description(&self) -> &str {
        "Vagrant VM state"
    }

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

        let vagrantfile = dir.join("Vagrantfile");
        if vagrantfile.exists() {
            let vagrant_dir = dir.join(".vagrant");
            if vagrant_dir.is_dir() {
                let size = utils::dir_size(&vagrant_dir);
                targets.push(CleanTarget {
                    label: format!("{}", vagrant_dir.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(vagrant_dir),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Unsafe,
                    explanation:
                        "Vagrant VM state and disk images, requires `vagrant up` to recreate"
                            .to_string(),
                });
            }
        }

        targets
    }

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