use std::path::Path;

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

pub struct ZoomPlugin;

impl Plugin for ZoomPlugin {
    fn name(&self) -> &str {
        "zoom"
    }

    fn description(&self) -> &str {
        "Zoom caches and logs"
    }

    #[cfg(target_os = "macos")]
    fn is_available(&self) -> bool {
        let home = utils::home_dir();
        home.join("Library/Caches/us.zoom.xos").is_dir()
            || home.join("Library/Application Support/zoom.us").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();

        // App cache
        let app_cache = home.join("Library/Caches/us.zoom.xos");
        if app_cache.is_dir() {
            let size = utils::dir_size(&app_cache);
            targets.push(CleanTarget {
                label: format!("{}", app_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(app_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Zoom application cache, regenerated automatically".to_string(),
            });
        }

        // Updater cache
        let updater = home.join("Library/Caches/us.zoom.updater");
        if updater.is_dir() {
            let size = utils::dir_size(&updater);
            targets.push(CleanTarget {
                label: format!("{}", updater.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(updater),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Zoom updater cache".to_string(),
            });
        }

        // Logs
        let logs = home.join("Library/Logs/zoom.us");
        if logs.is_dir() {
            let size = utils::dir_size(&logs);
            targets.push(CleanTarget {
                label: format!("{}", logs.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(logs),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Zoom application logs".to_string(),
            });
        }

        // WebKit data
        let webkit = home.join("Library/WebKit/us.zoom.xos");
        if webkit.is_dir() {
            let size = utils::dir_size(&webkit);
            targets.push(CleanTarget {
                label: format!("{}", webkit.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(webkit),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Zoom WebKit cache, regenerated automatically".to_string(),
            });
        }

        targets
    }
}
