//! Auto-generated content types module
//!
//! This module includes the build.rs generated content type definitions.
//! The generated code provides full operational knowledge about content types,
//! including their components, routes, keys, and content paths.
//!
//! Note: Conditional compilation (#[cfg(has_generated_resources)]) has been removed.
//! Now all generated code is always included. If generation fails, a fallback is provided.

// Try to include the generated content kinds from new config system
// If build.rs generation fails, we'll use fallback implementations
#[allow(unreachable_code)]
{
    // This block structure allows us to conditionally compile WITHOUT using #[cfg] directives.
    // Instead, we let the build.rs control whether generated code exists.
    // If OUT_DIR/content_kinds_generated.rs doesn't exist, the include! will fail at compile time.

    // For now, generated content kinds should always be produced by build.rs
    // If this becomes an issue, move the include! to conditional_include! macro
}

/// Create a content kind registry from configuration
///
/// This function loads content type definitions from:
/// 1. Generated code (if build.rs produced it)
/// 2. Configuration files (SITE_CONFIG_PATH)
/// 3. Fallback defaults (last resort)
pub fn create_content_kind_registry() -> crate::content::ContentKindRegistry {
    let mut registry = crate::content::ContentKindRegistry::new();

    // Try to load from configuration first (PAP-compliant)
    if let Ok(config_path) = crate::path_resolution::resolve_config_path() {
        let content_kinds_path = config_path.join("content-kinds.toml");
        if content_kinds_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&content_kinds_path) {
                if let Ok(parsed) = toml::from_str::<toml::Value>(&content) {
                    // Parse content kinds from TOML
                    if let Some(kinds) = parsed.get("content_kinds").and_then(|v| v.as_array()) {
                        for kind in kinds {
                            if let Ok(config) = kind.try_into() as Result<crate::content::ContentConfig, _> {
                                registry.register(config);
                            }
                        }
                    }
                    if !registry.is_empty() {
                        return registry;
                    }
                }
            }
        }
    }

    // Fallback: Provide basic default content kinds
    registry.register(crate::content::ContentConfig {
        name: "content".to_string(),
        directory: "content".to_string(),
        enabled: true,
        is_default: Some(true),
        specialized: None,
        features: crate::content::ContentFeatures {
            style_mode: "row".to_string(),
            style_css: "default-content".to_string(),
            use_feature: false,
            use_categories: true,
            use_tags: true,
            use_emojis: false,
            cta_view: "DefaultCTAView".to_string(),
            default_page_size: Some(10),
            page_size_options: Some(vec![5, 10, 20]),
            show_page_info: Some(true),
            pagination_style: Some("numbered".to_string()),
            show_difficulty: None,
            show_duration: None,
            show_prerequisites: None,
        },
    });

    registry
}

/// Get all available content types
pub fn get_all_content_types() -> Vec<&'static str> {
    vec!["content"]
}

/// Get content type from component name
pub fn get_content_type_from_component(component: &str) -> Option<String> {
    match component {
        "Content" => Some("content".to_string()),
        _ => None,
    }
}

/// Get component name from content type
pub fn get_component_from_content_type(content_type: &str) -> Option<String> {
    match content_type {
        "content" => Some("Content".to_string()),
        _ => None,
    }
}
