use std::path::Path;

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

pub struct BravePlugin;

impl Plugin for BravePlugin {
    fn name(&self) -> &str {
        "brave"
    }

    fn description(&self) -> &str {
        "Brave browser caches and data"
    }

    fn scan_directory(&self, _dir: &Path) -> Vec<CleanTarget> {
        Vec::new()
    }

    fn global_targets(&self) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        // Browser cache
        let cache_dir = brave_cache_dir();
        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: "Browser cache, regenerated automatically".to_string(),
            });
        }

        let support_dir = brave_support_dir();
        if support_dir.is_dir() {
            // Service Worker cache
            let sw_cache = support_dir.join("Default/Service Worker/CacheStorage");
            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: "Service Worker cache, regenerated automatically".to_string(),
                });
            }

            // GPU shader cache
            let shader_cache = support_dir.join("ShaderCache");
            if shader_cache.is_dir() {
                let size = utils::dir_size(&shader_cache);
                targets.push(CleanTarget {
                    label: format!("{}", shader_cache.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(shader_cache),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "GPU shader cache, regenerated automatically".to_string(),
                });
            }

            // GrShaderCache
            let gr_cache = support_dir.join("GrShaderCache");
            if gr_cache.is_dir() {
                let size = utils::dir_size(&gr_cache);
                targets.push(CleanTarget {
                    label: format!("{}", gr_cache.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(gr_cache),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "GPU shader cache, regenerated automatically".to_string(),
                });
            }

            // Code Cache (compiled JS bytecode)
            let code_cache = support_dir.join("Default/Code Cache");
            if code_cache.is_dir() {
                let size = utils::dir_size(&code_cache);
                targets.push(CleanTarget {
                    label: format!("{}", code_cache.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(code_cache),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "JavaScript bytecode cache, regenerated automatically".to_string(),
                });
            }

            // Crash reports
            let crash_reports = support_dir.join("Crashpad/completed");
            if crash_reports.is_dir() {
                let size = utils::dir_size(&crash_reports);
                targets.push(CleanTarget {
                    label: format!("{}", crash_reports.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(crash_reports),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Crash report data".to_string(),
                });
            }
        }

        targets
    }

    fn is_available(&self) -> bool {
        brave_support_dir().is_dir()
    }
}

fn brave_support_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Application Support/BraveSoftware/Brave-Browser")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".config/BraveSoftware/Brave-Browser")
    } else {
        utils::home_dir().join("AppData/Local/BraveSoftware/Brave-Browser/User Data")
    }
}

fn brave_cache_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Caches/BraveSoftware/Brave-Browser")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".cache/BraveSoftware/Brave-Browser")
    } else {
        brave_support_dir().join("Cache")
    }
}
