use std::path::Path;

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

pub struct ZedPlugin;

impl Plugin for ZedPlugin {
    fn name(&self) -> &str {
        "zed"
    }

    fn description(&self) -> &str {
        "Zed editor caches, logs, and support data"
    }

    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();
        let support_dir = home.join("Library/Application Support/Zed");

        if support_dir.is_dir() {
            // Node runtime (~1.7GB)
            let node = support_dir.join("node");
            if node.is_dir() {
                let size = utils::dir_size(&node);
                targets.push(CleanTarget {
                    label: format!("{}", node.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(node),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed node runtime, re-downloaded as needed".to_string(),
                });
            }

            // Language servers
            let languages = support_dir.join("languages");
            if languages.is_dir() {
                let size = utils::dir_size(&languages);
                targets.push(CleanTarget {
                    label: format!("{}", languages.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(languages),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed language servers, re-downloaded as needed".to_string(),
                });
            }

            // Extensions (installed extensions cache)
            let extensions = support_dir.join("extensions");
            if extensions.is_dir() {
                let size = utils::dir_size(&extensions);
                targets.push(CleanTarget {
                    label: format!("{}", extensions.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(extensions),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed extensions, re-downloaded as needed".to_string(),
                });
            }

            // Copilot runtime
            let copilot = support_dir.join("copilot");
            if copilot.is_dir() {
                let size = utils::dir_size(&copilot);
                targets.push(CleanTarget {
                    label: format!("{}", copilot.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(copilot),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed Copilot runtime, re-downloaded as needed".to_string(),
                });
            }

            // Debug adapters
            let debug_adapters = support_dir.join("debug_adapters");
            if debug_adapters.is_dir() {
                let size = utils::dir_size(&debug_adapters);
                targets.push(CleanTarget {
                    label: format!("{}", debug_adapters.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(debug_adapters),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed debug adapters, re-downloaded as needed".to_string(),
                });
            }

            // Prettier cache
            let prettier = support_dir.join("prettier");
            if prettier.is_dir() {
                let size = utils::dir_size(&prettier);
                targets.push(CleanTarget {
                    label: format!("{}", prettier.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(prettier),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed Prettier cache, re-downloaded as needed".to_string(),
                });
            }

            // Hang traces (diagnostic dumps)
            let hang_traces = support_dir.join("hang_traces");
            if hang_traces.is_dir() {
                let size = utils::dir_size(&hang_traces);
                targets.push(CleanTarget {
                    label: format!("{}", hang_traces.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(hang_traces),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Zed hang trace logs".to_string(),
                });
            }
        }

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

        // Logs and crash dumps
        let log_dir = home.join("Library/Logs/Zed");
        if log_dir.is_dir() {
            let size = utils::dir_size(&log_dir);
            targets.push(CleanTarget {
                label: format!("{}", log_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(log_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Zed application logs".to_string(),
            });
        }

        targets
    }

    fn is_available(&self) -> bool {
        utils::home_dir()
            .join("Library/Application Support/Zed")
            .is_dir()
    }
}
