use std::path::Path;

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

pub struct UnityPlugin;

impl Plugin for UnityPlugin {
    fn name(&self) -> &str {
        "unity"
    }

    fn description(&self) -> &str {
        "Unity project build artifacts and caches"
    }

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

        let project_settings = dir.join("ProjectSettings");
        if project_settings.is_dir() {
            for (name, explanation) in [
                ("Library", "Unity asset cache, regenerated on project open"),
                ("Temp", "Unity temporary files, regenerated on project open"),
                ("Obj", "Unity build intermediates, regenerated on build"),
            ] {
                let path = dir.join(name);
                if path.is_dir() {
                    let size = utils::dir_size(&path);
                    targets.push(CleanTarget {
                        label: format!("{}", path.display()),
                        size_bytes: size,
                        action: CleanAction::DeletePath(path),
                        plugin_name: self.name().to_string(),
                        safety: Safety::Recoverable,
                        explanation: explanation.to_string(),
                    });
                }
            }
        }

        targets
    }

    fn skip_subdirectories(&self) -> &'static [&'static str] {
        &["Library", "Temp"]
    }

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