use std::path::Path;

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

pub struct VitePlugin;

impl Plugin for VitePlugin {
    fn name(&self) -> &str {
        "vite"
    }

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

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

        let has_vite_config = dir.join("vite.config.js").exists()
            || dir.join("vite.config.ts").exists()
            || dir.join("vite.config.mjs").exists();

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

        targets
    }

    fn markers(&self) -> &'static [&'static str] {
        &["vite.config.js", "vite.config.ts", "vite.config.mjs"]
    }
}
