//! Footer component for the HTMX-SSR rendering profile.

use html_escape::{encode_double_quoted_attribute, encode_text};

/// Site-level footer metadata passed from the project's shell.
pub struct FooterConfig<'a> {
    pub title: &'a str,
    pub description: &'a str,
    pub copyright: &'a str,
    /// Each entry: `(display_name, url, icon_class)`.
    pub social_links: &'a [(&'a str, &'a str, &'a str)],
    /// Footer nav links: `(label, href)`.
    pub nav_links: &'a [(&'a str, &'a str)],
}

/// Render the site footer with site-specific config.
pub fn render_footer_with_config(config: &FooterConfig<'_>, _lang: &str) -> String {
    let social_html = config
        .social_links
        .iter()
        .map(|(name, url, icon)| {
            format!(
                r##"<a href="{url}" target="_blank" rel="noopener noreferrer" aria-label="{name}" class="ds-text-muted hover:ds-text transition-colors"><i class="{icon}"></i></a>"##,
                url  = encode_double_quoted_attribute(url),
                name = encode_double_quoted_attribute(name),
                icon = encode_double_quoted_attribute(icon),
            )
        })
        .collect::<String>();

    let nav_html = config
        .nav_links
        .iter()
        .map(|(label, href)| {
            format!(
                r##"<a href="{href}" class="ds-text-muted hover:ds-text text-xs transition-colors no-underline">{label}</a>"##,
                href  = encode_double_quoted_attribute(href),
                label = encode_text(label),
            )
        })
        .collect::<String>();

    format!(
        r##"<footer class="border-t border-gray-200 dark:border-gray-700 mt-auto py-8"><div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"><div class="flex flex-col sm:flex-row justify-between items-center gap-4"><div class="text-center sm:text-left"><p class="font-semibold ds-text text-sm">{title}</p><p class="text-xs ds-text-muted">{desc}</p></div><div class="flex items-center gap-4">{social}</div></div><div class="mt-6 pt-4 border-t border-gray-100 dark:border-gray-800 flex flex-col sm:flex-row justify-between items-center gap-2"><p class="text-xs ds-text-muted">{copy}</p><div class="flex items-center gap-4">{nav}</div></div></div></footer>"##,
        title = encode_text(config.title),
        desc = encode_text(config.description),
        social = social_html,
        copy = encode_text(config.copyright),
        nav = nav_html,
    )
}

/// Minimal footer fallback used when no `FooterConfig` is available.
pub fn render_footer(lang: &str) -> String {
    let _ = lang;
    r#"<footer class="border-t border-gray-200 dark:border-gray-700 mt-auto py-6"><div class="max-w-7xl mx-auto px-4 text-xs ds-text-muted text-center">© Jesús Pérez</div></footer>"#.to_string()
}

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

    #[test]
    fn renders_social_links() {
        let cfg = FooterConfig {
            title: "Test",
            description: "Desc",
            copyright: "© 2024",
            social_links: &[("GitHub", "https://github.com/x", "fab fa-github")],
            nav_links: &[],
        };
        let html = render_footer_with_config(&cfg, "en");
        assert!(html.contains("https://github.com/x"));
        assert!(html.contains("fab fa-github"));
    }

    #[test]
    fn renders_nav_links() {
        let cfg = FooterConfig {
            title: "T",
            description: "D",
            copyright: "C",
            social_links: &[],
            nav_links: &[("Privacy", "/privacy")],
        };
        let html = render_footer_with_config(&cfg, "en");
        assert!(html.contains("/privacy"));
        assert!(html.contains("Privacy"));
    }
}
