use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use super::{Order, Fill, MarketUpdate, Position};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EventType {
    MarketData(MarketUpdate),
    OrderSubmitted(Order),
    OrderFilled { order_id: String, fill: Fill },
    OrderCancelled { order_id: String },
    OrderRejected { order_id: String, reason: String },
    PositionUpdate(Position),
    RiskAlert { message: String, severity: RiskSeverity },
    SystemStatus { status: SystemStatus },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskSeverity {
    Info,
    Warning,
    Critical,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SystemStatus {
    Starting,
    Running,
    Paused,
    Stopping,
    Error(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
    pub id: String,
    pub timestamp: DateTime<Utc>,
    pub event_type: EventType,
}

impl Event {
    pub fn new(event_type: EventType) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            timestamp: Utc::now(),
            event_type,
        }
    }
    
    pub fn market_data(data: MarketUpdate) -> Self {
        Self::new(EventType::MarketData(data))
    }
    
    pub fn order_submitted(order: Order) -> Self {
        Self::new(EventType::OrderSubmitted(order))
    }
    
    pub fn order_filled(order_id: String, fill: Fill) -> Self {
        Self::new(EventType::OrderFilled { order_id, fill })
    }
}

pub trait EventHandler: Send + Sync {
    fn handle_event(&self, event: &Event);
    fn event_types(&self) -> Vec<String>; // Which event types this handler is interested in
}