use std::path::Path;

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

pub struct JestPlugin;

impl Plugin for JestPlugin {
    fn name(&self) -> &str {
        "jest"
    }

    fn description(&self) -> &str {
        "Jest test coverage reports"
    }

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

        // Only target coverage/ in projects that have Jest config
        let has_jest = dir.join("jest.config.js").exists()
            || dir.join("jest.config.ts").exists()
            || dir.join("jest.config.mjs").exists();

        if has_jest {
            let coverage = dir.join("coverage");
            if coverage.is_dir() {
                let size = utils::dir_size(&coverage);
                targets.push(CleanTarget {
                    label: format!("{}", coverage.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(coverage),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Jest coverage report, regenerated with `jest --coverage`"
                        .to_string(),
                });
            }
        }

        targets
    }

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