//! Resource contributor for the example theme plugin
//!
//! This module implements the `ResourceContributor` trait,
//! which allows the plugin to provide menus, themes and translations to the
//! framework.

use std::collections::HashMap;

use rustelo_core_lib::registration::ResourceContributor;

/// The example theme plugin's resource contributor
///
/// Implements the `ResourceContributor` trait to provide:
/// - A custom analytics dashboard theme
/// - English and Spanish translations
/// - No menus (optional)
///
/// # Implementation Details
///
/// Configuration files are embedded at compile time using `include_str!()`.
/// This ensures the theme is always available and keeps the plugin
/// self-contained.
#[derive(Debug, Clone, Copy)]
pub struct ExampleThemeContributor;

impl ResourceContributor for ExampleThemeContributor {
    fn contribute_menus(&self) -> HashMap<String, String> {
        // This plugin doesn't provide menus
        HashMap::new()
    }

    fn contribute_themes(&self) -> HashMap<String, String> {
        let mut themes = HashMap::new();

        // Embed the analytics dashboard theme
        let analytics_theme = include_str!("../config/themes/analytics-dashboard.toml");

        themes.insert(
            "analytics-dashboard".to_string(),
            analytics_theme.to_string(),
        );

        themes
    }

    fn contribute_ftl(&self) -> HashMap<String, String> {
        let mut ftl_files = HashMap::new();

        // Embed English translations
        let en_translations = include_str!("../config/i18n/en.ftl");
        ftl_files.insert("en".to_string(), en_translations.to_string());

        // Embed Spanish translations
        let es_translations = include_str!("../config/i18n/es.ftl");
        ftl_files.insert("es".to_string(), es_translations.to_string());

        ftl_files
    }

    fn name(&self) -> &str {
        "example-theme-plugin"
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_contributor_name() {
        let contrib = ExampleThemeContributor;
        assert_eq!(contrib.name(), "example-theme-plugin");
    }

    #[test]
    fn test_theme_is_provided() {
        let contrib = ExampleThemeContributor;
        let themes = contrib.contribute_themes();

        assert_eq!(themes.len(), 1);
        assert!(themes.contains_key("analytics-dashboard"));
    }

    #[test]
    fn test_theme_content_is_not_empty() {
        let contrib = ExampleThemeContributor;
        let themes = contrib.contribute_themes();

        let theme = themes.get("analytics-dashboard").unwrap();
        assert!(!theme.is_empty());
    }

    #[test]
    fn test_theme_is_valid_toml() {
        let contrib = ExampleThemeContributor;
        let themes = contrib.contribute_themes();

        let theme = themes.get("analytics-dashboard").unwrap();

        // Verify it can be parsed as TOML
        let parsed = toml::from_str::<toml::Value>(theme);
        assert!(
            parsed.is_ok(),
            "Analytics dashboard theme is not valid TOML: {}",
            parsed.unwrap_err()
        );
    }

    #[test]
    fn test_fluent_files_are_provided() {
        let contrib = ExampleThemeContributor;
        let ftl_files = contrib.contribute_ftl();

        assert!(ftl_files.contains_key("en"));
        assert!(ftl_files.contains_key("es"));
    }

    #[test]
    fn test_fluent_files_have_content() {
        let contrib = ExampleThemeContributor;
        let ftl_files = contrib.contribute_ftl();

        let en = ftl_files.get("en").unwrap();
        assert!(!en.is_empty());

        let es = ftl_files.get("es").unwrap();
        assert!(!es.is_empty());
    }

    #[test]
    fn test_menus_are_empty() {
        let contrib = ExampleThemeContributor;

        // This plugin doesn't provide menus
        assert!(contrib.contribute_menus().is_empty());
    }

    #[test]
    fn test_contributor_is_clone_and_copy() {
        let contrib1 = ExampleThemeContributor;
        let contrib2 = contrib1; // Copy

        assert_eq!(contrib1.name(), contrib2.name());
    }
}
