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

pub struct DiscordPlugin;

impl Plugin for DiscordPlugin {
    fn name(&self) -> &str {
        "discord"
    }

    fn description(&self) -> &str {
        "Discord app caches"
    }

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

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

        let home = utils::home_dir();

        // macOS
        let mac_base = home
            .join("Library")
            .join("Application Support")
            .join("discord");
        // Linux
        let linux_base = home.join(".config").join("discord");

        for base in [mac_base, linux_base] {
            if !base.is_dir() {
                continue;
            }
            for name in ["Cache", "Code Cache", "GPUCache"] {
                let cache = base.join(name);
                if cache.is_dir() {
                    let size = utils::dir_size(&cache);
                    targets.push(CleanTarget {
                        label: format!("{}", cache.display()),
                        size_bytes: size,
                        action: CleanAction::DeletePath(cache),
                        plugin_name: self.name().to_string(),
                        safety: Safety::Recoverable,
                        explanation: "Discord cache, regenerated automatically".to_string(),
                    });
                }
            }
        }

        targets
    }
}
