use std::path::Path;

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

pub struct JupyterPlugin;

impl Plugin for JupyterPlugin {
    fn name(&self) -> &str {
        "jupyter"
    }

    fn description(&self) -> &str {
        "Jupyter notebook checkpoints and kernel data"
    }

    fn scan_directory(&self, dir: &Path) -> Vec<CleanTarget> {
        let mut targets = Vec::new();

        let checkpoints = dir.join(".ipynb_checkpoints");
        if checkpoints.is_dir() {
            let size = utils::dir_size(&checkpoints);
            targets.push(CleanTarget {
                label: format!("{}", checkpoints.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(checkpoints),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Jupyter notebook checkpoints".to_string(),
            });
        }

        targets
    }

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

        let jupyter_data = home.join(".local/share/jupyter/runtime");
        if jupyter_data.is_dir() {
            let size = utils::dir_size(&jupyter_data);
            targets.push(CleanTarget {
                label: format!("{} (runtime files)", jupyter_data.display()),
                size_bytes: size,
                action: CleanAction::DeleteContents(jupyter_data),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Jupyter runtime files, regenerated on next session".to_string(),
            });
        }

        targets
    }

    fn skip_subdirectories(&self) -> &'static [&'static str] {
        &[".ipynb_checkpoints"]
    }

    fn markers(&self) -> &'static [&'static str] {
        &[".ipynb_checkpoints"]
    }
}
