use std::path::Path;

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

pub struct HaskellPlugin;

impl Plugin for HaskellPlugin {
    fn name(&self) -> &str {
        "haskell"
    }

    fn description(&self) -> &str {
        "Haskell Stack/Cabal build artifacts"
    }

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

        // Stack projects
        if dir.join("stack.yaml").exists() {
            let stack_work = dir.join(".stack-work");
            if stack_work.is_dir() {
                let size = utils::dir_size(&stack_work);
                targets.push(CleanTarget {
                    label: format!("{}", stack_work.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(stack_work),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Stack build artifacts, regenerated with `stack build`"
                        .to_string(),
                });
            }
        }

        // Cabal projects
        if dir.join("cabal.project").exists() {
            let dist_newstyle = dir.join("dist-newstyle");
            if dist_newstyle.is_dir() {
                let size = utils::dir_size(&dist_newstyle);
                targets.push(CleanTarget {
                    label: format!("{}", dist_newstyle.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(dist_newstyle),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Cabal build artifacts, regenerated with `cabal build`"
                        .to_string(),
                });
            }
        }

        targets
    }

    fn markers(&self) -> &'static [&'static str] {
        &["stack.yaml", "cabal.project"]
    }
}
