use std::path::Path;

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

pub struct OllamaPlugin;

impl Plugin for OllamaPlugin {
    fn name(&self) -> &str {
        "ollama"
    }

    fn description(&self) -> &str {
        "Ollama downloaded model weights"
    }

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

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

        let models_dir = ollama_models_dir();
        if models_dir.is_dir() {
            let size = utils::dir_size(&models_dir);
            targets.push(CleanTarget {
                label: format!("{}", models_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(models_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: "Ollama AI model weights, re-downloadable with `ollama pull`"
                    .to_string(),
            });
        }

        targets
    }

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

fn ollama_models_dir() -> std::path::PathBuf {
    if let Ok(dir) = std::env::var("OLLAMA_MODELS") {
        return dir.into();
    }
    utils::home_dir().join(".ollama/models")
}
