//! Stream Server pour Veza
//!
//! Serveur de streaming audio temps réel avec WebRTC

pub mod analytics;
pub mod audio;
pub mod auth;
pub mod cache;
pub mod codecs;
pub mod config;
pub mod core;
pub mod database;
pub mod error;
pub mod event_bus;
pub mod health;
pub mod middleware;
pub mod monitoring;
pub mod notifications;
pub mod routes {
    pub mod transcode;
}
pub mod streaming;
pub mod structured_logging;
pub mod transcoding; // NEW: Phase 3 Transcoding Engine
pub mod utils; // ORIGIN Architecture: Event-driven via RabbitMQ

use std::collections::HashMap;
use std::sync::Arc;

/// État global de l'application
/// Cette structure contient tous les services et composants nécessaires au serveur
#[derive(Clone)]
pub struct AppState {
    pub config: Arc<config::Config>,
    // Ces champs sont initialisés dans main.rs via create_app_state()
    // Utilisation de pub pour permettre l'accès depuis main.rs et les routes
    pub cache: Arc<cache::FileCache>,
    pub metrics: Arc<utils::metrics::Metrics>,
    pub analytics: Arc<analytics::AnalyticsEngine>,
    pub audio_processor: Arc<audio::processing::AudioProcessor>,
    pub adaptive_streaming: Arc<streaming::adaptive::AdaptiveStreamingManager>,
    pub health_monitor: Arc<health::HealthMonitor>,
    pub auth_manager: Arc<auth::AuthManager>,
    pub compression_engine: Arc<audio::compression::CompressionEngine>,
    pub notification_service: Arc<notifications::NotificationService>,
    pub websocket_manager: Arc<streaming::websocket::WebSocketManager>,
    pub event_bus: Option<Arc<event_bus::RabbitMQEventBus>>, // Add RabbitMQEventBus, wrapped in Arc for Clone trait
    pub transcoding_engine: Arc<transcoding::TranscodingEngine>, // Transcoding engine for audio encoding
}

impl AppState {
    pub async fn new(config: config::Config) -> Result<Self, Box<dyn std::error::Error>> {
        let config_arc = Arc::new(config.clone());

        // Cache needs max_size_mb as usize
        let cache = Arc::new(cache::FileCache::new(config.cache.max_size_mb as usize));

        // Metrics needs config
        let metrics = Arc::new(utils::metrics::Metrics::new(config_arc.clone()));

        // AnalyticsEngine::new is async and returns Result
        let analytics = Arc::new(
            analytics::AnalyticsEngine::new(&config.database.url, config_arc.clone()).await?,
        );

        let audio_processor = Arc::new(audio::processing::AudioProcessor::new(config_arc.clone()));

        let compression_engine = Arc::new(audio::compression::CompressionEngine::new(
            config_arc.clone(),
        ));

        // HealthMonitor needs config
        let health_monitor = Arc::new(health::HealthMonitor::new(config_arc.clone()));

        // AuthManager::new returns Result
        let auth_manager = Arc::new(auth::AuthManager::new(config_arc.clone())?);

        let notification_service =
            Arc::new(notifications::NotificationService::new(config_arc.clone()));

        // WebSocketManager::new takes no arguments
        let websocket_manager = Arc::new(streaming::websocket::WebSocketManager::new());

        // Adaptive streaming manager
        let adaptive_streaming = Arc::new(streaming::adaptive::AdaptiveStreamingManager::new(
            config_arc.clone(),
        ));

        // Transcoding engine
        let worker_count = num_cpus::get();
        let transcoding_engine = Arc::new(transcoding::TranscodingEngine::new(worker_count));
        // Démarrer les workers
        transcoding_engine.start();

        Ok(Self {
            config: config_arc,
            cache,
            metrics,
            analytics,
            audio_processor,
            adaptive_streaming,
            health_monitor,
            auth_manager,
            compression_engine,
            notification_service,
            websocket_manager,
            event_bus: None, // Initialisé à None, sera assigné dans main.rs
            transcoding_engine,
        })
    }
}

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

    #[test]
    fn test_app_state_structure() {
        // Just verify struct exists
        assert!(true);
    }
}
