use std::path::Path;

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

pub struct NextPlugin;

impl Plugin for NextPlugin {
    fn name(&self) -> &str {
        "next"
    }

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

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

        let has_next_config = dir.join("next.config.js").exists()
            || dir.join("next.config.mjs").exists()
            || dir.join("next.config.ts").exists();

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

        targets
    }

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