//! Core framework traits and abstractions
//!
//! This module provides essential traits and abstractions used throughout
//! the Rustelo framework. These are truly generic interfaces that don't
//! belong to any specific domain.
//!
//! ## Key Principles
//!
//! - **Zero dependencies**: No external crates beyond serde
//! - **Generic abstractions**: Domain-agnostic interfaces
//! - **Extensibility**: Designed for framework extension points

#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(clippy::all)]

use std::collections::HashMap;
use core::fmt;

// Re-export serde for convenience
pub use serde::{Deserialize, Serialize};

/// Framework configuration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FrameworkConfig {
    /// Current environment
    pub environment: Environment,
    /// Debug mode enabled
    pub debug_enabled: bool,
    /// Hot reload enabled (development)
    pub hot_reload: bool,
    /// Cache configuration
    pub cache_config: CacheConfig,
    /// Security configuration
    pub security_config: SecurityConfig,
}

/// Environment configuration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Environment {
    /// Development environment
    Development,
    /// Staging environment
    Staging,
    /// Production environment
    Production,
    /// Custom environment
    Custom(String),
}

impl fmt::Display for Environment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Environment::Development => write!(f, "development"),
            Environment::Staging => write!(f, "staging"),
            Environment::Production => write!(f, "production"),
            Environment::Custom(name) => write!(f, "{}", name),
        }
    }
}

/// Cache configuration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CacheConfig {
    /// Whether caching is enabled
    pub enabled: bool,
    /// Maximum cache entries
    pub max_entries: usize,
    /// Cache TTL in seconds
    pub ttl_seconds: u64,
    /// Whether to cache in memory
    pub memory_cache: bool,
    /// Whether to cache to disk
    pub disk_cache: bool,
}

/// Security configuration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// CORS enabled
    pub cors_enabled: bool,
    /// Allowed origins for CORS
    pub cors_origins: Vec<String>,
    /// Security headers enabled
    pub security_headers: bool,
    /// CSRF protection enabled
    pub csrf_protection: bool,
    /// Rate limiting configuration
    pub rate_limiting: Option<RateLimitConfig>,
}

/// Rate limiting configuration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Maximum requests per window
    pub max_requests: u32,
    /// Time window in seconds
    pub window_seconds: u64,
    /// Whether to enable burst allowance
    pub allow_burst: bool,
}

/// Error information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ErrorInfo {
    /// Error code
    pub code: String,
    /// Human-readable message
    pub message: String,
    /// Additional error details
    pub details: HashMap<String, String>,
    /// Language for the error message
    pub language: String,
}

/// Performance metrics
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Request processing time in milliseconds
    pub processing_time_ms: u64,
    /// Memory usage in bytes
    pub memory_usage_bytes: u64,
    /// Cache hit/miss information
    pub cache_hits: u32,
    /// Cache misses
    pub cache_misses: u32,
    /// Database queries executed
    pub db_queries: u32,
    /// Additional custom metrics
    pub custom_metrics: HashMap<String, f64>,
}

/// Build information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BuildInfo {
    /// Framework version
    pub framework_version: String,
    /// Build timestamp
    pub build_timestamp: String,
    /// Git commit hash
    pub git_commit: Option<String>,
    /// Build environment
    pub build_environment: Environment,
    /// Feature flags enabled at build time
    pub enabled_features: Vec<String>,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_entries: 1000,
            ttl_seconds: 3600, // 1 hour
            memory_cache: true,
            disk_cache: false,
        }
    }
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            cors_enabled: true,
            cors_origins: vec!["*".to_string()],
            security_headers: true,
            csrf_protection: true,
            rate_limiting: None,
        }
    }
}

impl Default for FrameworkConfig {
    fn default() -> Self {
        Self {
            environment: Environment::Development,
            debug_enabled: true,
            hot_reload: true,
            cache_config: CacheConfig::default(),
            security_config: SecurityConfig::default(),
        }
    }
}
