use std::path::Path;

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

pub struct Iterm2Plugin;

impl Plugin for Iterm2Plugin {
    fn name(&self) -> &str {
        "iterm2"
    }

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

    #[cfg(target_os = "macos")]
    fn is_available(&self) -> bool {
        let home = utils::home_dir();
        home.join("Library/Application Support/iTerm2").is_dir()
            || home.join("Library/Caches/com.googlecode.iterm2").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, parsers, etc.)
        let cache_dir = home.join("Library/Caches/com.googlecode.iterm2");
        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: "iTerm2 cache, regenerated automatically".to_string(),
            });
        }

        // Saved application state
        let saved_state =
            home.join("Library/Saved Application State/com.googlecode.iterm2.savedState");
        if saved_state.is_dir() {
            let size = utils::dir_size(&saved_state);
            targets.push(CleanTarget {
                label: format!("{}", saved_state.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(saved_state),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "iTerm2 saved application state".to_string(),
            });
        }

        // Saved state inside Application Support
        let support_saved = home.join("Library/Application Support/iTerm2/SavedState");
        if support_saved.is_dir() {
            let size = utils::dir_size(&support_saved);
            targets.push(CleanTarget {
                label: format!("{}", support_saved.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(support_saved),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "iTerm2 support saved state".to_string(),
            });
        }

        // Parser grammars cache
        let parsers = home.join("Library/Application Support/iTerm2/parsers");
        if parsers.is_dir() {
            let size = utils::dir_size(&parsers);
            targets.push(CleanTarget {
                label: format!("{}", parsers.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(parsers),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "iTerm2 parser grammar cache".to_string(),
            });
        }

        targets
    }
}
