use std::path::Path;

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

pub struct StorybookPlugin;

impl Plugin for StorybookPlugin {
    fn name(&self) -> &str {
        "storybook"
    }

    fn description(&self) -> &str {
        "Storybook static build output"
    }

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

        let storybook_config = dir.join(".storybook");
        if storybook_config.is_dir() {
            let storybook_static = dir.join("storybook-static");
            if storybook_static.is_dir() {
                let size = utils::dir_size(&storybook_static);
                targets.push(CleanTarget {
                    label: format!("{}", storybook_static.display()),
                    size_bytes: size,
                    action: CleanAction::DeletePath(storybook_static),
                    plugin_name: self.name().to_string(),
                    safety: Safety::Recoverable,
                    explanation: "Storybook build output, regenerated with `storybook build`"
                        .to_string(),
                });
            }
        }

        targets
    }

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