use std::path::Path;

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

pub struct FloxPlugin;

impl Plugin for FloxPlugin {
    fn name(&self) -> &str {
        "flox"
    }

    fn description(&self) -> &str {
        "Flox environment caches and state"
    }

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

        // Flox cache (metrics, process locks, etc.)
        let cache_dir = home.join(".cache/flox");
        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: "Flox cache, regenerated automatically".to_string(),
            });
        }

        // Flox state
        let state_dir = home.join(".local/state/flox");
        if state_dir.is_dir() {
            let size = utils::dir_size(&state_dir);
            targets.push(CleanTarget {
                label: format!("{}", state_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(state_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Flox state data, regenerated automatically".to_string(),
            });
        }

        // Flox data (environment metadata, pulled environments)
        let data_dir = home.join(".local/share/flox");
        if data_dir.is_dir() {
            let size = utils::dir_size(&data_dir);
            targets.push(CleanTarget {
                label: format!("{}", data_dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(data_dir),
                plugin_name: self.name().to_string(),
                safety: Safety::Unsafe,
                explanation: "Flox environment data".to_string(),
            });
        }

        targets
    }

    fn is_available(&self) -> bool {
        let home = utils::home_dir();
        home.join(".cache/flox").is_dir() || home.join(".local/share/flox").is_dir()
    }
}
