use std::path::Path;

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

pub struct ProtonPlugin;

impl Plugin for ProtonPlugin {
    fn name(&self) -> &str {
        "proton"
    }

    fn description(&self) -> &str {
        "Proton Drive and VPN caches"
    }

    #[cfg(target_os = "macos")]
    fn is_available(&self) -> bool {
        let home = utils::home_dir();
        home.join("Library/Caches/ch.protonmail.drive").is_dir()
            || home.join("Library/Caches/ch.protonvpn.mac").is_dir()
    }

    #[cfg(not(target_os = "macos"))]
    fn is_available(&self) -> bool {
        false
    }

    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();

        // Proton Drive cache
        let drive_cache = home.join("Library/Caches/ch.protonmail.drive");
        if drive_cache.is_dir() {
            let size = utils::dir_size(&drive_cache);
            targets.push(CleanTarget {
                label: format!("{}", drive_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(drive_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Proton Drive cache, regenerated automatically".to_string(),
            });
        }

        // Proton VPN cache
        let vpn_cache = home.join("Library/Caches/ch.protonvpn.mac");
        if vpn_cache.is_dir() {
            let size = utils::dir_size(&vpn_cache);
            targets.push(CleanTarget {
                label: format!("{}", vpn_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(vpn_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Proton VPN cache, regenerated automatically".to_string(),
            });
        }

        targets
    }
}
