use std::path::Path;

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

pub struct SlackPlugin;

impl Plugin for SlackPlugin {
    fn name(&self) -> &str {
        "slack"
    }

    fn description(&self) -> &str {
        "Slack caches, logs, and updater"
    }

    #[cfg(target_os = "macos")]
    fn is_available(&self) -> bool {
        let home = utils::home_dir();
        home.join("Library/Caches/com.tinyspeck.slackmacgap")
            .is_dir()
            || home
                .join("Library/Caches/com.tinyspeck.slackmacgap.ShipIt")
                .is_dir()
            || home
                .join("Library/Application Support/Slack/Service Worker")
                .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/com.tinyspeck.slackmacgap");
        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: "Slack application cache, regenerated automatically".to_string(),
            });
        }

        // ShipIt (Squirrel updater)
        let shipit = home.join("Library/Caches/com.tinyspeck.slackmacgap.ShipIt");
        if shipit.is_dir() {
            let size = utils::dir_size(&shipit);
            targets.push(CleanTarget {
                label: format!("{}", shipit.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(shipit),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Slack auto-updater staging area".to_string(),
            });
        }

        // Service Worker cache
        let sw = home.join("Library/Application Support/Slack/Service Worker");
        if sw.is_dir() {
            let size = utils::dir_size(&sw);
            targets.push(CleanTarget {
                label: format!("{}", sw.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(sw),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Slack Service Worker cache, regenerated automatically".to_string(),
            });
        }

        // GPU cache
        let gpu = home.join("Library/Application Support/Slack/GPUCache");
        if gpu.is_dir() {
            let size = utils::dir_size(&gpu);
            targets.push(CleanTarget {
                label: format!("{}", gpu.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(gpu),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Slack GPU cache, regenerated automatically".to_string(),
            });
        }

        // Logs
        let logs = home.join("Library/Logs/Slack");
        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: "Slack application logs".to_string(),
            });
        }

        targets
    }
}
