use std::path::Path;

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

pub struct SafariPlugin;

impl Plugin for SafariPlugin {
    fn name(&self) -> &str {
        "safari"
    }

    fn description(&self) -> &str {
        "Safari caches and website data"
    }

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

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

        // Safari cache
        let cache_dir = home.join("Library/Caches/com.apple.Safari");
        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: "Safari browser cache, regenerated automatically".to_string(),
            });
        }

        // Safari webpage previews / thumbnails
        let thumbnails = home.join("Library/Caches/com.apple.Safari/Webpage Previews");
        if thumbnails.is_dir() {
            let size = utils::dir_size(&thumbnails);
            targets.push(CleanTarget {
                label: format!("{}", thumbnails.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(thumbnails),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Safari webpage preview thumbnails".to_string(),
            });
        }

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

        // WebKit caches (shared between Safari and WebKit-based apps)
        let webkit_cache = home.join("Library/Caches/com.apple.WebKit.WebContent");
        if webkit_cache.is_dir() {
            let size = utils::dir_size(&webkit_cache);
            targets.push(CleanTarget {
                label: format!("{}", webkit_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(webkit_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "WebKit cache, regenerated automatically".to_string(),
            });
        }

        // Safari website data (LocalStorage, IndexedDB, etc.)
        let website_data = home.join("Library/Safari/LocalStorage");
        if website_data.is_dir() {
            let size = utils::dir_size(&website_data);
            targets.push(CleanTarget {
                label: format!("{}", website_data.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(website_data),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: "Safari website local storage data".to_string(),
            });
        }

        // Safari databases
        let databases = home.join("Library/Safari/Databases");
        if databases.is_dir() {
            let size = utils::dir_size(&databases);
            targets.push(CleanTarget {
                label: format!("{}", databases.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(databases),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: "Safari website database storage".to_string(),
            });
        }

        targets
    }
}
