use std::path::Path;

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

pub struct TableplusPlugin;

impl Plugin for TableplusPlugin {
    fn name(&self) -> &str {
        "tableplus"
    }

    fn description(&self) -> &str {
        "TablePlus caches and temporary data"
    }

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

        // Main cache (Sparkle updates, Sentry, WebKit cache)
        let cache_dir = home.join("Library/Caches/com.tinyapp.TablePlus");
        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: "TablePlus cache, regenerated automatically".to_string(),
            });
        }

        // Application Support cache & temp
        let support_dir = home.join("Library/Application Support/com.tinyapp.TablePlus");
        if support_dir.is_dir() {
            let cache = support_dir.join("Cache");
            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: "TablePlus application data cache".to_string(),
                });
            }

            let temp = support_dir.join("Temp");
            if temp.is_dir() {
                let size = utils::dir_size(&temp);
                targets.push(CleanTarget {
                    label: format!("{}", temp.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(temp),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "TablePlus temporary files".to_string(),
                });
            }
        }

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

        // Sentry crash reports
        let sentry_dir = home.join("Library/Caches/SentryCrash/TablePlus");
        if sentry_dir.is_dir() {
            let size = utils::dir_size(&sentry_dir);
            targets.push(CleanTarget {
                label: format!("{}", sentry_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(sentry_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "TablePlus crash reports".to_string(),
            });
        }

        targets
    }
}
