use std::path::Path;

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

pub struct CypressPlugin;

impl Plugin for CypressPlugin {
    fn name(&self) -> &str {
        "cypress"
    }

    fn description(&self) -> &str {
        "Cypress browser binaries cache"
    }

    fn scan_directory(&self, _dir: &Path) -> Vec<CleanTarget> {
        Vec::new()
    }

    fn global_targets(&self) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        let cache_dir = cypress_dir();
        if cache_dir.is_dir() {
            let size = utils::dir_size(&cache_dir);
            targets.push(CleanTarget {
                label: format!("{}", cache_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(cache_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Cypress browser binaries, reinstall with `npx cypress install`"
                    .to_string(),
            });
        }

        targets
    }

    fn is_available(&self) -> bool {
        cypress_dir().is_dir()
    }
}

fn cypress_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Caches/Cypress")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".cache/Cypress")
    } else {
        utils::home_dir().join("AppData/Local/Cypress/Cache")
    }
}
