use std::path::Path;

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

pub struct LogsPlugin;

impl Plugin for LogsPlugin {
    fn name(&self) -> &str {
        "logs"
    }

    fn description(&self) -> &str {
        "System and application log files"
    }

    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 user_logs = home.join("Library/Logs");
        if user_logs.is_dir() {
            let size = utils::dir_size(&user_logs);
            targets.push(CleanTarget {
                label: format!("{} (contents)", user_logs.display()),
                size_bytes: size,
                action: CleanAction::DeleteContents(user_logs),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: String::new(),
            });
        }

        // On macOS, /var/log is a symlink to /private/var/log — only list the canonical path.
        let var_log = Path::new("/var/log");
        if var_log.is_dir() {
            let canonical = var_log
                .canonicalize()
                .unwrap_or_else(|_| var_log.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(),
            });
        }

        targets
    }
}
