use std::path::Path;

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

pub struct NuxtPlugin;

impl Plugin for NuxtPlugin {
    fn name(&self) -> &str {
        "nuxt"
    }

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

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

        let has_nuxt_config =
            dir.join("nuxt.config.ts").exists() || dir.join("nuxt.config.js").exists();

        if has_nuxt_config {
            for (name, explanation) in [
                (".nuxt", "Nuxt build cache, regenerated with `nuxt build`"),
                (
                    ".output",
                    "Nuxt build output, regenerated with `nuxt 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] {
        &["nuxt.config.ts", "nuxt.config.js"]
    }
}
