//! Generated code access layer
//!
//! This module provides PAP-compliant access to generated resources using
//! the core-types trait system. Implementation projects provide concrete
//! trait implementations, while core-lib provides fallback implementations.

use rustelo_core_types::ContentKindRegistry;

/// Trait for accessing generated content types
/// Implementation projects should provide concrete implementations
pub trait GeneratedContentAccess {
    /// Get all available content types from generated code
    fn get_all_content_types() -> Vec<String>;

    /// Get enabled content types only
    fn get_enabled_content_types() -> Vec<String>;

    /// Create content kind registry from generated data
    fn create_content_kind_registry() -> ContentKindRegistry;
}

/// Trait for accessing generated configuration
/// Implementation projects should provide concrete implementations
pub trait GeneratedConfigAccess {
    /// Get server content URL from generated config
    fn get_server_content_url() -> String;

    /// Get base URL from generated config
    fn get_base_url() -> String;

    /// Check if running in production mode
    fn is_production() -> bool;
}

/// Fallback implementation for development/testing
pub struct FallbackGeneratedAccess;

impl GeneratedContentAccess for FallbackGeneratedAccess {
    fn get_all_content_types() -> Vec<String> {
        vec!["page".to_string(), "blog".to_string(), "docs".to_string()]
    }

    fn get_enabled_content_types() -> Vec<String> {
        Self::get_all_content_types()
    }

    fn create_content_kind_registry() -> ContentKindRegistry {
        ContentKindRegistry::new()
    }
}

impl GeneratedConfigAccess for FallbackGeneratedAccess {
    fn get_server_content_url() -> String {
        std::env::var("SERVER_CONTENT_URL").unwrap_or_else(|_| "/content".to_string())
    }

    fn get_base_url() -> String {
        std::env::var("BASE_URL").unwrap_or_else(|_| "http://localhost:3000".to_string())
    }

    fn is_production() -> bool {
        std::env::var("ENVIRONMENT")
            .map(|e| e == "production")
            .unwrap_or(false)
    }
}

// Public functions that use the fallback by default
// Implementation projects can override these by providing their own
// implementations

/// Get all content types using fallback implementation
/// Implementation projects should override this
pub fn get_all_content_types() -> Vec<String> {
    FallbackGeneratedAccess::get_all_content_types()
}

/// Get enabled content types using fallback implementation
/// Implementation projects should override this
pub fn get_enabled_content_types() -> Vec<String> {
    FallbackGeneratedAccess::get_enabled_content_types()
}

/// Create content kind registry using fallback implementation
/// Implementation projects should override this
pub fn create_content_kind_registry() -> ContentKindRegistry {
    FallbackGeneratedAccess::create_content_kind_registry()
}

/// Generated config module (to replace the missing generated_config calls)
pub mod generated_config {
    use super::{FallbackGeneratedAccess, GeneratedConfigAccess};

    /// Get server content URL using fallback implementation
    /// Implementation projects should override this
    pub fn get_server_content_url() -> String {
        FallbackGeneratedAccess::get_server_content_url()
    }

    /// Get base URL using fallback implementation
    /// Implementation projects should override this
    pub fn get_base_url() -> String {
        FallbackGeneratedAccess::get_base_url()
    }

    /// Check if running in production mode using fallback implementation
    /// Implementation projects should override this
    pub fn is_production() -> bool {
        FallbackGeneratedAccess::is_production()
    }
}
