// Placeholder types for new event system (will be replaced when domain module is activated)
#[derive(Debug, Clone)]
pub struct NewEvent {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub source: String,
    pub event_type: NewEventType,
}

#[derive(Debug, Clone)]
pub enum NewEventType {
    MarketData {
        symbol: String,
        data: crate::MarketDataType,
    },
    OrderFilled {
        order_id: String,
        fill: crate::Fill,
    },
    OrderSubmitted {
        order_id: String,
    },
    OrderCancelled {
        order_id: String,
    },
    PositionUpdated {
        symbol: String,
        update: crate::PositionUpdate,
    },
    RiskLimitExceeded {
        reason: String,
    },
}
use std::collections::HashMap;
use tokio::sync::mpsc;

// Placeholder EventBus type (will be replaced when events module is activated)
pub struct NewEventBus {
    sender: mpsc::UnboundedSender<NewEvent>,
}

impl NewEventBus {
    pub async fn publish(&mut self, event: NewEvent) {
        let _ = self.sender.send(event);
    }
}

/// Maps between old and new event types
pub struct EventAdapter;

impl EventAdapter {
    /// Convert from new event to old event format if needed
    pub fn from_new_event(event: &NewEvent) -> Option<OldEventFormat> {
        match &event.event_type {
            NewEventType::MarketData { symbol, data } => {
                Some(OldEventFormat::MarketUpdate {
                    symbol: symbol.clone(),
                    timestamp: event.timestamp,
                    data: data.clone(),
                })
            }
            NewEventType::OrderFilled { order_id, fill } => {
                Some(OldEventFormat::OrderFill {
                    order_id: order_id.clone(),
                    fill: fill.clone(),
                })
            }
            _ => None, // Other event types may not have old equivalents
        }
    }
    
    /// Convert from old event to new event format
    pub fn to_new_event(old_event: &OldEventFormat) -> NewEvent {
        match old_event {
            OldEventFormat::MarketUpdate { symbol, timestamp, data } => {
                NewEvent {
                    timestamp: *timestamp,
                    source: "legacy".to_string(),
                    event_type: NewEventType::MarketData {
                        symbol: symbol.clone(),
                        data: data.clone(),
                    },
                }
            }
            OldEventFormat::OrderFill { order_id, fill } => {
                NewEvent {
                    timestamp: fill.timestamp,
                    source: "legacy".to_string(),
                    event_type: NewEventType::OrderFilled {
                        order_id: order_id.clone(),
                        fill: fill.clone(),
                    },
                }
            }
        }
    }
}

/// Placeholder for old event format (to be defined based on actual old implementation)
#[derive(Debug, Clone)]
pub enum OldEventFormat {
    MarketUpdate {
        symbol: String,
        timestamp: chrono::DateTime<chrono::Utc>,
        data: crate::MarketDataType,
    },
    OrderFill {
        order_id: String,
        fill: crate::Fill,
    },
}

/// Event bus adapter to bridge old and new event systems
pub struct EventBusAdapter {
    new_event_bus: Option<NewEventBus>,
    old_handlers: HashMap<String, Vec<Box<dyn Fn(&OldEventFormat) + Send + Sync>>>,
}

impl EventBusAdapter {
    pub fn new() -> Self {
        Self {
            new_event_bus: None,
            old_handlers: HashMap::new(),
        }
    }
    
    pub fn with_new_event_bus(mut self, event_bus: NewEventBus) -> Self {
        self.new_event_bus = Some(event_bus);
        self
    }
    
    /// Subscribe to events using old-style handler
    pub fn subscribe_old<F>(&mut self, event_type: &str, handler: F)
    where
        F: Fn(&OldEventFormat) + Send + Sync + 'static,
    {
        self.old_handlers
            .entry(event_type.to_string())
            .or_insert_with(Vec::new)
            .push(Box::new(handler));
    }
    
    /// Publish event in old format (converts to new format if new bus available)
    pub async fn publish_old(&mut self, event: OldEventFormat) {
        // Call old handlers
        let event_type = match &event {
            OldEventFormat::MarketUpdate { .. } => "market_update",
            OldEventFormat::OrderFill { .. } => "order_fill",
        };
        
        if let Some(handlers) = self.old_handlers.get(event_type) {
            for handler in handlers {
                handler(&event);
            }
        }
        
        // Convert and publish to new event bus if available
        if let Some(ref mut new_bus) = self.new_event_bus {
            let new_event = EventAdapter::to_new_event(&event);
            new_bus.publish(new_event).await;
        }
    }
}