//! Safe CTA Section component for unified content pages using
//! configuration-driven design

use std::collections::HashMap;

// Note: get_content_safe function not available - using direct approach
use leptos::prelude::*;

use crate::content::helpers::defs::CTAViewType;

// Simple content getter function
fn get_content_safe(content: &Option<HashMap<String, String>>, key: &str) -> String {
    content
        .as_ref()
        .and_then(|map| map.get(key))
        .cloned()
        .unwrap_or_else(|| key.to_string())
}

/// Safe CTA section component with dynamic configuration
#[component]
pub fn CTASection(
    #[prop(into)] cta_type: CTAViewType,
    #[prop(into)] content_type_key: String,
    #[prop(default = None)] lang_content: Option<std::collections::HashMap<String, String>>,
) -> impl IntoView {
    // Pre-compute all values before view! to avoid reactive issues
    let content_getter = move |key: &str| get_content_safe(&lang_content, key);

    // Pre-compute all strings using fully dynamic configuration
    let (title_key, desc_key, primary_text, primary_url, secondary_text, secondary_url) =
        match cta_type {
            CTAViewType::Default => (
                "default-cta-title".to_string(),
                "default-cta-description".to_string(),
                "default-cta-contact".to_string(),
                "/contact".to_string(),
                "".to_string(), // No secondary button for default
                "".to_string(),
            ),
            CTAViewType::Custom(custom_type) => (
                format!("{}-category-cta-title", content_type_key),
                format!("{}-category-cta-description", content_type_key),
                format!("{}-cta-primary", custom_type),
                "/contact".to_string(),
                format!("{}-cta-secondary", custom_type),
                "/services".to_string(),
            ),
        };

    // Get content values before view!
    let title = content_getter(&title_key);
    let description = content_getter(&desc_key);
    let primary_button_text = content_getter(&primary_text);
    let secondary_button_text = if !secondary_text.is_empty() {
        Some(content_getter(&secondary_text))
    } else {
        None
    };

    view! {
        <section class="mt-12 text-center">
            <div class="ds-bg ds-rounded-lg ds-shadow-lg p-8">
                <h3 class="text-2xl font-bold ds-text mb-4">{title}</h3>
                <p class="ds-text-secondary mb-6">{description}</p>
                <div class="flex flex-col sm:flex-row gap-4 justify-center">
                    <a href={primary_url} class="ds-btn-primary">
                        {primary_button_text}
                    </a>
                    {secondary_button_text.map(|text| {
                        view! {
                            <a href={secondary_url.clone()} class="ds-btn-secondary">
                                {text}
                            </a>
                        }
                    })}
                </div>
            </div>
        </section>
    }
}
