use std::path::Path;

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

pub struct TmpPlugin;

impl Plugin for TmpPlugin {
    fn name(&self) -> &str {
        "tmp"
    }

    fn description(&self) -> &str {
        "Temporary files and directories"
    }

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

        // On macOS, /tmp is a symlink to /private/tmp — only list the canonical path.
        let tmp = Path::new("/tmp");
        if tmp.is_dir() {
            let canonical = tmp.canonicalize().unwrap_or_else(|_| tmp.to_path_buf());
            let size = utils::dir_size(&canonical);
            targets.push(CleanTarget {
                label: format!("{} (contents)", canonical.display()),
                size_bytes: size,
                action: CleanAction::DeleteContents(canonical),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: String::new(),
            });
        }

        let caches = home.join("Library/Caches");
        if caches.is_dir() {
            let size = utils::dir_size(&caches);
            targets.push(CleanTarget {
                label: format!("{} (contents)", caches.display()),
                size_bytes: size,
                action: CleanAction::DeleteContents(caches),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: String::new(),
            });
        }

        targets
    }
}
