use std::path::Path;

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

pub struct PythonPlugin;

impl Plugin for PythonPlugin {
    fn name(&self) -> &str {
        "python"
    }

    fn description(&self) -> &str {
        "Python virtual environments, __pycache__, and pip cache"
    }

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

        // Detect virtual environments by pyvenv.cfg
        let pyvenv_cfg = dir.join("pyvenv.cfg");
        if pyvenv_cfg.exists() {
            let size = utils::dir_size(dir);
            targets.push(CleanTarget {
                label: format!("{} (venv)", dir.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(dir.to_path_buf()),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Python virtual environment, recreate with `python -m venv`"
                    .to_string(),
            });
            return targets;
        }

        // Detect __pycache__ directories
        let pycache = dir.join("__pycache__");
        if pycache.is_dir() {
            let size = utils::dir_size(&pycache);
            targets.push(CleanTarget {
                label: format!("{}", pycache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(pycache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Python bytecode cache, regenerated automatically".to_string(),
            });
        }

        // Detect .venv directories in project roots
        if dir.join("requirements.txt").exists()
            || dir.join("pyproject.toml").exists()
            || dir.join("setup.py").exists()
        {
            let venv_dir = dir.join(".venv");
            if venv_dir.is_dir() {
                let size = utils::dir_size(&venv_dir);
                targets.push(CleanTarget {
                    label: format!("{}", venv_dir.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(venv_dir),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Python virtual environment, recreate with `python -m venv`"
                        .to_string(),
                });
            }
        }

        targets
    }

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

        let pip_cache = utils::home_dir().join(".cache/pip");
        if pip_cache.is_dir() {
            let size = utils::dir_size(&pip_cache);
            targets.push(CleanTarget {
                label: format!("{}", pip_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(pip_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "pip download cache, re-downloaded as needed".to_string(),
            });
        }

        targets
    }

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

    fn markers(&self) -> &'static [&'static str] {
        &[
            "pyvenv.cfg",
            "__pycache__",
            "requirements.txt",
            "pyproject.toml",
            "setup.py",
        ]
    }
}
