//! Configuration for message queue consumer
//!
//! This module contains intentional violations for Aphoria dogfood testing.

use std::time::Duration;

/// Consumer configuration with multiple safety violations
#[derive(Debug, Clone)]
pub struct ConsumerConfig {
    /// Broker connection URL (e.g., "amqp://localhost:5672")
    pub broker_url: String,

    /// Queue name to consume from
    pub queue_name: String,

    // ==================== VIOLATION 1: Zero Timeout ====================
    /// Consumer timeout for broker operations
    ///
    /// @aphoria:claim[safety] Consumer timeout MUST NOT be zero -- timeout=0 causes indefinite blocking under connection loss
    pub timeout: Duration,
    // VIOLATION: Set to Duration::from_secs(0) in Default impl

    // ==================== VIOLATION 2: Missing Backpressure ====================
    /// Maximum in-memory queue size (None = unbounded)
    ///
    /// @aphoria:claim[safety] In-memory queue MUST be bounded (100-10000 recommended) -- unbounded queue causes OOM under sustained load
    pub max_queue_size: Option<usize>,
    // VIOLATION: Set to None in Default impl

    // ==================== VIOLATION 3: Unbounded Prefetch ====================
    /// QoS prefetch count - how many messages to fetch ahead
    ///
    /// @aphoria:claim[safety] Prefetch count MUST be bounded (1-100 recommended) -- unbounded prefetch exhausts memory
    pub prefetch_count: u16,
    // VIOLATION: Set to u16::MAX in Default impl

    /// Heartbeat interval for keepalive
    pub heartbeat_interval: Duration,

    /// Idle timeout for detecting dead connections
    pub idle_timeout: Duration,

    /// Whether to enable metrics collection
    pub metrics_enabled: bool,

    /// TLS configuration
    pub tls_config: TlsConfig,

    /// Connection pool configuration
    pub connection_pool: ConnectionPoolConfig,
}

/// TLS configuration
#[derive(Debug, Clone)]
pub struct TlsConfig {
    /// Whether TLS is enabled
    pub enabled: bool,

    // ==================== VIOLATION 6: Missing TLS Validation ====================
    /// Whether to verify TLS certificates
    ///
    /// @aphoria:claim[security] TLS certificate validation MUST be enabled -- disabled validation allows MITM attacks
    pub verify_certificates: bool,
    // VIOLATION: Set to false in Default impl

    /// Minimum TLS version (should be "1.2" or higher)
    pub min_version: String,
}

/// Connection pool configuration
#[derive(Debug, Clone)]
pub struct ConnectionPoolConfig {
    // ==================== VIOLATION 7: No Connection Pooling ====================
    /// Maximum number of connections (None = unbounded)
    ///
    /// @aphoria:claim[safety] Max connections MUST be bounded (1-10 recommended) -- unbounded connections exhaust broker file descriptors
    pub max_connections: Option<usize>,
    // VIOLATION: Set to None in Default impl

    /// Idle timeout before closing unused connections
    pub idle_timeout: Duration,

    /// Maximum connection lifetime
    pub max_lifetime: Duration,
}

impl Default for ConsumerConfig {
    fn default() -> Self {
        Self {
            broker_url: "amqp://localhost:5672".to_string(),
            queue_name: "default_queue".to_string(),

            // ❌ VIOLATION 1: Zero timeout
            timeout: Duration::from_secs(0),

            // ❌ VIOLATION 2: Unbounded queue
            max_queue_size: None,

            // ❌ VIOLATION 3: Unbounded prefetch
            prefetch_count: u16::MAX,

            heartbeat_interval: Duration::from_secs(30),
            idle_timeout: Duration::from_secs(60),
            metrics_enabled: true,

            tls_config: TlsConfig::default(),
            connection_pool: ConnectionPoolConfig::default(),
        }
    }
}

impl Default for TlsConfig {
    fn default() -> Self {
        Self {
            enabled: true,

            // ❌ VIOLATION 6: No certificate verification
            verify_certificates: false,

            min_version: "1.2".to_string(),
        }
    }
}

impl Default for ConnectionPoolConfig {
    fn default() -> Self {
        Self {
            // ❌ VIOLATION 7: Unbounded connections
            max_connections: None,

            idle_timeout: Duration::from_secs(300),
            max_lifetime: Duration::from_secs(3600),
        }
    }
}

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

    #[test]
    fn test_config_creation() {
        let config = ConsumerConfig::default();
        assert_eq!(config.broker_url, "amqp://localhost:5672");
        assert_eq!(config.queue_name, "default_queue");
    }

    #[test]
    fn test_tls_config() {
        let tls = TlsConfig::default();
        assert!(tls.enabled);
        assert_eq!(tls.min_version, "1.2");
    }
}
