use std::path::Path;

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

pub struct LinearPlugin;

impl Plugin for LinearPlugin {
    fn name(&self) -> &str {
        "linear"
    }

    fn description(&self) -> &str {
        "Linear desktop app caches and updater"
    }

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

        // Updater cache (old app versions)
        let updater = home.join("Library/Caches/@lineardesktop-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: "Linear updater cache".to_string(),
            });
        }

        // App cache
        let app_cache = home.join("Library/Caches/com.linear");
        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: "Linear application cache, regenerated automatically".to_string(),
            });
        }

        // ShipIt (Squirrel updater)
        let shipit = home.join("Library/Caches/com.linear.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: "Linear auto-updater staging area".to_string(),
            });
        }

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

        targets
    }
}
